您的位置:首页 > 其它

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

2017-08-14 08:38 351 查看

Computer Transformation

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


[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
Every input line contains one natural number n (0 < n ≤1000).

[align=left]Output[/align]
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.

[align=left]Sample Input[/align]

2
3

[align=left]Sample Output[/align]

1
1

[align=left]Source[/align]
Southeastern Europe 2005

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

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
;
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
.sum);
55        cout<<node
.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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: