Children’s Queue大数加法及规律

Problem Description
There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?
 
Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)
 
Output
For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.
 
Sample Input
1
2
3
 
Sample Output
1
2
4
 
Author
SmallBeer (CML)
 
Source
杭电ACM集训队训练赛(VIII)
 
Recommend
lcy
 

先写出前面几个来,可以发现规律f[i]=f[i-1]+f[i-2]+f[i-4];注意当1000的时候会爆long long所以要用大数!!!

代码:

#include<stdio.h>
#include<string.h>
char a[1005][1001];
void add(int x,int y){//计算两个数相加,将各个位置的数转化为int型进行计算,然后判断是否大于10,大于10则将z=1,否则赋为0.
 int len_a,len_b;
 len_a=strlen(a[x]);
 len_b=strlen(a[y]);
 int z=0;
 int tx,ty,t,i=0;
 while(i<len_a||z==1){//如果最后z=1则继续循环将1加到末尾
  tx=ty=t=0;
  if(i<len_a)
   tx=a[x][i]-'0';//如果i>=len_a则tx为之前赋的0值,对计算无影响了。
  if(i<len_b)
   ty=a[y][i]-'0';//同上。
  t=tx+ty+z;//计算两个数同一位数的值,如果上两个数的值大于10则加上z的值否则为0,对计算无影响
  if(t>=10){//判断此时的值是否大于10,如果大于10则t-10&&z=1
   t=t-10;
   z=1;
  }
  else//如果此时t不大于10则赋0。
   z=0;
  a[x][i]=t+'0';
  i++;
 }
}
int main()
{
 int n,i;
 a[1][0]='1';
 a[2][0]='2';
 a[3][0]='4';
 a[4][0]='7';
 for(i=5;i<=1000;i++){
  strcpy(a[i],a[i-1]);
  add(i,i-2);//计算a[i-1]+a[i-2].
  add(i,i-4);//计算a[i-1]+a[i-2]+a[i-4].
 }
 while(scanf("%d",&n)!=EOF){
  for(i=strlen(a[n])-1;i>=0;i--)//注意将位置颠倒过来,i=0时是各位,输出时应是最高位。
   printf("%c",a[n][i]);
  printf("\n");
 }
 return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值