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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言oj中a b怎么做,【HDUOJ】第1002题 A + B Problem II 纯C语言解法

發(fā)布時間:2023/12/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言oj中a b怎么做,【HDUOJ】第1002题 A + B Problem II 纯C语言解法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【HUDOJ-1002】

1.原題:

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

21 2112233445566778899 998877665544332211

Sample Output

Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110

Author

Ignatius.L

2:解題思路及技巧:

在計算機(jī)中,當(dāng)要表示的數(shù)據(jù)超出計算機(jī)所使用的數(shù)據(jù)的表示范圍時,則產(chǎn)生數(shù)據(jù)的溢出。溢出導(dǎo)致大數(shù)相加計算錯誤,此時就需要一種新的辦法。由于大數(shù)無法直接保存,我們采用字符串的方式保存大數(shù)的每一位,然后將字符轉(zhuǎn)換為數(shù)字型,類似于小學(xué)加法算式將兩個數(shù)對應(yīng)位相加(大于十進(jìn)一),這里需要注意以下幾點(diǎn):

a.字符轉(zhuǎn)數(shù)字:遍歷數(shù)組對每一位減‘0’得到對應(yīng)數(shù)字的integer值。

b.要初始化所有數(shù)組的所有元素為0以便后續(xù)的計算,這里采用memset函數(shù)將數(shù)組的所有元素初始化為0。

c.存數(shù)時需要倒序輸入便于計算。

d.memset函數(shù):memset是計算機(jī)中C/C++語言函數(shù)。將s所指向的某一塊內(nèi)存中的后count個字節(jié)的內(nèi)容全部設(shè)置為ch指定的ASCII值, 第一個值為指定的內(nèi)存地址,塊的大小由第三個參數(shù)指定,這個函數(shù)通常為新申請的內(nèi)存做初始化工作, 其返回值為s。

void*?memset(void*?s,intch,size_tcount)

{

char*xs?=?(char*)?s;

while(count--)

*xs++?=?ch;

returns;

}

3:完整代碼(已AC)

#include #include int main(int argc,char const*argv[])

{

int T;

char m[1001],n[1001];

scanf("%d",&T);

int cnt=1;

int k=T;

while(k--){

int a[1001],b[1001],c[1001];

memset(a,0,sizeof(a));

memset(b,0,sizeof(b));

memset(c,0,sizeof(c));

//初始化數(shù)組元素為零

int l,len;

scanf("%s",&m);

scanf("%s",&n);

//讀入字符數(shù)組

int len1=strlen(m),len2=strlen(n);

//保存字符型數(shù)字的長度

int max=(len1>len2)?len1:len2;

for(int i=0;i=10){

c[len]+=a[len]+b[len]-10;

c[len+1]++;

}

else

c[len]+=a[len]+b[len];

}

for(l=len;l=10){

c[l]+=a[l]-10;

c[l+1]++;

}

else

c[l]+=a[l];

}

printf("Case %d:\n",cnt);

for(int i=len1-1;i>=0;i--)

printf("%d",a[i]);

printf(" + ");

for(int i=len2-1;i>=0;i--)

printf("%d",b[i]);

printf(" = ");

for(int x=l-1;x>=0;x--)

printf("%d",c[x]);

if(cnt++

總結(jié)

以上是生活随笔為你收集整理的C语言oj中a b怎么做,【HDUOJ】第1002题 A + B Problem II 纯C语言解法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。