zoukankan      html  css  js  c++  java
  • hdoj 1002大数加法

    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
    const int MAX = 1010;
    char s1[MAX],s2[MAX];
    int s[MAX];
    int main()
    {
        int n, c, m, count = 1;
        scanf("%d ",&n);
        for(int l=0; l<n; l++)
        {
            memset(s1, '', sizeof(s1));
            memset(s2, '', sizeof(s2));
            memset(s,0,sizeof(s));
            scanf("%s", s1);
            scanf("%s", s2);
            int i = strlen(s1);
            int j = strlen(s2);
            reverse(s1, s1+i);
            reverse(s2, s2+j);
            int k = i>j?i:j;
            if(i>=j)
            {
                c = 0;
                for(m=0; m<j; m++)
                {
    
                    s[m] = s1[m]-'0'+s2[m]-'0'+c;
                    c = s[m]/10;
                    s[m] = s[m]%10;
                }
                for(m=j; m<i; m++)
                {
                    s[m] = s1[m]-'0'+c;
                    c = s[m]/10;
                    s[m] = s[m]%10;
                }
    
            }
            else
            {
                c = 0;
                for(m=0; m<i; m++)
                {
    
                    s[m] = s1[m]-'0'+s2[m]-'0'+c;
                    c = s[m]/10;
                    s[m] = s[m]%10;
                }
                for(m=i; m<j; m++)
                {
                    s[m] = s2[m]-'0'+c;
                    c = s[m]/10;
                    s[m] = s[m]%10;
                }
            }
            if(c)
                s[k] = c;
            else
                k--;
            reverse(s1, s1+i);
            reverse(s2, s2+j);
            cout<<"Case "<<count<<":"<<endl;
            cout<<s1<<" "<<"+"<<" "<<s2<<" "<<"="<<" ";
            for(i=k; i>=0; i--)
            {
                cout<<s[i];
            }
            if(count<n)
            {
                cout<<endl<<endl;
            }
            else
                cout<<endl;
            count++;
        }
    
    
        return 0;
    }
    

    用例:

    n

    s1 s2

    s1 s2

    n为组数,s1 s2为每组相加的数

  • 相关阅读:
    【扫盲】模块 库 框架 包
    AJax和JQ的结合使用
    AJax的三种响应
    使用session存储数据
    Requset作用域
    Cookie实现记住密码代码
    Cookie技术
    请求转发和重定向
    重定向的两次请求分别是????
    servlet
  • 原文地址:https://www.cnblogs.com/ft039x/p/11418032.html
Copyright © 2011-2022 走看看