杭電1002

來源:互聯網
上載者:User

標籤:

#include <iostream>#include <string>#include <string.h>#include <stdlib.h>using namespace std;int main(){int a[1002];int b[1002];int n;char aa[1002];char bb[1002];cin>>n;int line = 1;while (line <= n){cin>>aa>>bb;memset(a, 0, sizeof(a));       //不要寫入程式碼,採用sizeof,因為是int是4位元組,之前自己給自己埋了個坑,寫成1002。。。memset(b, 0, sizeof(b));int strLenA = strlen(aa);for (int i = 0; i < strLenA; i++)    //數組逆轉存入{a[i] = aa[strLenA - 1 - i] - ‘0‘;}int strLenB = strlen(bb);for (int j = 0; j < strLenB; j++){b[j] = bb[strLenB - 1 - j] - ‘0‘;}int maxLen = strLenA > strLenB ? strLenA : strLenB;for (int k = 0; k < maxLen; k++)   //先處理高位{a[k+1] += (a[k] + b[k]) / 10;a[k] = (a[k]+ b[k]) % 10;}cout<<"Case "<<line<<":"<<endl;cout<<aa<<" + "<<bb<<" = ";if (a[maxLen] > 0)                //判斷最高位是否有進位cout<<a[maxLen];for (int kk = maxLen - 1; kk >= 0; kk--)cout<<a[kk];cout<<endl;if (line < n)                  //注意最後一個案例不要多輸出空行cout<<endl;line++;}return 0;}

  

杭電1002

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.