HDU A + B Problem II 大資料

來源:互聯網
上載者:User

標籤:

 1 import static java.lang.System.out; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4  5 public class Main { 6  7     public static void main(String[] args) { 8         BigInteger a, b; 9         Scanner sc = new Scanner(System.in);10         int T = sc.nextInt();11         for (int i = 1; i <= T; i++) {12             a = sc.nextBigInteger();13             b = sc.nextBigInteger();14             out.println("Case " + i + ":");15             out.println(a + " + " + b + " = " + a.add(b));16             if (i < T)17                 out.println();18         }19     }20 }
View Code

java中更加快捷

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #define sc(x) scanf("%d",&x) 6 #define pf(x) printf("%d\n",x) 7 #define PF(x) printf("%d",x) 8 #define CL(x,y) memset(x,y,sizeof(x)) 9 using namespace std;10 const int MAX = 1005;11 char a[MAX], b[MAX];12 int aLen, bLen, n;13 int T, k, i, j;14 void Add(char* a, char* b);15 int main()16 {17     sc(T);18     for(i = 1; i <= T; i++)19     {20         CL(a, 0);21         CL(b, 0);22         cin >> a >> b;23         printf("Case %d:\n", i);24         cout << a << " + " << b << " = ";25         Add(a, b);26         if(i < T) printf("\n");27     }28     return 0;29 }30 void Add(char* a, char* b)31 {32     aLen = strlen(a);33     bLen = strlen(b);34     n = MAX - (aLen > bLen ? aLen : bLen);//設定等位線35     for(j = aLen-1, k = MAX; j >= 0; --j, --k)36     {37         a[k] = a[j]-‘0‘;38         a[j] = 0;39     }40     for(j = bLen-1, k = MAX; j >= 0; --j, --k)41     {42         b[k] = b[j]-‘0‘;43         b[j] = 0;44     }45     for(k = MAX; k >= n; --k)46         a[k] += b[k];47     for(k = MAX; k >= n; --k)48     {49         int temp = a[k]/10;//進位50         a[k-1] += temp;51         a[k] %= 10;52     }53     for(k = n+1; k <= MAX; k++)54 //            cout << a[k] << endl;55         PF(a[k]);56         cout << endl;57 }
View Code

 

HDU A + B Problem II 大資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.