杭电oj1002(c++)

题目分析

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

长整数加法。可以使用数组或链表,我使用的是数组。
为解决输入任意长度整数一步,将数字以字符串形式接受,可以很方便地确认数字长度。再将字符串按位转化为整数存入数组,数组之间也按位相加。

代码

#include
#include
#define N 10000
int i,j;
using namespace std;

void cal(int a[],int b[],int p,int q){
	p=p>q?p:q;
	for(i=0;i=10){
			b[i]-=10;
			b[i+1]+=1;
		}
	}
	if(b[p]==1){
		cout<=0;i--){
		cout<>T;
	int k;
	for(k=1;k<=T;k++){
		int a[N]={},b[N]={};/*一定要放在循环内以初始化数组*/
		cin>>x>>y;
		p=strlen(x);q=strlen(y);
		for(i=0;i

本题涉及字符串转化数组,所以循环范围一定要仔细考虑。

提交时的错误

以下内容转自 百度知道 基友Sue的回答

可以用传递数组元素个数的方法解决即:用两个实参,一个是数组名,一个是数组的长度。

runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。

比如说:

除以零
数组越界:int a[3]; a[10000000]=10
指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10
使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free§; *p=10
数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000]

你可能感兴趣的:(杭电oj1002(c++))