ZOJ 1284 Perfection

来源:互联网 发布:数据分类汇总步骤文字 编辑:程序博客网 时间:2024/05/20 06:52

查看原题

题意

如果一个数的所有真约数等于它本身,即完全数
如果大于它本身,则abundant
否则deficient
注意输入输出,要求对齐

思路

代码

#include <iostream>#include <math.h>#include <stdio.h>using namespace std;int temp[1000];int isPerfect(int n){    int sum=0;    for(int i=1;i<=n/2;i++){        if(n%i==0){            sum+=i;        }    }    if(sum==n){        return 1;    }    else if(sum>n){        return 2;    }    else{        return 0;    }}int main(int argc, char *argv[]){    int step=0;    while(cin>>temp[++step]){        if(temp[step]==0){            break;        }    }    cout<<"PERFECTION OUTPUT"<<endl;    for(int i=1;i<=step;i++){        if(temp[i]==0){            cout<<"END OF OUTPUT"<<endl;            break;        }        printf("%5d  ",temp[i]);//注意这里        if(isPerfect(temp[i])==1){            cout<<"PERFECT"<<endl;        } else if(isPerfect(temp[i])==2){            cout<<"ABUNDANT"<<endl;        }else{            cout<<"DEFICIENT"<<endl;        }    }    return 0;}
原创粉丝点击