HDU ACM 1002 A + B Problem II

方法一

#include <iostream>
#include <string>
//#include <sstream>
//#include <math.h>
using namespace std;
void main()
{
    int T=0;
    cin>>T;
    for(int n=1; n<=T; n++)
    {
        string a;
        string b;
        cin>>a>>b;
        int i=a.length()-1;
        int j=b.length()-1;
        string result="";
        int carry=0;
        while(i>=0||j>=0||carry==1)
        {
            int sum=0;
            int a1=(i>=0)?(a[i--]-'0'):0;
            int b1=(j>=0)?(b[j--]-'0'):0;
            sum=(a1+b1+carry)%10;
            carry=(a1+b1+carry)/10;
            result=(char)(sum+'0')+result;
        }
        cout<<"Case "<<n<<':'<<endl;
        cout<<a<<" + "<<b<<" = "<<result<<endl;
        if(n!=T)
            cout<<endl;
    }
}

方法二

学到知识点
  • char*p=NULL; cin>>p;错误:必须给p分配空间或使用c++中字符串变量
  • strlen()用法:以结束符\0判断长度,因此当字串中有ascii值为0,不会计入
  • acm中对最后换行符有要求,注意:例如最后一行只输出一行
// 1002Problem.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
//change input str to corresponding ascii  values
void char2num(char* str)
{
    int N=strlen(str);
    for(int i=0; i<N; i++)
        *(str+i)=*(str+i)-'0';
}
//return the char num of pstr3. 
int add_str(char* pstr1, int len1, char* pstr2, int len2, char* pstr3)
{
    int N1=len1;
    int N2=len2;
    //strlen need ending char('/0') to caculate length
    int ncount=0;   //pstr3 counter,get real num chars.
    char flag=0;    //carry flag.
    while(N1>0||N2>0)
    {
        char sum=0;
        //if one of char array ends,make it zero.
        if(N1==0) sum=*(pstr2+N2-1)+flag;
        else
            if(N2==0) sum=*(pstr1+N1-1)+flag;
            else
                sum=*(pstr1+N1-1)+*(pstr2+N2-1)+flag;
        flag=sum/10;
        sum %=10;
        *(pstr3+ncount)=sum;
        if(N1>0) N1--;
        if(N2>0) N2--;
        ncount++;
        //the last time of addtion, save flag if sum>9
        if(N1==0&&N2==0)
        {
            if(flag==1)
            {
                *(pstr3+ncount)=1;
                ncount++;
            }
        }
    }
    return ncount;
}
//output result ascii values of chars
void outputchar(char* pstr, int len)
{
    int N=len;
    int nonzeroflag=0;
    for(int i=0; i<N; i++)
    {
        if((int)(*(pstr+i))!=0)
        {
            cout<<(int)(*(pstr+i));
            nonzeroflag=1;
        }
        if((nonzeroflag==1)&&((int)(*(pstr+i))==0))
            cout<<(int)(*(pstr+i));
    }
    if(nonzeroflag==0)
        cout<<0;
}
//sovle such input case: 001 +009...
void output_result(char* pstr, int len)
{
    int N=len;
    int nonzeroflag=0;
    for(int i=N-1; i>=0; i--)
    {
        if((int)(*(pstr+i))!=0)
        {
            cout<<(int)(*(pstr+i));
            nonzeroflag=1;
        }
        if((nonzeroflag==1)&&((int)(*(pstr+i))==0))
            cout<<(int)(*(pstr+i));
    }
    if(nonzeroflag==0)
        cout<<0;
}
int main()

{
    int T=0;
    cin>>T;
    char* strA=(char*)malloc(10000);        //get A
    char* strB=(char*)malloc(10000);    //get B
    char* strC=(char*)malloc(10000);    //get sum
    for(int i=1; i<=T; i++)
    {
        cin>>strA;
        cin>>strB;
        int len1=strlen(strA);
        int len2=strlen(strB);
        char2num(strA); //after this some char's ascii=0
        char2num(strB); //strlen() not count it if ascii value=0
        int strC_len=add_str(strA, len1, strB, len2, strC);
        cout<<"Case "<<i<<':'<<endl;
        outputchar(strA, len1);
        cout<<" + ";
        outputchar(strB, len2);
        cout<<" = ";
        output_result(strC, strC_len);
        if(i!=T)
            cout<<endl<<endl;
        else    cout<<endl; //last case.
    }
    free(strA);
    free(strB);
    free(strC);
    return 0;
}

题目链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值