zoukankan      html  css  js  c++  java
  • 数字河

    时限:1000ms 内存限制:10000K  总时限:3000ms

    描述: 数字河中的一个数n 的后继数是n 加上其每位数字的和。例如,12345的后继数是12360,因为12345+1+2+3+4+5=12360。如果数字河的第一个数为k,我们就称此数字河为river k。例如,river 480 代表序列{480, 492, 507, 519, ...},river 483 代表序列{483, 498, 519, ...}。 当两个数字河有相同的元素时,我们称这两个数字河在此元素处相遇。例如,river 480 和river 483 在元素519处相遇。所有数字河都会和river 1, river 3 或river 9 相遇。编程计算给定的数字河最先与以上三条河流中的哪一条相遇,在何元素处相遇?

    输入: 输入文件包括多组测试用例,每个测试用例占一行,以“0”标志文件结束,该行无需处理。 每行给定一个整数 n ,(1<=n<=16384) ,即river n。

    输出: 对于每个测试用例输出两行,第一行为测试用例号,第二行输出“first meets river x at y”。其中,y表示river n 最先遇到的river x中的最小元素值

    (x = 1,3,9)。

    输入样例:

    117

    52

    0

    输出样例:

    Case #1

    first meets river 9 at 117

    Case #2

    first meets river 1 at 107

    #include<stdio.h>
    #include<math.h>
    int a[1000]={0},b[1000]={0},c[1000]={0};
    int num[50]={0},count=0;
    void init();
    void shu(int a[1000]);
    bool search(int num,int a[1000]);
    int main()
    {
        init();
        int j;
        for(j=0;j<count-1;j++)
        {
            while(1)
            {
                 if(search(num[j],a) )  
                 {   printf("Case #%d\nfirst meets river 1 at %d\n",j+1,num[j]);
                     break;
                 }
                 if(search(num[j],b) )  
                 {   printf("Case #%d\nfirst meets river 3 at %d\n",j+1,num[j]);
                     break;
                 }
                 if(search(num[j],c) )  
                 {     printf("Case #%d\nfirst meets river 9 at %d\n",j+1,num[j]);
                     break;
                 }
        
                 int num1=num[j],temp=num[j];
                 while(temp/10!=0)
                 {    num1+=temp%10;
                    temp=temp/10;
                 }
                 num1+=temp;
                 num[j]=num1;    
            }
        }
        return 0;
    }
    
    bool search(int num,int a[1000])
    {
      for(int i=0;i<1000;i++)
          if(num==a[i])
              return true;
      return false;
    }
    void init()
    {
        int i;
        a[0]=1;  b[0]=3;   c[0]=9;
        shu(a);     shu(b);   shu(c);
        /*for(i=0;i<1000;i++)
            printf("%d ",b[i]);*/
        i=0;
        do
        {    scanf("%d",&num[i++]);
            count++;
        }while(num[i-1]!=0);
        /*for(i=0;i<count;i++)
            printf("%d ",num[i]);*/
    }
    void shu(int a[1000])
    {
        for(int i=1;i<1000;i++)
        { 
            a[i]=a[i-1];
            int temp=a[i-1];
            while(temp/10!=0)
            {    a[i]+=temp%10;
            temp=temp/10;
            }
            a[i]+=temp;
        }
    }
  • 相关阅读:
    dhcp服务配置
    配置一台时间服务器
    创建kvm虚拟机
    实现跳板机
    双向同步使用unison
    17、 Shell脚本题:编写个shell脚本将当前目录下大于10K的文件转移到/tmp目录下。
    find 命令
    权限管理:建立一个经理组
    使用sudo命令
    [转]tftp在put上传的时候显示File not found的解决办法
  • 原文地址:https://www.cnblogs.com/IThaitian/p/2584495.html
Copyright © 2011-2022 走看看