hdu1013+ Educational Codeforces Round 59 (Rated for Div. 2)--B题(树根的概念)

当一个数m的数位和不是一个一位数的时候一直取他的数位和,直到是一个个位数这个数就是数字m的根

只需要m对9取余就可以了,当m > 0时,对9取余,若余数是0则根是9,否则就是余数

hdu1013要用大数取余,若一个极大数对一个较小的数取余

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
string n;
int main(){
	while(cin>>n,n!="0"){
		int len = n.size();
		int ans = 0;
		for(int i = 0;i < len;i++){
			ans = (int)(((long long)ans*10+n[i]-'0')%9);
		}
		if(ans==0) ans=9;
		printf("%d\n",ans);
	}
	return 0;
}

Codeforces ER 59 - B题

问第k个根为n的数是多少,9为他的循环节,所以第k个数根为n的数就是n+(k-1)*9`在这里插入代码片

#include <cstdio>
using namespace std;
int main(){
	int t;scanf("%d",&t);
	long long k,x;
	while(t--){
		scanf("%I64d %I64d",&k,&x);
		printf("%I64d\n",1ll*((k-1)*9+x));	
	}
} 
发布了27 篇原创文章 · 获赞 0 · 访问量 342

猜你喜欢

转载自blog.csdn.net/weixin_44083561/article/details/104145229
今日推荐