题目:
定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
现在,给定一个字符串s,请求出一个字符串,使得:1、它所代表的整数是一个幸运数字;
2、它非空;
3、它作为s的子串(不是子序列)出现了最多的次数(不能为0次)。
请求出这个串(如果有多解,请输出字典序最小的那一个)。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <cstring>
using namespace std;
char a[55];
int c[2];
int main()
{
memset(a,'\0',sizeof(a));
memset(c,0,sizeof(c));
scanf("%s",a);
for(int i =0;i<55;i++)
{
if(a[i]=='4') c[0]++;
else if(a[i]=='7') c[1]++;
}
if(c[1]==0&&c[0]==0)
printf("-1\n");
else if(c[1]>c[0])
printf("%c\n",'7');
else if(c[0]>=c[1])
printf("%c\n",'4');
return 0;
}
1.memset();
extern void *memset(void *buffer, int c, int count)
buffer:为指针或是数组,
c:是赋给buffer的值,
count:是buffer的长度.
Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;
例:char a[100];memset(a, '/0', sizeof(a));
memset可以方便的清空一个结构类型的变量或数组。
2.
例:substr(字符串,截取开始位置,截取长度) //返回截取的字
var s, ss; //声明变量。
var s = "The rain in Spain falls mainly in the plain.";
ss = s.substr(12, 5); //获取子字符串。
return(ss); //返回 "Spain"。