OPENJUDGE 2925 大整数的因子

问题描述:


时间限制: 

描述
已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k.
输入
若干个非负整数c,c的位数<=30
每行一个c,当c=-1时中止
(不要对-1进行计算!)
输出
每一个c的结果占一行
1) 若存在满足 c%k == 0 的k,输出所有这样的k,中间用空格隔开,最后一个k后面有空格。
2) 若没有这样的k则输出"none"
样例输入
30
72
13
-1
样例输出
2 3 5 6 
2 3 4 6 8 9 
none
提示
注意整数溢出问题

不要对-1进行计算


解题思路:

There is an Digit-Based approach to figure out if 7 is a factor of an integer:

Multiply each digit with 1 3 2 -1 -3 -2 recursively from the rightmost digit to the leftmost digit and add these multiplied digits together to form a new integer, if such integer is to be divided by 7, so is the original integer.


程序代码:

# include <iostream>
# include <cstdio>
# include <string>
# include <vector>
# include <cmath>
# include <bitset>

using namespace std;

int checkList[6] = {1,3,2,-1,-3,-2};
bitset<8> affirm(0);

int digitSum(vector<int>& a)
{
	int len = a.size();
	int total = 0;
	for ( int i = 0; i < len; i++ )
	{
		total += a[i];
	}
	return total;
}

void findFactor(string& str)
{
	vector<int> inputNum;
	int len = str.length();
	int sum = 0;
	int factor = 1;
	for ( int i = 0; i < len; i++ )
	{
		inputNum.push_back(str[len-i-1]-'0');
	}
	
	//Check 8,4,2
	factor = 1;
	for ( int i = 0;( i < 3 && i < len ); i++ )
	{
		sum += inputNum[i] * (int)pow(10,(double)i);
	}
	if ( sum % 2 == 0 )
	{
		affirm[0] = true;
	}
	if ( sum % 4 == 0 )
	{
		affirm[2] = true;
	}
	if ( sum % 8 == 0 )
	{
		affirm[6] = true;
	}
	
	//Check 3,9
	sum = digitSum(inputNum);
	if ( sum % 9 == 0 )
	{
		affirm[7] = true;
		affirm[1] = true;
	}
	else if ( sum % 3 == 0 )
	{
		affirm[1] = true;
	}

	//Check 5
	if ( inputNum[0] == 0 || inputNum[0] == 5 )
	{
		affirm[3] = true;
	}

	//Check 6
	if ( affirm[0] == true && affirm[1] == true )
	{
		affirm[4] = true;
	}

	//Check 7 - An Step-in approach
	sum = 0;
	for ( int i = 0; i < len; i++ )
	{
		sum += checkList[i%6] * inputNum[i];
	}
	if ( sum % 7 == 0 )
	{
		affirm[5] = true;
	}
		
}



int main()
{
	
	string input;
	int fact = 0;
	while ( cin >> input )
	{
		if ( input == "-1" )
		{
			break;
		}
		affirm.reset();
		findFactor(input);
		if ( affirm.count() == 0 )
		{
			cout << "none" << endl;
		}
		else
		{
			for ( int i = 0; i < 8; i++ )
			{
				if ( affirm[i] == true )
				{
					cout << i+2 << " ";
				}
			}
			cout << endl;
		}

	}


	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值