Newcoder 70 A.幸運數字Ⅰ(水~)
阿新 • • 發佈:2018-12-16
Description
定義一個數字為幸運數字當且僅當它的所有數位都是或者。
比如說,都是幸運數字而都不是。
現在,給定一個字串,請求出一個字串,使得:
1、它所代表的整數是一個幸運數字;
2、它非空;
3、它作為的子串(不是子序列)出現了最多的次數(不能為0次)。
請求出這個串(如果有多解,請輸出字典序最小的那一個)。
Input
串。只包含數字字元,可以有前導零。
Output
一個串表示答案。
無解輸出。
Sample Input
047
Sample Output
4
Solution
統計的個數,如果的數量不小於的數量則為,否則是,如果不存在則無解
Code
#include<cstdio> #include<cstring> using namespace std; char s[55]; int main() { scanf("%s",s); int n=strlen(s),n4=0,n7=0; for(int i=0;i<n;i++) if(s[i]=='4')n4++; else if(s[i]=='7')n7++; if(!n4&&!n7)printf("-1\n"); else if(n4>=n7)printf("4\n"); else printf("7\n"); return 0; }