牛客练习赛13

题解:出现了最多次数的只可能是4或者7

 1 #pragma warning(disable:4996)
 2 #include<queue>
 3 #include<cstdio>
 4 #include<bitset>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 int cnt1, cnt2, len;
11 
12 int main()
13 {
14     char s[100];
15     scanf("%s", s);
16     len = strlen(s);
17     cnt1 = cnt2 = 0;
18     for (int i = 0; i < len; i++) {
19         if (s[i] == '4') cnt1++;
20         if (s[i] == '7') cnt2++;
21     }
22     if (cnt1 == 0 && cnt2 == 0) printf("-1\n");
23     else if (cnt1 >= cnt2) printf("4\n");
24     else printf("7\n");
25 }

题解:先构造出在1e10范围内的幸运数,然后分段讨论。

 1 #pragma warning(disable:4996)
 2 #include<queue>
 3 #include<cstdio>
 4 #include<bitset>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 typedef long long ll;
10 
11 const int maxn = 2000;
12 
13 ll cnt = 0;
14 ll a[maxn];
15 
16 void DFS(ll x, int d) {
17     a[cnt++] = x;
18     if (d == 9) return;
19     DFS(x * 10 + 4, d + 1);
20     DFS(x * 10 + 7, d + 1);
21 }
22 
23 void Inite() {
24     DFS(0, 0);
25     sort(a, a + cnt);
26     a[cnt++] = 4444444444;
27 }
28 
29 int main()
30 {
31     Inite();
32     ll l, r;
33     while (scanf("%lld%lld", &l, &r)!=EOF) {
34         ll ans = 0, pos = 0;
35         while (l <= r) {
36             while (a[pos] < l) pos++;
37             ans += (min(a[pos], r) - l + 1)*a[pos];
38             l = a[pos] + 1;
39         }
40         printf("%lld\n", ans);
41     }
42 }

题解:注意到两种情况,447,4在偶数的位置上。477,4在奇数的位置上。剩下的直接改就行。

 1 #pragma warning(disable:4996)
 2 #include<queue>
 3 #include<cstdio>
 4 #include<bitset>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 const int maxn = 1e5 + 5;
11 
12 int n, k;
13 char s[maxn];
14 
15 int main()
16 {
17     while (scanf("%d%d", &n, &k) != EOF) {
18         scanf("%s", s + 1);
19         int l = strlen(s+1);
20         for (int i = 1; i < l; i++) {
21             if (s[i] == '4' && s[i + 1] == '7') {
22                 //if (i == 1) { s[i + 1] = '4'; k--; continue; }
23                 if (i & 1) {
24                     if (i + 1 != l && s[i + 2] == '7') {
25                         if (k & 1) s[i + 1] = '4';
26                         break;
27                     }
28                     else s[i + 1] = '4';
29                 }
30                 else {
31                     if (i - 1 != 0 && s[i - 1] == '4') {
32                         if ((k & 1)) s[i] = '7';
33                         break;
34                     }
35                     else s[i] = '7';
36                 }
37                 k--;
38             }
39         }
40         printf("%s\n", s + 1);
41     }
42     return 0;
43 }

 

posted @ 2018-03-17 23:05  天之道,利而不害  阅读(252)  评论(0编辑  收藏  举报