• HDU 1005 Number Sequence


    方法一

    #include<stdio.h>
    int main
    ()
    {
         int a,b,n;
       
    while
    (scanf("%d %d %d",&a,&b,&n)!=EOF&&(a+b+n)!=0
    )
    {
     
    int
    f1=1,f2=1,f3=1,t,r[8][8]={0},i,flag=0,x;
           
      for
    (i=3;i<=n;i
    ++)
      {
          f3=(a*f2+b*f1)%7;      
            r[f1][f2]=i;           
       f1=f2; f2=f3;
               
        if
    (r[f1][f2]&&flag==0
    )
        {
                  
          t=i-r[f1][f2]+1;              
            i=r[f1][f2]-3;      
         
       n=(n-i)%t+i;
             
          if
    (n==i) n=t+i;
                    
         if
    (n==(i+1)) {f3=f1;break;}
      
              if
    (n==(i+2)) {f3=f2;break;}             
              i+=2;         
             flag=1
    ;            
           }  
              
      }
            
         printf("%d\n",f3
    );    
      }
        
      getchar();
      getchar
    ();
    }

    方法二

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int a,b,n,i;
        while(1)
        {
            scanf("%d%d%d",&a,&b,&n);
       if(a==0&&b==0&&n==0) break;
       int f[1009];
       f[1]=1;f[2]=1;
       for(i=3;i<=1008;i++)
       {
       
        f[i]=(a*f[i-1]+b*f[i-2])%7;
       
       
       }
       printf("%d\n",f[(n-1)%1008+1]);
    }
    return 0;
    }

  • 相关阅读:
    Python3---filter,map,reduce
    老男孩-day2作业-计算器
    Python 简易购物系统--老男孩作业
    老男孩-day1作业二
    老男孩-day1作业一
    Axure8.0图片旋转注意
    CentOS6.5部署KVM及实现在线迁移
    CentOS 6.5 安装部署iSCSi共享存储
    CentOS 6.5 安装部署KVM
    CentOS6.5部署L2TP over IPSec
  • 原文地址:https://www.cnblogs.com/zxj015/p/2740303.html
Copyright © 2020-2023  润新知