1. 程式人生 > >A+B Problem II——大數定理

A+B Problem II——大數定理

A+B Problem II

時間限制:3000 ms  |  記憶體限制:65535 KB 難度:3
描述

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

A,B must be positive.

輸入
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.
輸出
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.
樣例輸入
2
1 2
112233445566778899 998877665544332211
樣例輸出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
這道題簡單一看只是大數定理,但其實比較坑爹的是  輸入00008+2應該輸出10,需要過濾前導0,這道題如果不是前導0我只用了半小時就做對了,還去拉了個大便,結果看討論的說有前導零,又費了一個小時,唉!!
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int i,j,y,n,k,h,p,lena,lenb;
int a[1000],b[1000],sum[1000],f[1000];
int main()
{
	string a1,b1;
	cin>>n;
	for(y=1;y<=n;y++)
	{
		cin>>a1>>b1;
		lena=a1.length();
		lenb=b1.length();
		for(i=0;i<1000;i++)
		{
			a[i]=0;b[i]=0;f[i]=0;
		}
		for(i=lena-1;i>=0;i--)
		    a[lena-1-i]=a1[i]-'0';
		for(i=lenb-1;i>=0;i--)
		    b[lenb-1-i]=b1[i]-'0';
	    k=0;
		for(i=0;i<lenb||i<lena;i++)
		{
			h=a[i]+b[i]+k;
			f[i]=h%10;
			k=h/10;
		}
		if(k!=0)
		    f[i++]=k; 
		cout<<"Case "<<y<<":"<<endl<< a1 <<" + "<< b1 <<" = ";
		p=0;
		for(j=i-1;j>=0;j--)
		{
		 	if(p==0&&f[j]==0)
		 	{
		 	    continue;
		 	}
		 	else
		 	{
		 	    p=1;
		 	    cout<<f[j];
		 	}
		}
		cout<<endl;
	}
	return 0;
}

相關推薦

A+B Problem II——大數定理

A+B Problem II 時間限制:3000 ms  |  記憶體限制:65535 KB 難度:3 描述 I have a very simple problem for you. Giv

1002 1002 A + B Problem II大數相加

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 317528    Acce

HDU 1002 A + B Problem II | 大數入門 (詳細 c/c++程式碼解析)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 434632    Accepted Subm

HPU 1002 A + B Problem II大數

lar 保存 memory positive test size mod ack auth A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327

題解報告:hdu 1002 A + B Problem II大數加法)

return 大數類 class family HERE contains urn integer ons Problem Description I have a very simple problem for you. Given two integers A and

HDU1002 A + B Problem II大數

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission

A + B Problem II JAVA大數水過

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

1001 A + B Problem II(C語言 大數加法模板)

#include<stdio.h> int len(char *a)//一個判斷字串長度的函式與strlen一樣 { int i=0; while(*a++!='\0') { i++; } return i; } /*************

杭電 HDU 1002 A + B Problem II Java 大數的初級用法

<a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=1002">點選開啟連結</a> import

hdoj 1002 A + B Problem II大數A+B

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 308540    Accep

HDU 1002 A + B Problem II(兩個大數相加)

Problem 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. In

HDOJ1002-A + B Problem II(高精加)

change test case logs names div lar struct () har Problem Description I have a very simple problem for you. Given two integers A and B,

Week 1 # A A + B Problem II

res lines osi 可能 c代碼 turn contains inpu mean 原題描述: A - A + B Problem II I have a very simple problem for you. Given two integers A and B,

HDU 1002 B - A + B Problem II

scan ora include color content can 今天 time ons A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (

A + B Problem II

cee ane 代碼 () each space ces put follow Problem Description I have a very simple problem for you. Given two integers A and B, your job is

(string高精度)A + B Problem II hdu1002

ext ring stream turn out bsp bit ons OS A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe

hduoj 1002 A + B Problem II

hdu cin http short ins line include RoCE problem 原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 題目描述如下: A + B Problem II Time Limit

洛谷【P2005】A/B Problem II

.html pan emc .com void const strlen uri ring 題目傳送門:https://www.luogu.org/problemnew/show/P2005 高精除低精:https://www.cnblogs.com/AKMer/p/972

水題 第四站 HDU A + B Problem II

一道很簡單的題目讓我做得噁心無比,大數問題,不熟悉java,還是用C++寫吧。顯然需要用到字串,然後再將字串中的數字轉化為整數型別進行加法運算。去年做類似題目的時候還只知其然不知其所以然,現在明白了為什麼不能直接讀入陣列。 然而時隔一年也忘記了思路,加上當時沒有總結過,只是仿照程式碼敲

HDU1002 A + B Problem II 解題報告

目錄 題目資訊 Problem Description Input Output Sample Input Sample Output 題解 思路 注意事項 Code https://blog.csdn.