hdu 1002 A+B

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

复习一下大数,5分钟秒过。。。

 1 #include<iostream>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<stdlib.h>
 5 #include<stdio.h>
 6 using namespace std;
 7 const int M=1005;
 8 void xsone(int a[],int b[],int lenz)
 9 {
10     for(int i=0;i<lenz;i++)
11     a[i]=a[i]+b[i];
12     int temp=0;
13     for(int i=0;i<lenz+1;i++)
14     {
15         a[i]+=temp;
16         temp=a[i]/10;
17         a[i]%=10;
18     }
19     int i;
20     for(i=lenz+1;i>=0;i--)
21     if(a[i]!=0)
22     break;
23     for(;i>=0;i--)
24     printf("%d",a[i]);
25     printf("\n");
26 }
27 int main()
28 {
29     //freopen("in.txt","r",stdin);
30     int n;
31     scanf("%d",&n);
32     for(int i=1;i<=n;i++)
33     {
34         char sa[M],sb[M];
35         memset(sa,0,sizeof(sa));
36         memset(sb,0,sizeof(sb));
37         scanf("%s %s",sa,sb);
38         int lena = strlen(sa);
39         int lenb = strlen(sb);
40         int lenz = lena>lenb ?lena :lenb;
41
42         int a[M],b[M];
43         memset(a,0,sizeof(a));
44         memset(b,0,sizeof(b));
45         int j;
46         for(j=0;j<lena;j++)
47         a[lena-1-j]=sa[j]-‘0‘;
48         for(j=0;j<lenb;j++)
49         b[lenb-1-j]=sb[j]-‘0‘;
50         printf("Case %d:\n",i);
51         printf("%s + %s = ",sa,sb);
52         xsone(a,b,lenz);
53         if(i!=n)
54         printf("\n");
55     }
56     return 0;
57 }
时间: 2024-10-18 00:10:44

hdu 1002 A+B的相关文章

HDU 1002 A - A + B Problem II (大数问题)

原题代号:HDU 1002 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 原题描述: 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 int

HDU 1002 A + B Problem II(两个大数相加)

详细题目点击:http://acm.hdu.edu.cn/showproblem.php?pid=1002 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)

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. 1 while (a) //将每位数字取出来,取完为止 2 { 3 num1[i]=a%10; //将每一个各位取出存在数组里面,实现了将数字反转 4 i++; //数组的变化 5 a/=10; 6 } 趁热打铁 例题:hdu 4554 叛逆的小明 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid

hdu 1002.A + B Problem II 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define max 1010 5 char a[max], b[max]; 6 7 int main() 8 { 9 int i, k, s, c, T, len1, len2; 10 scanf("%d&quo

hdu 1002 Java 大数 加法

http://acm.hdu.edu.cn/showproblem.php?pid=1002 PE   因为最后一个CASE不需要输出空行 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner in = new Scanner(System.in); int ncase; ncase = in.nextIn

hdu 1002大数(Java)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 230395    Accepted Submission(s): 44208 Problem Description I have a very simple problem for you. Given two integers A and B, you

hdu 1002 A + B Problem II(大数模拟加法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目要求:输入t 为测试组数,然后输入两个正数 a,b求和: [思路] 首先,用字符串读入a,b,再将a,b 逆序存到数组中,然后对应位相加就行了,长度小的数要注意补零 倒过来做可以让进位变得方便 ,如果对应位的和大于等于10,就把余数储存在当前位置,后一位加1: [代码] #include <iostream> #include <string> #include <cs

hdu 1002 A + B Problem II(大正整数相加)

代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; char s1[10000],s2[10000],s3[10000]; int main() { int t; scanf("%d",&t); int tt=0; while(t--) { ++tt; if(tt!=1) printf("\n"

HDU 1002 A + B Problem II(大整数相加)

A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 c