• rwkj 1332 C语言实验四(函数):题目1、数字根


    C语言实验四(函数):题目1、数字根

    时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
    总提交:305            测试通过:185

    描述

     

          正整数的数字根是将数位上的数字求和进行计算而来。如果各位数字之和为一位的整数,那么这个整数就是这个数的数字根;如果之后为多位数,那么重复运用此规则进行计算直至求出一个一位数。例如12,那么它的数字根就为1+2=3;例如39那么它的数字根就为3+9=12,1+2=3,最终为3。

     

    输入

     

         每行输入一个正整数,输入0表示结束。

     

    输出

     

          输出一个一位数。

     

    样例输入

    24
    39
    0

    样例输出

    6
    3

     

     

     

    #include <stdio.h>
    int f(int x)
    {
        int s=0;
        while (x)
        {    s=s+x%10; x=x/10; }
        return s;
    }
    int main()
    {   
        int x;
        while( scanf("%d",&x)==1 && x)
        {   
            while (x>10)
               x=f(x);
            printf("%d
    ", x);
        }
        return 0;
    }
    View Code

     

    #include <stdio.h>
    int f(int x)
    {
        int s=0;
        while (x)
        {    s=s+x%10; x=x/10; }
        return s;
    }
    int main()
    {   
        int x;
        while( scanf("%d",&x)==1 && x)
        {   
            while (x>10)
               x=f(x);
            printf("%d ", x);
        }
        return 0;
    }

     

     

     

     

     

    #include<stdio.h>
    int main()
    {
        int a=0,n;
        while(scanf("%d",&n)!=EOF)
        {
            while(n>9)
            {
                if(n>9)
                {
                    a=n%10;
                    n/=10;
                    n+=a;
                }
            }
                if(n==0)
                    printf("");
                else
                    printf("%d
    ",n);
            
        }
    } 
    
        
    View Code

     

    #include<stdio.h>
    int main()
    {
        int a=0,n;
        while(scanf("%d",&n)!=EOF)
        {
            while(n>9)
            {
                if(n>9)
                {
                    a=n%10;
                    n/=10;
                    n+=a;
                }
            }
                if(n==0)
                    printf("");
                else
                    printf("%d ",n);
           
        }
    }

       

    #include <iostream>
    using namespace std;
    int f(int x)
    {
        int s=0;
        while (x)
        {
            s=s+x%10;
            x=x/10;
        }
        if (s>9)    return f(s);
        else return s;
    
    }
    
     
    
    
    int main(int argc, char *argv[])
    {
        int x,y;
        while (cin>>x)
        {if(x==0)  ;
        else cout<<f(x)<<endl;
        }
    
    } 
    
     
    
     
    View Code

    #include <iostream>
    using namespace std;
    int f(int x)
    {
        int s=0;
        while (x)
        {
            s=s+x%10;
            x=x/10;
        }
        if (s>9)    return f(s);
        else return s;

    }


    int main(int argc, char *argv[])
    {
        int x,y;
        while (cin>>x)
        {if(x==0)  ;
        else cout<<f(x)<<endl;
        }

    }

     

     

  • 相关阅读:
    LOJ6274 数字
    test20200415 看门人 和 WC2010 重建计划
    TJOI2018 Party 和 HDU4352 XHXJ's LIS
    LOJ3228 Tree Depth
    AGC026D Histogram Coloring
    LOJ3277 星座 和 BZOJ2616 PERIODNI
    LOJ2331 某位歌姬的故事 和 CF1327F AND Segments
    AGC024F Simple Subsequence Problem
    LOJ3278 收获
    LOJ3282 治疗计划
  • 原文地址:https://www.cnblogs.com/2014acm/p/3885569.html
Copyright © 2020-2023  润新知