Winter-1-C A + B II 解题报告及测试数据

Time Limit:1000MS

Memory Limit:32768KB

Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2

1 2

112233445566778899 998877665544332211

Sample Output

Case 1:

1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

以下是代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#include <iostream>

#include <cstring>

#include <cstdio>

using namespace std;

char a[1000],b[1000];

int c[1100],n;

void cal(char a[],char b[],int r)

{

    int i = strlen(a),j = strlen(b);

    int k=0,t;

    i--;j--;

    memset(c,0,sizeof(c));

    while(i>=0 || j >=0){

        if(i>=0 && j>=0)t=a[i]+b[j]+c[k]-‘0‘-‘0‘;

        else if (i<0)t=b[j]+c[k]-‘0‘;

        else t=a[i]+c[k]-‘0‘;

        if(t>=10){

            c[k++]=t%10;c[k]+=1;

        }else c[k++]=t;

        i--;j--;

    }

    while(c[k]==0)k--;

    printf("Case %d:\n%s + %s = ",r,a,b);

    for(i=k;i>=0;i--)printf("%d",c[i]);

    printf("\n");

    if(r!=n)printf("\n");

}

int main(){

    int len1,len2;

    cin >> n;

    for(int i=0;i<n;i++){

        scanf("%s%s",a,b);

        cal(a,b,i+1);

    }

}

时间: 2024-10-13 16:09:18

Winter-1-C A + B II 解题报告及测试数据的相关文章

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

Pascal&#39;s Triangle &amp; II 解题报告

杨辉三角,分别求前n行和第n行. [求杨辉三角前n行] Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 基础题,直接看代码,注意边界. public class Solution { public List<List<Integer>&g

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of