hdu1002

题目意思:计算两个大整数的和

http://acm.hdu.edu.cn/showproblem.php?pid=1002

AC代码:

#include<iostream>

#include<string>

using namespace std;

string add(string s1,string s2){//字符串模拟大数加法,

string s;

int len1,len2;

len1=s1.size()-1; len2=s2.size()-1;

int i=0,flag=0;

while(len1>-1&&len2>-1){

int sum=flag+(s1[len1--]-‘0‘)+(s2[len2--]-‘0‘);

s+=char ((sum)%10+‘0‘);

flag=sum/10;

}

while(len1>-1){

int sum=flag+(s1[len1--]-‘0‘);

s+=char ((sum)%10+‘0‘);

flag=sum/10;

}

while(len2>-1){

int sum=flag+(s2[len2--]-‘0‘);

s+=char ((sum)%10+‘0‘);

flag=sum/10;

}

if(flag) s+=char (‘0‘+flag);

//cout<<s<<endl;

for(int i=0;i<s.size()/2;i++){

char c=s[i];

s[i]=s[s.size()-i-1];

s[s.size()-i-1]=c;

}

return s;

}

int main()

{

int t,k=0;

string a,b;

cin>>t;

while(t--){

cin>>a>>b;

cout<<"Case "<<++k<<":"<<endl;

cout<<a<<" + "<<b<<" = "<<add(a,b)<<endl;

if(t) cout<<endl;

}

return 0;

}

时间: 2024-08-01 22:45:33

hdu1002的相关文章

hdu1000,hdu1001,hdu1002,hdu1003

hdu1000 仅仅是为了纪念 1 #include <cstdio> 2 int main() 3 { 4 int a,b; 5 while (scanf("%d%d",&a,&b)!=EOF) 6 { 7 printf("%d\n",a+b); 8 } 9 return 0; 10 } hdu1001 题目说n*(n+1)/2 不不会爆 但是没和你说n*(n+1)不会爆 用下面这个小方法解决  见代码 #include <ios

HDU1002 大数相加

1 #include <iostream> 2 #include <iostream> 3 #include <iomanip> 4 #include<string> 5 #include<cstring> 6 using namespace std; 7 8 string A , B; 9 char a[1010]; 10 char b[1010]; 11 char result[1010]; 12 13 int max(int a,int b

Hdu1002 大数加法

#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>using namespace std;const int MAX = 1010;char s1[MAX],s2[MAX];int s[MAX];int main(){ int n, c, m, count = 1; scanf("%d ",&n); for(int l=0; l&

[HDU1002] 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. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

HDU-1002 A + B Problem II

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, eac

hdu1002 大数相加问题

这个题对于 几个月前的我简直是噩梦  好在磕磕绊绊终于写出来了 由于自己的问题  还被巨巨嘲讽了 #include<stdio.h>               #include<string.h>               int main()               {               char a[10001], b[10001],c[10001];              int len1,len2;              int i,j=1,n,p

hdu1002 A + B Problem II(大数题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 230247    Accepted Submission(s): 44185 Problem Description I have a very sim

A——大整数加法(HDU1002)

题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe 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 co

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. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases.