链接:https://ac.nowcoder.com/acm/contest/7818/J
来源:牛客网
题目描述
The State Veterinary Services Department recently reported an outbreak of a newly found cow disease. All cows found to have affected by the disease have since euthanized because of the risk to the industry. Number of affected cows increased to 21, 34 and reached 55 after eight, nine and ten hours respectively.
You have been assigned by the authority to study the pattern of the outbreak and develop a program to predict the next number of affected cows so that the authorities could prepare and act accordingly.
You have been assigned by the authority to study the pattern of the outbreak and develop a program to predict the next number of affected cows so that the authorities could prepare and act accordingly.
输入描述:
Input will consist of a series of positive integer numbers no greater than 490 each on a separate line represent the hours. The input process will be terminated by a line containing -1.
输出描述:
For each input value, the output contains a line in the format: Hour X:Y cow(s) affected ext { Hour } mathbf{X}: mathbf{Y} ext { cow(s) affected } Hour X:Y cow(s) affected ,
where Xmathbf{X}X is the hour, and Ymathbf{Y}Y is the total affected cows that need to be euthanized based on the hour given by Xmathbf{X}X.
示例1
输入
1 4 6 11 -1
输出
Hour: 1: 1 cow(s) affected Hour: 4: 3 cow(s) affected Hour: 6: 8 cow(s) affected Hour: 11: 89 cow(s) affected
思路
每个数与前一个数的差成斐波那契数列,即fib(n) = fib(n-1) + fib(n-2),因此要先求出差分数组(斐波那契数列),再求出前缀和(即第一个数与第n个数的差)。
但要注意要用字符串模拟大整数,不然会爆(没有做对的原因就是大整数加法出了问题)。
代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define pi acos(-1) 7 //现将a,b串逆序并将每一位储存为整数 8 void add(char a[], char b[], char ans[]) { 9 int i, j, n1, n2, n; 10 int num1[305] = {0}, num2[305] = {0}; 11 n1 = strlen(a); 12 j = 0; 13 for(i = n1 - 1; i >= 0; i--) 14 num1[j++] = a[i] - '0'; 15 n2 = strlen(b); 16 j = 0; 17 for(i = n2 - 1; i >= 0; i--) 18 num2[j++] = b[i] - '0'; 19 n = n1 > n2 ? n1 : n2; 20 for (i = 0;i < n; i++) { 21 num1[i] += num2[i]; 22 if (num1[i] >= 10) { //进位处理 23 num1[i] -= 10; 24 num1[i+1]++; 25 } 26 } 27 j = 0; 28 if(num1[n] != 0) ans[j++] = num1[n] + '0'; //最高位相加后进了位 29 for(i = n-1; i >= 0; i--) 30 ans[j++] = num1[i] + '0'; 31 ans[j] = '