今天第一次做杭电的acm题
初看1000感觉非常简单,但是我这样写完提交出去时,总是报wrong answer!,My God,我太天真啦!
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << a+b << endl;
return 0;
}
杭电题1000
Problem Description
Calculate
A + B.
Input
Each line will contain two integers
A and
B. Process to end of file.
Output
For each case, output
A + B in one line.
Sample Input
1 1
Sample Output
2
注意其中Process to end of file.没说你只运行一次就OK,所以要考虑多次运行的情况,很自然就想到用while语句啊!
#include "iostream"
using namespace std ;
int main()
{
int a,b;
while (cin>>a>>b)
{
cout<<a+b<<endl;
}
return 0;
}
杭电题1001
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line. 输入将包含一个整数,每行只有一个整数
Output
For each case,
output SUM(n) in one line,
followed by a blank line. You may assume the result will be in the range of
32-bit signed integer. ----》int类型啊
Sample Input
1 100
Sample Output
1 5050
赶鸭子的,这题捣腾了半天,硬是没看懂Input和Output,唉,看题不仔细啊。
#include "iostream"
using namespace std ;
int main()
{
int n;
while(cin>>n)
{
int sum=0,i=1;
for(i;i<=n;i++)
sum+=i;
cout<<sum<<endl<<endl;
}
return 0;
}
效果展示:
杭电题1002
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
//将这个数字拆开,拆成一位一位的,或者是四位四位的存储到一个数组中, 用一个数组去表示一个数字
#include "iostream"
using namespace std ;
int main()
{
int n;
int a[1002]={0},
b[1002]={0},
c[1002]={0};
char str1[1002],str2[1002];
int len1,len2,large,count,sign=0;
cin>>n;
while (n--)
{
if(sign>0)cout<<endl;
cin>>str1>>str2;
len1=strlen(str1);
len2=strlen(str2);
large =len1>len2?len1:len2;
for(count=0;count<len1;count++){
a[count] = str1[len1 - count -1] - '0';//字符型减去'0'的ASCIII码值转换为数字
}
for(count=0;count<len2;count++){
b[count]=str2[len2-count-1]-'0';
}
for(count=0;count<large;count++){//处理进位问题,如果大于10,则进位
c[count]=a[count]+b[count]+c[count];
c[count+1]=c[count]/10;
c[count]=c[count]%10;
}
if (c[count])large++;
sign++;
cout<<"Case"<<" "<<sign<<":"<<endl;
cout<<str1<<" "<<"+"<<" "<<str2<<" "<<"="<<" ";
for (count=large-1;count>=0;count--)
cout<<c[count];
cout<<endl;
for (count=0;count<large;count++)
{
a[count]=0;
b[count]=0;
c[count]=0;
}
}
return 0;
}