zoukankan      html  css  js  c++  java
  • 1002

    大致的思想:把数用字符串数组保存,然后转换到int型数组,最后完成加法。

    #include <iostream>
    using namespace std;
    
    void add(const char *str1,const char *str2)
    {
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        int result[1000];
        int a1[1000];
        int a2[1000];
        memset(result,0,sizeof(result));
        memset(a1,0,sizeof(a1));
        memset(a2,0,sizeof(a2));
        int i,j;
        for(i=len1-1;i>=0;--i)
            a1[i] = str1[i] - '0';
        for(j=len2-1;j>=0;--j)
            a2[j] = str2[j] - '0';
        int k=0;
        for(i=len1-1,j=len2-1;i>=0&&j>=0;--i,--j,++k)
        {
            int temp  = result[k] + a1[i] + a2[j]; // 注意点一
            result[k] = temp%10;
            result[k+1] =  temp/10;
        }
        if(i>=0&&j<0)
        {
            for(i;i>=0;--i,++k)
            {
                int temp = result[k] + a1[i];
                result[k] = temp%10;
                result[k+1] = temp/10;
            }
        }
        if(j>=0&&i<0)
        {
            for(j;j>=0;--j,++k)
            {
                int temp = result[k] + a2[j];
                result[k] = temp%10;
                result[k+1] = temp/10;
            }
        }
        while(result[k]==0&&k>0)  //注意点二
            k = k-1;
        for(i=k;i>=0;--i)
            cout << result[i];
        cout <<endl;
    }
    int main()
    {
        int num;
        char str1[1000],str2[1000];
        int ci = 1;
        cin >> num;
        while(ci<=num)
        {
            cin >> str1 >> str2;
            if(ci!=1) cout<<endl;
            cout<<"Case "<<ci++<<":"<<endl; //格式注意
            cout << str1 <<" + "<<str2<<" = ";
            add(str1,str2);    
            
        }
        return 0;
    }
  • 相关阅读:
    创建包含前后255天所有天数的视图。
    VC获取主机名和主机信息
    在PowerDesigner增加unique约束
    差集的几种计算方法
    动态列的处理(统计)。
    一个查询语句各个部分的执行顺序
    IDC机房跳线
    软件下载链接
    IDC装机检查思路
    运维工程师之IDC系列
  • 原文地址:https://www.cnblogs.com/xuxu8511/p/2446231.html
Copyright © 2011-2022 走看看