Codeforces Beta Round #84 (Div. 2 Only) A. Nearly Lucky Number

http://codeforces.com/problemset/problem/110/A

题意:如果一个数字幸运数字的个数为幸运数字,则称它几乎是一个幸运数字。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

int main() {
long long n;
cin >> n;
int cnt = 0;
while (n) {
if ((n % 10) == 4 || (n % 10) == 7) cnt++;
n /= 10;
}
if (cnt == 4 || cnt == 7) printf("YES\n");
else printf("NO\n");
return 0;
};

把数字当做字符串输入也是个好方法。(避免了int范围不够的问题)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;

string s;
int i, cnt;

int main() {
cin >> s;
for (i = 0; i < s.size(); i++) if (s[i] == '4' || s[i] == '7') cnt++;
if (cnt == 4 || cnt == 7) cout << "YES\n"; else cout << "NO\n";
return 0;
};