1117 数字之王


solution

  • 判断现有数字是否全为个位数
    • 全为个位数,找出出现次数最多的数字,并首行输出最多出现次数,第二行输出所有出现该次数的数值
    • 不全为个位数
      • 若当前位数值为0,无需处理
      • 若当前位数值非0,则每位立方相乘,并把结果个位和相加更新为当前位新数值
cpp 复制代码
#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

cpp 复制代码
#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;
} 
相关推荐
吃好睡好便好4 小时前
提取矩阵某一行或某一列元素
开发语言·人工智能·线性代数·算法·matlab·矩阵
wljy18 小时前
二、进制状态转换
linux·运维·服务器·c语言·c++
云泽8088 小时前
笔试算法 -位运算篇(二):从唯一字符到消失数字
c++·算法·位运算
ʚ希希ɞ ྀ8 小时前
不同路径|| -- dp
算法
繁华落尽,倾城殇?8 小时前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice9 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手9 小时前
C++-二叉搜索树
开发语言·c++
bucenggaibian9 小时前
《C语言》编程前置:计算机底层逻辑(诞生的基础)
c语言·程序框架·编译运行·内存地址·底层逻辑
IT 行者9 小时前
SimHash 与 MinHash:相似性计算的双子星算法
算法·hash·比对