PKU-大整数的因子

题目描述

已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k.

输入描述

若干个非负整数c,c的位数<=30
每行一个c

输出描述

每一个c的结果占一行
1) 若存在满足 c%k == 0 的k,输出所有这样的k,中间用空格隔开,最后一个k后面没有空格。
2) 若没有这样的k则输出"none"

注意整数溢出问题
不要对-1进行计算

程序代码

#include <iostream>
using namespace std;
int main()
{
	string num;
	cin >> num;
	int a[num.length()], i, k, mod, count=0;
	for(i=0; i<num.length(); i++)
	a[i]=num[i]-48;
	for(k=2; k<10; k++)
	{
		mod = 0;
	    for(i=0; i<num.length(); i++)
		mod = (mod*10+a[i])%k;
		if(!mod)
		{
			if(count) cout << " ";
			cout << k;
			count++;
		}
	}
	if(!count) cout << "none";
	cout << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38196810/article/details/81164037