日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

杭电ACM刷题(1):1002,A + B Problem II

發布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 杭电ACM刷题(1):1002,A + B Problem II 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近忙于考試復習,沒有多少可供自己安排的時間,所以我利用復習之余的空閑時間去刷刷杭電acm的題目,也當對自己編程能力的鍛煉吧。

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

程序思路:

  • 使用scanf函數以字符串格式接收兩個數,分別存在兩個數組中,數組中每一個成員對應數的一位(0-9);
  • 由于數組中存放的是每個數字對應的ASCII碼,所以將其減去0得到十進制的數值;
  • 對比連個數組的長度,如果長度不等,就補0,使其在做加法時位數能夠對齊;考慮到有可能兩個數的最高位相加后有進位,所以兩個數組都再額外多補充一位的0;
  • 隨后可以加一個for循環,將兩個數組的所有對應位相加,對于每一位,如果和大于等于10,就進位,即下一位加1,當前為減10;
  • 輸出得到的結果。
  • 程序:

    #include "stdio.h" #include "string.h"#pragma warning(disable:4996)int main() {char a_str[1000] = { 0 }, b_str[1000] = {0};int a_num[1000], b_num[1000];int a_len, b_len, max_len;int n;int i, j, k;int temp;scanf("%d", &n);if (n < 1 || n>20)return -1;for (i = 1;i <= n;i++){//步驟1scanf("%s%s", a_str, b_str);a_len = strlen(a_str);b_len = strlen(b_str);//步驟2temp = 0;for (j = a_len - 1;j >= 0;j--){a_num[temp++] = a_str[j] - '0';}temp = 0;for (k = b_len - 1;k >= 0;k--){b_num[temp++] = b_str[k] - '0';}//步驟3if (a_len > b_len){for (j = b_len;j <= a_len;j++){b_num[j] = 0;}a_num[a_len] = 0;}else if (a_len < b_len){for (j = a_len;j <= b_len;j++){a_num[j] = 0;}b_num[b_len] = 0;}else{a_num[a_len] = 0;b_num[b_len] = 0;}max_len = (a_len >= b_len) ? a_len : b_len;//步驟4for (j = 0;j <= max_len;j++){ a_num[j] += b_num[j];if (a_num[j] >= 10){a_num[j] -= 10;a_num[j + 1] += 1;}}//步驟5printf("Case %d:\n", i);printf("%s + %s = ", a_str, b_str);if (a_num[max_len] == 0){for (j = max_len - 1;j >= 0;j--)printf("%d", a_num[j]);}else{for (j = max_len;j >= 0;j--)printf("%d", a_num[j]);}if (i != n)printf("\n\n");elseprintf("\n");}return 0; }

    備注:
    由于我是在visual studio 2015環境中調試這段程序的,在使用“scanf”函數時會報錯。因為在microsoft的Visual Studio進行編譯時,會提示“scanf”函數是不安全的,建議使用“scanf_s”函數來代替“scanf”。“scanf_s”函數在使用時,如果需要輸入字符串數組,還需要指定邊界,即數組的長度。一般情況下使用“scanf_s”函數就能替代“scanf”函數了,但是這題中卻不行,因為我們也無法確定輸入數組的長度。所以得出的結論就是,繼續使用“scanf”函數,在程序加入這段代碼:

    #pragma warning(disable:4996)

    這樣就可以照常在程序中使用“scanf”而不報錯了。

    運行結果:

    總結

    以上是生活随笔為你收集整理的杭电ACM刷题(1):1002,A + B Problem II的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。