zoukankan      html  css  js  c++  java
  • HDOJ 1002 的几种方法

     1 #include <iostream>
    2 #include <string>
    3
    4 usingnamespace std;
    5
    6 string LSum ( string, string );
    7
    8 int main()
    9 {
    10 string s_1, s_2;
    11 int count_inside =0, count_outside;
    12 cin >> count_outside ;
    13 while ( count_inside != count_outside && cin >> s_1 >> s_2 )
    14 {
    15 cout <<"Case "<<++count_inside <<":"<< endl;
    16 cout << s_1 <<" + "<< s_2 <<" = "<< LSum ( s_1, s_2 ) << endl;
    17 if ( count_inside != count_outside ) cout << endl;
    18 }
    19 return0;
    20 }
    21
    22 string LSum ( string A, string B )
    23 {
    24 string::size_type i = A.size(), j = B.size();
    25 int c =0;
    26 string Ans;
    27 while ( i || j )
    28 {
    29 int tmp = c;
    30 if ( i ) tmp += A[--i] -48;
    31 if ( j ) tmp += B[--j] -48;
    32 if ( ( i +1 ) || ( j +1 ) )
    33 Ans += ( tmp %10+48 );
    34 c = tmp /10;
    35 }
    36 if ( c ) Ans += c +48;
    37 string AnsCopy ( Ans );
    38 string::size_type AnsLength = Ans.size();
    39 for ( int k = AnsLength; k !=0; --k )
    40 Ans[AnsLength-k] = AnsCopy[k-1];
    41 return Ans;
    42 }

    这个是我自己的,没有加工,看完string那一部分后写的。

    下面这个是网上找到的,很多个链接都是这个:

    #include <iostream>
    #include <string.h>
    using namespace std;
    void add ( char a[], char b[] )
    {
        char sum[1010] = {' '};
        int flg = 0;
        int temp = 0;
        int len_a = strlen ( a );
        int len_b = strlen ( b );
        int i = len_a;
        int j = len_b;
        for ( ; i > 0; i-- )
        {
            if ( j > 0 )
            {
                temp = a[i-1] + b[j-1] + flg - 96;
                j--;
            }
            else temp = a[i-1] + flg - 48;
            if ( temp >= 10 )
            {
                flg = 1;
            }
            else flg = 0;
            temp = temp % 10;
            sum[i] = temp + 48;
        }
        if ( flg == 1 ) sum[0] = 49;
        i = 0;
        while ( i <= len_a )
        {
            if ( sum[i] != ' ' ) cout << sum[i];
            i++;
        }
        cout << endl;
    }
    int main()
    {
        int N;
        while ( cin >> N )
        {
            for ( int i = 1; i <= N; i++ )
            {
                char a[1000];
                char b[1000];
                cin >> a;
                cin >> b;
                int len_a = strlen ( a );
                int len_b = strlen ( b );
                cout << "Case " << i << ":\n" << a << " + " << b << " = ";
                if ( len_a >= len_b )
                {
                    add ( a, b );
                }
                else add ( b, a );
                if ( i != N ) cout << endl;
            }
        }
        return 0;
    }
    

    我看了这道题的统计,代码最少的只有380B左右,很想看到代码,可惜看不到,也没在网上找到。

    作为一个新手,希望大家能多多指点,谢谢!

  • 相关阅读:
    HDU 5115 Dire Wolf ——(区间DP)
    2016 ICPC 大连网络赛 部分题解
    CodeForces 707D Persistent Bookcase ——(巧妙的dfs)
    HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)
    【Permutations II】cpp
    【Permutations】cpp
    【Subsets II】cpp
    【Subsets】cpp
    【Search a 2D Matrix】cpp
    【Search Insert Position 】cpp
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2109390.html
Copyright © 2011-2022 走看看