码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_1041(Computer Transformation) 大数加法模板+找规律

时间:2017-08-14 10:11:33      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:tom   source   desc   rms   amp   return   code   while   ons   

Computer Transformation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8367    Accepted Submission(s): 3139


Problem Description
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on. 

How many pairs of consequitive zeroes will appear in the sequence after n steps? 
 

 

Input
Every input line contains one natural number n (0 < n ≤1000).
 

 

Output
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
 

 

Sample Input
2 3
 

 

Sample Output
1 1
 

 

Source
 

规律:数串的右边一般是上一个数串,数串的左半边是上两个数串+上一个数串的左半区

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 #define ll long long
 8 const int N = 1010;
 9 struct Node{
10     string sum;
11     string leftsum;
12     int left,right;//zuo ban qu
13 }node[N];
14 
15 string add(string s1,string s2){ //大数 s1 + s2
16        if(s1.length()<s2.length()){
17             string temp=s1;
18             s1=s2;
19             s2=temp;
20        }
21        for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--){
22             s1[i]=char(s1[i]+( j>=0 ? s2[j]-0 : 0));
23             if(s1[i]-0>=10) {
24                 s1[i]=char( (s1[i]-0)%10+0 );
25                 if(i) s1[i-1]++;
26                 else  s1="1"+s1;
27             }
28        }
29        return s1;
30 }
31 
32 void init()
33 {
34    /* for(int i = 0; i < N; i++){
35         node[i].sum ="0";
36         node[i].leftsum = "0";
37     }*/
38     node[1].sum = "0", node[1].left = 0, node[1].right = 0, node[1].leftsum = "0";
39     node[2].sum = "1", node[2].left = 1, node[2].right = 0, node[2].leftsum = "0";
40     for(int i = 3; i < N; i++)
41     {
42         node[i].leftsum = add(node[i-2].sum,node[i-1].leftsum);
43         node[i].left = node[i-2].left;
44         node[i].right = node[i-1].right;
45         node[i].sum=add(node[i-1].sum,node[i].leftsum);
46         if(node[i].right==node[i-1].left&&node[i].right == 0)  node[i].sum = add(node[i].sum,"1");
47     }
48 }
49 int main()
50 {
51     int n;
52     init();
53     while(~scanf("%d",&n)){
54        // printf("%s\n",node[n].sum);
55        cout<<node[n].sum<<endl;
56     }
57     return 0;
58 }

 

 大数加法模板

 1 string add(string s1,string s2){ //大数 s1 + s2
 2        if(s1.length()<s2.length()){
 3             string temp=s1;
 4             s1=s2;
 5             s2=temp;
 6        }
 7        for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--){
 8             s1[i]=char(s1[i]+( j>=0 ? s2[j]-0 : 0));
 9             if(s1[i]-0>=10) {
10                 s1[i]=char( (s1[i]-0)%10+0 );
11                 if(i) s1[i-1]++;
12                 else  s1="1"+s1;
13             }
14        }
15        return s1;
16 }

 

hdu_1041(Computer Transformation) 大数加法模板+找规律

标签:tom   source   desc   rms   amp   return   code   while   ons   

原文地址:http://www.cnblogs.com/shanyr/p/7355911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!