solution
- 判断现有数字是否全为个位数
- 全为个位数,找出出现次数最多的数字,并首行输出最多出现次数,第二行输出所有出现该次数的数值
- 不全为个位数
- 若当前位数值为0,无需处理
- 若当前位数值非0,则每位立方相乘,并把结果个位和相加更新为当前位新数值
#include<iostream>
using namespace std;
int n1, n2, flag[10] = {0}, a[1001], maxn = -1, judge = 0, first = 1, d;
int main(){
scanf("%d%d", &n1, &n2);
for(int i = n1; i <= n2; i++){
a[i] = i;
}
if(n2 > 9) judge = 1;
while(judge){
judge = 0;
for(int i = n1; i <= n2; i++){
int t1 = 1, t2 = 0, t = a[i];
if(t == 0) continue;
while(t){
d = t % 10;
t /= 10;
t1 *= d * d * d;
}
while(t1){
d = t1 % 10;
t1 /= 10;
t2 += d;
}
a[i] = t2;
if(t2 > 9) judge = 1;
}
}
for(int i = n1; i <= n2; i++){
flag[a[i]]++;
}
for(int i = 0; i < 10; i++){
if(flag[i] > maxn) maxn = flag[i];
}
printf("%d\n", maxn);
for(int i = 0; i < 10; i++){
if(flag[i] == maxn){
if(first) first = 0;
else printf(" ");
printf("%d", i);
}
}
return 0;
}
or
#include<iostream>
using namespace std;
int n1, n2, flag[10] = {0}, a[1001], maxn = -1, judge = 0, first = 1, d;
int main(){
scanf("%d%d", &n1, &n2);
for(int i = n1; i <= n2; i++) a[i] = i;
if(n2 > 9) judge = 1;
while(judge){
judge = 0;
for(int i = n1; i <= n2; i++){
int t1 = 1, t2 = 0, t = a[i];
if(t == 0) continue;
while(t){
d = t % 10;
t /= 10;
t1 *= d * d * d;
}
while(t1){
d = t1 % 10;
t1 /= 10;
t2 += d;
}
a[i] = t2;
if(t2 > 9) judge = 1;
}
}
for(int i = n1; i <= n2; i++){
flag[a[i]]++;
if(flag[a[i]] > maxn) maxn = flag[a[i]];
}
printf("%d\n", maxn);
for(int i = 0; i < 10; i++){
if(flag[i] == maxn){
if(first) first = 0;
else printf(" ");
printf("%d", i);
}
}
return 0;
}