2020牛客国庆集训派对day2 VIRUS OUTBREAK
VIRUS OUTBREAK
文章目錄
- 題目描述
- 輸入描述:
- 輸出描述:
- 題解:
- 代碼:
題目描述
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.
輸入描述:
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: \text { Hour } \mathbf{X}: \mathbf{Y} \text { cow(s) affected } Hour X:Y cow(s) affected , where \mathbf{X}X is the hour, and \mathbf{Y}Y is the total affected cows that need to be euthanized based on the hour given by \mathbf{X}X.
輸入
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題解:
直接看樣例就不難看出是求斐波那契數(shù)列,再一看數(shù)據(jù)范圍,果然是大數(shù)的斐波那契數(shù)列,我拿出珍藏的java版的斐波那契數(shù)列,java對寫大數(shù)加減等這方面有得天獨厚的優(yōu)勢
但是爺就想用c++來寫咋辦?
要知道300的斐波那契數(shù)列是222232244629420445529739893461909967206666939096499764990979600
longlong也存不下,那怎么辦?我們就用數(shù)組來存數(shù),就是一位一位的存,然后我們手寫模擬加法過程,前兩位相加等于第三位,記得考慮進位情況
代碼:
#include <stdio.h> #include <string.h> #include<bits/stdc++.h> using namespace std; int a[1005][1005]; int main() {int T;int n;while(cin>>n){if(n==-1)break;int c;int d=0;memset(a,0,sizeof(a));a[1][0]=1;a[2][0]=1;//第一個和第二個數(shù)要先保存下來 for(int i=3;i<=n;i++)//從第三個數(shù)開始都是等于前兩個數(shù)的和 {c=0;//保存余數(shù) for(int j=0;j<=d;j++){a[i][j]=a[i-1][j]+a[i-2][j]+c;//計算結(jié)果 c=a[i][j]/10;//將其他的數(shù)進位 a[i][j]%=10;//將大于10的數(shù)要余數(shù) }while(c!=0)//最后一位要是大于10,需要進位,并且最高位也需要加1! {a[i][++d]=c%10;c/=10;}}printf("Hour: %d: ",n);for(int i=d;i>=0;i--)//輸出需要求的數(shù)的所有的位所有的值! {printf("%d",a[n][i]);}printf(" cow(s) affected\n");}return 0; }總結(jié)
以上是生活随笔為你收集整理的2020牛客国庆集训派对day2 VIRUS OUTBREAK的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 诺贝尔奖今起揭晓 4位华裔科学家成热门
- 下一篇: 2020牛客国庆集训派对day2 AKU