A - Lucky Division
题目问输入的n是否是almost lucky number,almost lucky的定义是被lucky number 整除。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1005];
int lucky(int x)
{
int a;
while(x){
a = x % 10;
if(a == 4 || a == 7){
x = x / 10;
}
else{
return 0;
}
}
return 1;
}
int main()
{
int i, j;
memset(a, 0, sizeof(a));
for(i = 2; i <= 1000; ++i){
if(lucky(i)){
a[i] = 2;
continue;
}
for(j = 1; j < i; ++j){
if(a[j] == 2 && i % j == 0){
a[i] = 1;
break;
}
}
}
int n;
while(scanf("%d", &n) != EOF){
if(a[n])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
B - Lucky Substring
找出出现次数最多的串,串必须是lucky number,想一下就知道不是4就是7,不会是44之类的长度超过1的串,其实就是统计4和7的个数,如果个数一样输出逻辑值小的串,也就是4的个数。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char a[1005];
int main()
{
while(gets(a)){
int i,num_4,num_7;
for(i = num_4 = num_7 = 0; a[i]; ++i){
if(a[i] == '4')
num_4++;
if(a[i] == '7')
num_7++;
}
if(num_4 + num_7 == 0){
printf("-1\n");
continue;
}
if(num_4 >= num_7)
printf("4\n");
else
printf("7\n");
}
return 0;
}
C - Lucky Sum
首先定义next(n),表示最小的比n大的lucky number。题目输入a,b,求next(a) + next(a+1) + ……next(b)的值。马上想到打表看看10^9内的lucky number,不多,1000+,然后暴力查找。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
using namespace std;
set<__int64> S;
void GetNum(__int64 x)
{
if(x <= 10000000000){
S.insert(x);
GetNum(x * 10 + 4);
GetNum(x * 10 + 7);
}
return ;
}
int main()
{
S.clear();
GetNum(4);
GetNum(7);
__int64 l, r;
while(scanf("%I64d %I64d", &l, &r) != EOF){
__int64 ans = 0;
__int64 now = l;
while(now <= r){
now = *S.lower_bound(now);
if(now >= r){
ans = ans + (r - l + 1) * now;
break;
}
else{
ans = ans + (now - l + 1) * now;
now++;
l = now;
}
}
printf("%I64d\n", ans);
}
//printf("%d\n", S.size());
return 0;
}
D - Lucky Transformation
给定一个串d,做K次操作,每次操作是要找到最小的x,d(x) = d(x+1) = 4 或 7,如果x为奇数,改成d(x) = d(x+1) = 4,否则d(x) = d(x+1) = 7。假如没到K次已经不存在这样的操作,就结束。注意出现死循环。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char a[100005];
int b[100005];
int main()
{
int n, k;
while(scanf("%d %d", &n, &k) != EOF){
getchar();
gets(a);
int i = 0;
memset(b, 0, sizeof(b));
while(k && i < n - 1){
for(; i < n - 1; ++i){
if(a[i] == '4' && a[i + 1] == '7'){
if(i % 2 == 0){
a[i + 1] = '4';
b[i + 1]++;
k--;
if(b[i + 1] == 10000){
k = k % 2;
if(k == 0)
break;
}
}
else{
a[i] = '7';
b[i]++;
k--;
if(b[i] == 10000){
k = k % 2;
if(k == 0)
break;
}
i--;
}
break;
}
}
}
puts(a);
}
return 0;
}