题解 | #大整数的因子#
大整数的因子
http://www.nowcoder.com/practice/3d6cee12fbf54ea99bb165cbaba5823d
判断大整数能否被整除,直接用字符串从最高位开始取余即可
#include<iostream> #include<string> using namespace std; bool isDividable(string num,int divider){ int total = 0; for(int i=0;i<num.size();i++){ int digit = num[i]-'0'; total *= 10; total += digit; total %= divider; } return total==0; } int main(){ string c; while(cin>>c){ if(c=="-1") break; bool first = true; for(int i=2;i<=9;i++){ if(isDividable(c,i)){ if(first){ first = false; } else { cout<<" "; } cout<<i; } } if(first) cout<<"none"; cout<<endl; } return 0; }