zoukankan      html  css  js  c++  java
  • HDU 6225 Little Boxes

    Little Boxes

      Little boxes on the hillside. 
      Little boxes made of ticky-tacky. 
      Little boxes. 
      Little boxes. 
      Little boxes all the same. 
      There are a green boxes, and b pink boxes. 
      And c blue boxes and d yellow boxes. 
      And they are all made out of ticky-tacky. 
      And they all look just the same. 

    Input

      The input has several test cases. The first line contains the integer t (1 ≤ t ≤ 10) which is the total number of test cases. 
      For each test case, a line contains four non-negative integers a, b, c and d where a, b, c, d ≤ 2^62, indicating the numbers of green boxes, pink boxes, blue boxes and yellow boxes. 
    Output

      For each test case, output a line with the total number of boxes. 
    Sample Input

    4
    1 2 3 4
    0 0 0 0
    1 0 0 0
    111 222 333 404

    Sample Output

    10
    0
    1
    1070

    解题思路:
      测试数量t,每个测试给出四个巨大的数a,b,c,d要求计算他们的和,由于数的长度过大这里采用字符串模拟加法(好像ull也可以)。

      小学我们学过一种极为好用的方法——竖式计算

    对于两个数字,将它们记录为字符串,从个位开始按位计算,记录记录完后的结果与进位,计算下一位时将进位加上即可。

    AC代码

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5+10;
     4 struct BigNum{  //记录大数
     5     int num[maxn];
     6     int len;    //长度
     7     BigNum(){
     8         memset(num, 0, sizeof(num));
     9         len = 0;
    10     }
    11 };
    12 BigNum change(string temp){ //将字符串转换为大数类型
    13     //为了方便计算将字符串倒过来储存
    14     BigNum a;
    15     a.len = temp.size();
    16     for(int i = 0; i < a.len; i++)  //逆序储存字符串
    17         a.num[i] = temp[a.len - i - 1] - '0';
    18     return a;
    19 }
    20 BigNum add(BigNum a, BigNum b){
    21     BigNum c;
    22     int carry = 0;//进位
    23     for(int i = 0; i < a.len || i < b.len; i++){//以较长的长度为界限
    24         int temp = a.num[i] + b.num[i] + carry;//两个位置相加后加上进位
    25         c.num[c.len++] = temp % 10; //记录该位结果
    26         carry = temp / 10;  //记录进位
    27     }
    28     if(carry != 0)  //记录首位进位
    29         c.num[c.len++] = carry;
    30     return c;
    31 }
    32 int main()
    33 {
    34     int t; 
    35     while(scanf("%d", &t) != EOF){ //输入测试数量
    36         while(t--){
    37             string temp1;
    38             BigNum sum, temp;
    39             for(int i = 0; i < 4; i++){
    40                 cin >> temp1;    
    41                 temp = change(temp1);   //输入大数并记录为BigNum型
    42                 sum = add(sum, temp);
    43                 //计算和
    44             }
    45             for(int i = sum.len - 1; i >= 0; i--)
    46                 printf("%d",sum.num[i]);
    47             printf("
    ");
    48         }
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    hibernate一对多查询
    hibernate关联关系查询
    Cookie&&session
    JSP&&EL&&JSTL
    servlet下的request&&response
    servlet
    mysql命令
    html小结
    RabbitMQ初步学习和使用
    爬虫简单案例
  • 原文地址:https://www.cnblogs.com/suvvm/p/10034196.html
Copyright © 2011-2022 走看看