主題
Calculate a + b
杭電OJ-1000
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.
Mine
#include <stdio.h> int main()
{
int a,b;
while(~scanf("%d %d",&a,&b)) //多次輸入a和b。
{
printf("%d\n",a+b);
}
} /***
while(~scanf("%d %d",&a,&b)) 多次輸入a和b。
這句話中的“~”符號可以理解為“重複”,程式碼含義是反覆執行scanf(“%d %d”,&a,&b) 語句,直到語句接收不到有效結果。換一種說法就是while語句會在括號中的判斷為真的情況執行語句,那麼對於scanf函式而言,判斷為真也就是接收到了有效資料。而~符號代表無限重複,直到scanf語句不能取到有效的值為止(while的括號中判斷為假),迴圈跳出。
***/
Review
題目: 接收兩個整數並返回兩個數的和。
需要注意的是題目中說明了每行兩個資料,但並沒有說明多少行。換一種常用說法叫:“多組資料”,是常見的要求。但沒有C語言演算法書會寫明接收多組資料的方式。
杭電OJ-1001
Description
calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Outpu
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.
Mine
#include<stdio.h> int main()
{
int x,n,sum;
scanf("%d",&x);
scanf("%d",&n);
for(x=1;x<=n;x++)
{
sum=0;
sum=sum+x;
}
printf("1\n%d",sum); }
Writeup
#include <stdio.h>
int main()
{
int a;
int sum=0;
while((scanf("%d",&a))!=EOF){
for(int i=0;i<=a;i++)
sum = sum+i;
printf("%d\n\n",sum);
sum = 0;
}
return 0;
}
Review
- 我的問題在於沒有考慮連續讀取的可能性,好像對輸入輸出有誤解T-T
- 有一個疑問:輸入輸出需要和sample一樣的格式嗎?
- 本身是一個前N項和的累加問題,如果用公式法也是no accept,原因是S=(1+n)*n/2中乘法容易造成溢位,而迴圈累加的好處在於溢位的可能比較小。
杭電OJ-1002
Description
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.
手寫程式碼@V1-20210805
#include<stdio.h>
#include<string.h>
#define max 1005 int main()
{
int a[max],b[max],c[max];
while(~scanf("%s1 %s2",s1,s2))
{
int i,j,k;
i=0;j=0;
k=strlen(s1)>strlen(s2)?strlen(s1):strlen(s2) for(i;i<=k;i++) //k有無定義的必要?
{
a[]=s1; //好像不太合適?字元陣列能直接賦值給陣列嗎?查一下書
b[]=s2; //似乎需要迴圈讀入? c[i]=a[i]+c[i];
if(c[i]>=10)
{
c[i]=c[i]%10;
c[i+1]++; //s2的長度是必要的嗎?
}
}
for(j=0;j<=k;j++)
{
printf("%d",a[j]); //哪裡有點奇怪
}
} }
v1現在存在的問題
- 讀入的順序和相加的順序不太對?要處理一下?[n-i]好像可行?
- 輸出的時候應該是逆序?
- 需要加一個讀入的限制條件:讀取正整數?
- 題目要求的輸入輸出 用一個迴圈?
手寫程式碼@V2-20210806
#include<stdio.h>
#include<string.h>
#define max 1010 int main()
{
int T,u,n,i,j;
char s1[max],s2[max],c[max];
scanf("%d",&T);
while(T>=1 && T<=20)
{
for(u=0;u<=T;u++)
{
scanf("%s %s",s1,s2);
} n =strlen(s1)>strlen(s2)?strlen(s1):strlen(s2); for(i=2 ; i<=n ; i++)
{
c[n-i]=s1[n-i]+s2[n-i];
if(c[i]>=10)
{
c[n-i]=c[n-i]%10;
c[n-i-1]++; //沒有考慮不是同位數的情況
}
} for(j=0;j<=n;j++)
{
printf("case %d:\n",u);
printf("%s + %s = %d\n",s1,s2,c[j]);
} } }
writeup-0806
#include<stdio.h>
#include<string.h>
#define max 1000+10 /**
* 1. define the variable
**/ int a[max],b[max];
char str1[max],str2[max]; int main(){ int m; //test number T?
int k=1;
scanf("%d",&m); // read T? /**
*2.read number and make sure input.
this part is to read the big number and covert to array
**/ while(m--){ //this circle ie funny! it's better than mine which use more variable u. scanf("%s %s",str1,str2); //read big number
memset(a,0,sizeof(a));
memset(b,0,sizeof(b)); //memset() 函式可以說是初始化記憶體的“萬能函式”,通常為新申請的記憶體進行初始化工作。 int i,j;
for(i=0,j=strlen(str1)-1;i<strlen(str1);i++){ //it's similar to my code ,i use the i=1 to replace len
a[j--]=str1[i]-'0'; //string1 covert to array1?
} for(i=0,j=strlen(str2)-1;i<strlen(str2);i++){ //two strlen conditions,less code ,nice~
b[j--]=str2[i]-'0'; //string2 to array2? why not combine with above 'for cicle' together ?
}
/**
* 3. add two big number
**/
for(i=0;i<max;i++){
a[i]+=b[i];
if(a[i]>=10){
a[i]-=10; //mine : a[i]=a[i]%10
a[i+1]+=1; //similar~
}
}
/**
* 4.output
*
**/
printf("Case %d:\n",k++); // k has been defined and value=1?
printf("%s + %s = ",str1,str2);
for(i=max-1;(i>=0)&&(a[i]==0);i--); //reverse output?(i>=0)&&(a[i]==0) what's mean? only deal the 10?
if(i>=0){
for(;i>=0;i--){
printf("%d",a[i]);
}
}
else printf("0");
if(m!=0) printf("\n\n");
else printf("\n"); //not clear..
}
return 0;
}
Review
- 大數加法問題一般考慮陣列進行儲存,然後按位相加滿十進一。
- 再看Writeup時發現大家再提java,
import java.util.Scanner
,以及大數需要import java.math.BigInteger
,且BigInterger相加不是"a+b",而是"a.add(b)",就可以很好的解決大數問題。
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
public static void main(String args[]){
BigInteger a,b;
int T;
int n=1;
Scanner in = new Scanner(System.in);
T=in.nextInt();
while(T>0){
a=in.nextBigInteger();
b=in.nextBigInteger();
System.out.println("Case "+n+":");
System.out.println(a+" + "+b+" = "+a.add(b));
if(T!=1) System.out.println();
T--;
n++;
}
}
}
杭電OJ-1089
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
**Mine **- accepted
#include<stdio.h> int main()
{ int a,b;
while(~scanf("%d %d",&a,&b))
{
printf("%d\n",a+b); }
return 0; }
杭電OJ-1090
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Mine
#include<stdio.h> int i,a,b,c;
int main()
{
while(~scanf("%d",&i))
{
for(c=1;c<=i;c++)
{
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
}
}
}
杭電OJ-1091
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Mine
#include<stdio.h> int a,b;
int main()
{
while(~scanf("%d %d",&a,&b))
{
if(a==0 && b==0)
{return 0;}
else
{
printf("%d\n",a+b);
}
}
}
杭電OJ-1092
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Mine
#include<stdio.h> //始終是wrong... int main()
{
int a,n,sum;
while(scanf("%d",&n)!=EOF && n!=0)
{
while(n--)
{
scanf("%d",&a);
sum=0;
sum+=a;
}
printf("%d\n",sum);
}
return 0;
}
Writeup
#include<stdio.h>
int main()
{
int a[10000];
int n, i, s;
while (scanf("%d", &n) && n)
//輸入正確scanf返回1,n!=0繼續輸入
{
for (i = s = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
{
s = s + a[i];
}
printf("%d\n", s);
}
return 0;
}Review
沒有考慮到大數的可能,存在溢位問題,用陣列存放更為合理
杭電OJ-1093
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Mine
#include<stdio.h> //wrong answer... int main()
{
int i,j,k,s;
int a[1000];
while(~scanf("%d",&i))
{
for(i;i>=1;i--)
{
while(scanf("%d",&j)!=EOF && j)
{
for(j;j>=0;j--)
{
scanf("%d", &a[i]);
}
for (k= 0; k< j; k++)
{
s += a[i];
}
printf("%d\n", s);
}
}
}
}writeup
#include<stdio.h>
int main()
{
int n,i,m,sum; //n是行數,m是加數個數
scanf("%d",&n);
while(n--) //簡潔!
{
sum=0;
scanf("%d",&m);
while(m--) //兩個-- 簡潔欸!
{
scanf("%d",&i);
sum=sum+i;
}
printf("%d\n",sum);
}
return 0;
}
Review
所以上一道題不是大數的原因?? (((φ(◎ロ◎;)φ)))
杭電OJ-1094
Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.
Mine
#include<stdio.h> int main()
{
int n,m,sum;
while(~scanf("%d",&n))
{
sum = 0;
while(n--)
{
scanf("%d",&m);
sum += m;
}
printf("%d\n",sum);
}
}
杭電OJ-1095
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
Mine
#include<stdio.h>
int main()
{
int a,b;
while(~scanf("%d %d",&a,&b))
{
printf("%d\n",a+b);
printf("\n");
}
return 0;
}
杭電OJ-1096
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
Mine
#include<stdio.h>
int main()
{
int n,m,i;
int sum;
while(~scanf("%d\n",&n)) //行數
{
while(n--)
{
scanf("%d",&m); //加數個數
sum = 0;
while(m--)
{
scanf("%d",&i);
sum += i;
}
printf("%d\n",sum);
if(m!=0)
printf("\n",sum);
}
}
}