poj2756解題報告

來源:互聯網
上載者:User
Autumn is a Genius
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6040   Accepted: 1269

Description

Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can do integer additions when she was just 2 years old! Since a lot of people suspect that Autumn may make mistakes, please write a program to prove that Autumn is a real genius.

Input

The first line contains a single integer T, the number of test cases. The following lines contain 2 integers A, B(A, B < 32768) each. The size of input will not exceed 50K.

Output

The output should contain T lines, each with a single integer, representing the corresponding sum.

Sample Input

11 2

Sample Output

3

Hint

There may be '+' before the non-negative number!題目大意:Autumn是一個天才,他能計算A+B的和 (A, B < 32768).......思路:這個題玩文字遊戲,只給定了AB的上限而沒給定AB下線,靠的。。。Autumn真是個天才,哎無奈 乖乖的用高精加減吧。。。汗#include<iostream><br />using namespace std;<br />char a[100000],b[100000],c[1000000];int lena,lenb,lenc;<br />void plus()<br />{<br />int jin=0,i;<br />lena=strlen(a)-1;<br />lenb=strlen(b)-1;<br />if(lena>lenb)<br />{strcpy(c,a);lenc=lena;}<br />else<br />{strcpy(c,b);strcpy(b,a);lenc=lenb;lenb=lena;}<br />while(lenb>-1)<br />{<br />i=c[lenc]+b[lenb]+jin-96;<br />c[lenc]=i%10+48;<br />jin=i/10;<br />lenc--;lenb--;<br />}<br />while(lenc>-1&&jin)<br />{<br />i=c[lenc]-48+jin;<br />c[lenc]=i%10+48;<br />jin=i/10;<br />lenc--;<br />}<br />if(jin)<br />{<br />for(i=strlen(c)+1;i>0;i--)<br />c[i]=c[i-1];<br />c[0]=jin+48;<br />}<br />}<br />void jian()<br />{<br />int jie=0,i,j;<br />lena=strlen(a)-1;<br />lenb=strlen(b)-1;<br />lenc=lena;<br />strcpy(c,a);<br />while(lenb>-1)<br />{<br />if(c[lenc]-b[lenb]-jie<0)<br />{<br />c[lenc]=c[lenc]-b[lenb]-jie+58;<br />jie=1;<br />}<br />else<br />{c[lenc]=c[lenc]-b[lenb]-jie+48;jie=0;}<br />lenb--;lenc--;<br />}<br />while(lenc>-1&&jie)<br />{<br />if(c[lenc]-48-jie<0)<br />{c[lenc]='9';jie=1;}<br />else<br />{c[lenc]-=jie;jie=0;}<br />lenc--;<br />}<br />for(i=0;c[i]=='0'&&i<strlen(c)-1;i++);<br />for(j=0;;j++)<br />{<br />c[j]=c[i+j];<br />if(c[j]=='/0')<br />break;<br />}<br />}<br />int main()<br />{<br />int t,i;<br />cin>>t;<br />while(t--)<br />{<br />int f1=1,f2=1;<br />cin>>a;<br />if(a[0]=='-'||a[0]=='+')<br />{<br />if(a[0]=='-')<br />f1=-1;<br />for(i=0;a[i]!='/0';i++)<br />a[i]=a[i+1];<br />}<br />cin>>b;<br />if(b[0]=='-'||b[0]=='+')<br />{<br />if(b[0]=='-')<br />f2=-1;<br />for(i=0;b[i]!='/0';i++)<br />b[i]=b[i+1];<br />}</p><p>if(f1*f2==1)<br />{<br />plus();<br />if(f1==-1)<br />cout<<'-'<<c<<endl;<br />else<br />cout<<c<<endl;<br />}<br />else if(f1*f2==-1)<br />{<br />if(f1==1)<br />{<br />if(strlen(a)>strlen(b)||(strlen(a)==strlen(b)&&strcmp(a,b)>0))<br />{<br />jian();<br />cout<<c<<endl;<br />}<br />else<br />{<br />strcpy(c,a);strcpy(a,b);strcpy(b,c);<br />jian();<br />if(strcmp(c,"0"))<br />cout<<'-'<<c<<endl;<br />else<br />cout<<"0"<<endl;<br />}<br />}<br />else<br />{<br />if(strlen(a)>strlen(b)||(strlen(a)==strlen(b)&&strcmp(a,b)>0))<br />{<br />jian();<br />if(strcmp(c,"0"))<br />cout<<'-'<<c<<endl;<br />else<br />cout<<"0"<<endl;<br />}<br />else<br />{<br />strcpy(c,a);strcpy(a,b);strcpy(b,c);<br />jian();<br />cout<<c<<endl;<br />}</p><p>}<br />}<br />}<br />return 0;<br />}

聯繫我們

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