2018年1月18日 星期四

UVA Q10922 - 2 the 9s

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
char a[1001];
int b[1001],c[1001],bp,cp,d;
while(cin>>a){
    if(a[0]=='0'){
        break;
    }
    for(int x=0;x<strlen(a);x++){
        b[x]=a[x]-48;
    }
    bp=strlen(a);
    d=0;
    if(bp==1){
        d++;
    }
    while(bp>1){
        memset(c,0,sizeof(c));
        cp=1;
        for(int x=0;x<bp;x++){
            c[0]=c[0]+b[x];
            for(int y=0;y<cp;y++){
                if(c[y]>=10){
                    cp++;
                    c[y+1]=c[y+1]+(c[y]/10);
                    c[y]=c[y]%10;
                }
            }
        }
        memset(b,0,sizeof(b));
        for(int x=0;x<cp;x++){
            b[x]=c[x];
        }
        bp=cp;
        d++;
    }
    if(b[0]%9==0){
        cout<<a<<" is a multiple of 9 and has 9-degree "<<d<<".";
    }else{
        cout<<a<<" is not a multiple of 9.";
    }
    cout<<endl;
}


system("pause");
return 0;
}

沒有留言:

張貼留言