• hdoj 1013


    比较坑的一道题。。。直接做肯定WA

    http://acm.hdu.edu.cn/showproblem.php?pid=1013

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 int main(){
     5     char a[1000];
     6     while(cin>>a){
     7         int len = strlen(a);
     8         long long n =0;
     9         for(int i=0;i<len;i++){
    10             n += a[i]-'0';
    11         }
    12         if(n==0)
    13             break;
    14         while(1){
    15             int sum = 0;
    16             while(n ){
    17                 sum += n%10;
    18                 n = n/10;
    19             }
    20             if(sum>9)//这个地方设置得比较巧妙,直接判断是不是小于9即可。。。。。。我当初使用。。。。下边附。。。
    21                 n = sum;
    22             else{
    23                 cout<<sum<<endl;
    24                 break;
    25             }
    26         }
    27     }
    28   return 0;
    29 }

    首先下边这样做WA 但是方法远远比上边的麻烦。。。。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 bool issingle(int n){
     5     if(n/10==0)
     6         return true;
     7     return false;
     8 }
     9 int getroot(int n){
    10     int sum = 0;
    11     while(n){
    12         sum+=n%10;
    13         n = n/10;
    14     }
    15     return sum;
    16 }
    17 int main()
    18 {
    19     int n;
    20     while(cin>>n&&n){
    21         while(!issingle(n)){
    22             int ss = getroot(n);
    23             if(issingle(ss)){
    24                 cout<<ss<<endl;
    25                 break;
    26             }
    27             n = ss;
    28         }
    29 
    30 
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    c++之类模板
    c++之函数模板
    c++之继承三
    c++之继承二
    c++之继承一
    c++之类类型转换
    c++之运算符重载二
    c++之运算符重载一
    Mahout学习路线路
    数据库分区
  • 原文地址:https://www.cnblogs.com/Bang-cansee/p/3192586.html
Copyright © 2020-2023  润新知