| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 813 人关注过本帖
标题:请教一c++题目
只看楼主 加入收藏
Super819
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-7
收藏
 问题点数:0 回复次数:2 
请教一c++题目
ACM种有这么一题:

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.

 

Sample Input
2
1 2
112233445566778899 998877665544332211
 

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

我写了这么一个程序:
#include <iostream>
#include <string.h>
using namespace std;

char a[1001],b[1001];

char* add(char *a,char *b){
        int i,j,k = 0,tmp[105],l1 = strlen(a),l2 = strlen(b);
        for (i = l1-1,j = l2-1;i >= 0 && j >= 0;--i,--j)               
                tmp[k++] = a[i]+b[j]-'0'-'0';        
        for (;i >= 0;--i)               
                tmp[k++] = a[i]-'0';        
        for (;j >= 0;--j)               
                tmp[k++] = b[j]-'0';
        
        tmp[k] = 0;        
        for (i = 0;i < k;++i){
                tmp[i+1] += tmp[i]/10;               
                tmp[i] %= 10;        
        }
               
        if (!tmp[k])               
                --k;               
        for (i = 0;i <= k;++i)               
                a[i] = tmp[k-i] + '0';        
        a[k+1] = '\0';         
        
        return a;
}

int main(){        
        char *r;
        int t;
        int i;
        cin>>t;
        if(t>=1&&t<=20)
        {
            for(i=1;i<=t;i++)
            {
                scanf("%s%s",a,b);
                r = add(a,b);
                cout<<"Case "<<i<<":"<<endl;
                cout<<a<<" + "<<b<<" = "<<r<<endl;
                if(i!=t)
                    cout<<endl;
            }
        }
        return 0;
}
但提交时出现了以下错误:
Runtime Error
(ACCESS_VIOLATION
哪位大虾帮我下,不知道还有什么好的方法解决这题,快疯了。。。
搜索更多相关主题的帖子: test first positive problem process 
2008-07-07 23:35
ytunx
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-9
收藏
得分:0 
这是我写的,不知道对你有没有帮助
#include <iostream.h>
#include <string.h>

#define MAX 200

class HugeInt
{    //重载运算符<<
    friend ostream &operator << (ostream &output, HugeInt &h);
    //重载运算符>>
    friend istream &operator >> (istream &input, HugeInt &h);
public:
    HugeInt(char *str = "");
    //重载运算符" = "
    HugeInt & operator = (HugeInt &h);
    //重载运算符" + "
    HugeInt & operator + (HugeInt &h);
public:
    unsigned int num[MAX];
};

HugeInt::HugeInt(char *str)
{
    for (int i=0; i<MAX; i++)
        num[i] = 0;
    //i为数组起始位置
    i = MAX - strlen(str) ;
    for (; i<MAX; i++)
        num[i] = *str++ - '0';
}
///////////////////////////////////////////////////////////
//定义重载函数
//  <<
ostream & operator << (ostream &output, HugeInt &h)
{
    for (int i=0; i<MAX; i++)
        if (h.num[i] != 0)
            break;
    //从第一个不为零的位置开始输出
    for (; i<MAX; i++)
        output<<h.num[i];
    return output;
}
//  >>
istream & operator >> (istream &input, HugeInt &h)
{
    char buff[1000];
    int i, j;
    cin>>buff;
    //计算输入的长度和数组开始位置
    i = MAX - strlen(buff);
    for (j=0; i<MAX; i++, j++)
        h.num[i] = buff[j] - '0';
    return input;
}
//  =
HugeInt & HugeInt::operator = (HugeInt &h)
{
    //检查是否自我赋值
    if (&h == this)
        return *this;
    for (int i=0; i<MAX; i++)
        num[i] = h.num[i];
    return *this;
}
//  +
HugeInt & HugeInt::operator + (HugeInt &h)
{
    static HugeInt sum;
    int n = 0;  //进位
    for (int i=MAX-1; i>0; i--)
    {  //从最后一位开始相加,满10进1(n=1)
        sum.num[i] = (num[i] + h.num[i] + n)%10;
        n = (num[i] + h.num[i] + n)/10;
    }
    //如果最高位相加满10,数据溢出
    if ((num[i] + h.num[i] + n)/10 == 1)
    {
        cerr<<"数据溢出!"<<endl;    
    }
    else
        sum.num[i] = (num[i] + h.num[i] + n)%10;
    return sum;
}
//////////////////////////////////////////////////
//主函数
int main()
{
    HugeInt a[20], b[20];
    HugeInt sum[20];
    int count;
    cin>>count; //case
    for (int i=0; i<count; i++)
    {
        cin>>a[i]>>b[i];
        sum[i] = a[i] + b[i];        
    }
    cout<<endl;
    for (i=0; i<count; i++)
    {
        cout<<"case "<<i+1<<":"<<endl;
        cout<<a[i]<<"  +  "<<b[i]<<"  =  "<<sum[i]<<endl<<endl;    
    }
    return 0;
}
2008-07-09 04:29
Super819
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-7
收藏
得分:0 
嘿嘿 太感谢了
2008-07-10 15:06
快速回复:请教一c++题目
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015138 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved