ACM: A + B Problem II (两个大整数相加)

Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 1000
//给数组赋值
void arrV(int a[],int len,int p){
	int i;
	for(i=0;i<len;i++){
		a[i]=p;
	}
}

//打印数组中的运算结果
void printRverse(int a[]){
	int len=0,i=0;
	while(a[i]!=-1){
		len++;
		i++;
	}
	for(i=len-1;i>-1;i--){
		printf("%d",a[i]);
	}
}

//两个数组相加
void arrSum(int a[],int b[],int s[],int len_a,int len_b){
	int jin=0,i=0,j=0,temp;
	int len=len_a<=len_b?len_a:len_b;
	for(;i<len;i++){
		temp=a[i]+b[i];
		if(temp<9){
			s[i]=jin==0?temp:temp+1;
			jin=0;
		}else if(temp==9){
			if(jin==0){
				s[i]=temp;
				jin=0;
			}else{
				s[i]=0;
				jin=1;
			}
		}else{
			s[i]=jin==0?temp%10:temp%10+1;
			jin=1;
		}
		//printf("%d=[%d]\n",i,s[i]);
	}
	if(len_a>len_b){
		while(i<len_a){
			if(jin==0){
				s[i]=a[i];
			}else{
				if(a[i]<9){
					s[i]=a[i]+1;
					jin=0;
				}else if(a[i]==9){
					s[i]=0;
					jin=1;
				}
			}
			i++;
		}
		if(jin==1){
			s[i]=1;
		}
	}else{
		while(i<len_b){
			if(jin==0){
				s[i]=b[i];
				jin=0;
			}else{
				if(b[i]<9){
					s[i]=b[i]+1;
					jin=0;
				}else if(b[i]==9){
					s[i]=0;
					jin=1;
				}
			}
			i++;
		}
		if(jin==1){
			s[i]=1;
		}
	}

}

//将读入的字符串转入到整型数组中
void charToArray(char *p,int a[],int *len){
	int i,j=0;
	*len=strlen(p);
	for(i=*len-1;i>-1;i--){
		a[j]=p[i]-48;
		j++;
	}
}

int main(){
	int n,a[MAX],b[MAX],s[MAX],i=1,j=0,len_a,len_b;
	char p[MAX],q[MAX];

	scanf("%d",&n);
	while(n>0){
		arrV(a,MAX,-1);
		arrV(b,MAX,-1);
		arrV(s,MAX,-1);
		scanf("%s %s",p,q);
		charToArray(p,a,&len_a);
		charToArray(q,b,&len_b);
		arrSum(a,b,s,len_a,len_b);
		printf("Case %d:\n",i);
		printf("%s + %s = ",p,q);
		printRverse(s);
		printf("\n");
		if(n>1){
			printf("\n");
		}
		i++;
		n--;
	}

	return 1;
}

  

ACM: A + B Problem II (两个大整数相加),布布扣,bubuko.com

时间: 2024-08-04 14:13:55

ACM: A + B Problem II (两个大整数相加)的相关文章

大整数相加问题

#include <iostream> #include <string> /*project:两个大整数相加 **@author:浅滩 **data:2019.05.15 */ using namespace std; void add(const string &,const string &); int main() {int n; string str1,str2; cin>>str1>>str2; add(str1,str2); }

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

求两个大整数相乘的积,数字长度在127个字符之内。

计算两个大整数相乘的结果,数字不是double类型能表示的,数字长度没有限制(最大127位). 方法一:使用java中的大整数处理库函数,java.math.BigInteger,java.math.BigDecimal; 方法二:不利用库函数,自己实现其功能,可以练习自己对待复杂问题的处理能力,提高编程水平. 此代码中利于两者的结合,利于库函数可以看自己编写的代码的正确性. 1.主函数 import java.math.BigDecimal; import java.util.Scanner;

算法---大整数相加

原文:算法---大整数相加 开通博客开始第一次写发表算法博客.深知一半算法考试都是用C,C++,由于大四开始到今年毕业工作到现在一直从事C#开发,C++用得很少了.链表,指针也只知道一个概念了.用得没以前熟练了.所以后续更新的算法题我都是基于C#语法的.算法主要体现的是解题思路.跟题目一样,本次算法主要实现大数据相加. 解题思路: 1. 将大数据存储到一个链表中,C#中用List<int>来存储,每个节点表示每一位的数字. {1,2,3,4,5} =>12345 和{9,6,5,9,5}

[Java]#从头学Java# Java大整数相加

重操旧业,再温Java,写了个大整数相乘先回顾回顾基本知识.算法.效率什么的都没怎么考虑,就纯粹实现功能而已. 先上代码: 1 package com.tacyeh.common; 2 3 public class MyMath { 4 5 public static String BigNumSum(String... n) { 6 int length = n.length; 7 StringBuilder result = new StringBuilder(); 8 //这里判断其实不需

C++ string 实现大整数相加减

任意两个大整数的加减算法,可自动判断正负号,代码如下: #include <iostream> #include <vector> #include <cstring> #include <algorithm> #include <string> using namespace std; string BigInegerAdd(string s1, string s2) // s1+s2; { int len = s1.size()>s2.

如何实现大整数相加

思路:在程序中列出 "竖式" ,然后逐位相加.究竟是什么样子呢?我们以 426709752318 + 95481253129 为例,来看看大整数相加的详细步骤: 第一步,把整数倒序存储,整数的个位存于数组0下标位置,最高位存于数组长度-1下标位置.之所以倒序存储,更加符合我们从左到右访问数组的习惯. 第二步,创建结果数组,结果数组的最大长度是较大整数的位数+1,原因很明显. 第三步,遍历两个数组,从左到右按照对应下标把元素两两相加,就像小学生计算竖式一样. 例子中,最先相加的是数组A的

实现大整数相加(考虑符号位,可能有负整数) 思维严谨程度!!

//实现大整数相加 //还得考虑符号位,一个比另一个短 #include<iostream> #include <string> #include <cstring> using namespace std; #define maxlen 2001 int a[maxlen]; int b[maxlen]; int len1, len2, i, j; int bigger(int a, int b) { return a>b ? a : b; } void Add