zoukankan      html  css  js  c++  java
  • 2020牛客国庆集训派对day2 J VIRUS OUTBREAK补题(大整数加法、前缀和)

    链接: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.

    输入描述:

    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] = '';
    32 }
    33 
    34 int main() {
    35     char a[495][300], b[495][300], ans[300];
    36     memset(a, 0, sizeof(a));
    37     memset(b, 0, sizeof(b));
    38     a[1][0]  = '0'; a[2][0]  = '1';//a[1][] = 0; a[2] = 1; //i+1 to i+2
    39     for(int i = 3; i <= 490; i++) {
    40         add(a[i-2], a[i-1], ans);
    41         strcpy(a[i], ans);//a[i] = a[i-1] + a[i-2];
    42     }
    43     strcpy(b[1], a[1]);//b[1] = a[1];
    44     for(int i = 2; i <= 490; i++) {
    45         add(a[i], b[i-1], ans);
    46         strcpy(b[i], ans);//b[i] = b[i-1] + a[i];
    47     }
    48     //for(int i = 1; i <= 490; i++) cout << a[i] << " ";
    49     long long n;
    50     while(~scanf("%lld", &n)) {
    51         if(n == -1) break;
    52         if(n == 1) printf("Hour: %llu: 1 cow(s) affected
    ", n);
    53         else {
    54             add("1", b[n-1], ans);
    55             printf("Hour: %llu: %s cow(s) affected
    ", n, ans);
    56         }
    57     }
    58     return 0;
    59 }
    60 
    61 //for(int i = 0; i < n; i++)
    感谢https://www.cnblogs.com/cs-whut/p/11190639.html提供的大整数思路。
  • 相关阅读:
    排名第一、第二的OCR软件
    补码输出
    枚举 与 枚举的应用
    动态构造结构体数组
    c 冒泡排序
    strcpy
    typedef用法
    C 结构体小结
    int 占一个机器字长
    SQL Server创建视图——视图的作用
  • 原文地址:https://www.cnblogs.com/knightoflake/p/13762657.html
Copyright © 2011-2022 走看看