题解 | #大整数的因子#

大整数的因子

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;
}


全部评论

相关推荐

NBA球星伦纳德:jd是这样的,工作连拧螺丝都算不上
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务