Input and output exercises in online programming of C language
A+B(1)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: the input includes two positive integers a and B (1 < = a, B < = 10 ^ 9), and the input data includes multiple groups.
Output Description: output the result of a+b
Input example 1:
1 5
10 20
Output example 1:
6
30
Test code:
#include<stdio.h> int main(){ int a,b; //Writing method 1: while(scanf("%d %d",&a,&b)==2) //The scanf function returns the number of data items successfully read in //Method 2: //while(scanf("%d %d\n",&a,&b)!=EOF) { printf("%d\n",a+b); } return 0; }
Note: scanf function returns the number of data items successfully read in, that is, the number of data items read in each time
A+B(2)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: input the number of data groups t in the first row (1 < = T < = 100)
Next, each line includes two positive integers a, B (1 < = a, B < = 10 ^ 9)
Output Description: output the result of a+b
Input example 1:
2
1 5
10 20
Output example 1:
6
30
Test code:
//Method 1: use t to write a for loop #include<stdio.h> int main(){ int t,a,b; scanf("%d",&t); for(int i=0;i<t;i++){ scanf("%d %d",&a,&b); printf("%d\n",a+b); } return 0; } //Method 2: directly receive the following numbers and output the results #include<stdio.h> int main(){ int t,a,b; scanf("%d",&t); //while(scanf("%d %d",&a,&b)==2) while(scanf("%d %d\n",&a,&b)!=EOF) { printf("%d\n",a+b); } return 0; } //Method 3: write a while loop using t # include <stdio.h> int main() { int t,a,b; scanf("%d",&t); while (t) { scanf("%d %d",&a,&b); printf("%d \n",a+b); t--; } return 0; } //Writing method 2: do not return 0 #include<stdio.h> int main(void) { int t, a, b; scanf("%d", &t); while(t) { scanf("%d %d", &a, &b); printf("%d\n", a + b); t--; } }
A+B(3)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: the input includes two positive integers a and B (1 < = a, B < = 10 ^ 9). There are multiple groups of input data. If the input is 0, the input will be ended
Output Description: output the result of a+b
Input example 1:
1 5
10 20
0 0
Output example 1:
6
30
Test code:
//Method 1: add a limit to the cycle conditions #include<stdio.h> int main(void){ int a,b; while(scanf("%d %d",&a,&b)!=EOF && a!=0 && b!=0) //while(scanf("%d %d",&a,&b)==2 && a!=0 && b!=0) { printf("%d\n",a+b); } } //Method 2: judge the exit in the loop #include <stdio.h> int main(){ int a,b; while(scanf("%d%d\n",&a,&b)!=EOF) { if(a==0&&b==0) break; printf("%d\n",a+b); } }
A+B(4)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: the input data includes multiple groups.
Each group of data has one row. The first integer of each row is the number of integers n (1 < = n < = 100). When n is 0, input ends.
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description: output the summation result of each group of data
Input example 1:
4 1 2 3 4
5 1 2 3 4 5
0
Output example 1:
10
15
Test code:
//Method 1: write it yourself and directly modify the conditions of the while loop #include<stdio.h> int main(){ int n,res=0,sum=0; scanf("%d",&n); //First, get an n value //while(n!=0) while(n) { sum=0; for(int i=0;i<n;i++){ scanf("%d",&res); sum+=res; } printf("%d\n",sum); scanf("%d",&n); //After the cycle is completed, take down an n value for judgment } } //Method 2: judge n==0 in the loop to exit #include <stdio.h> int main(){ int n, num, sum=0; while(scanf("%d", &n)!=EOF){ sum=0; if(n==0) break; for(int i=0;i<n;i++) { scanf("%d", &num); sum+=num; } printf("%d\n", sum); } return 0; } //Method 3: use while (scanf ('% d', & n), n) to judge #include<stdio.h> int main() { int n,a,sum; while(scanf("%d",&n),n){ if(n == 0) { break; } sum=0; for(int i=0;i<n;i++) { scanf("%d",&a); sum += a; } printf("%d\n",sum); } return 0; }
Note: while (scanf ('% d', & n), n) is actually equivalent to while(n), but it is written as while (scanf ('% d', & n), n) because it needs to be input
A+B(5)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 32M, other languages 64M
Input Description: the first line of input includes a positive integer t (1 < = T < = 100), indicating the number of data groups.
Next t rows, one set of data per row.
The first integer in each line is the number of integers n (1 < = n < = 100).
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description: output the summation result of each group of data
Input example 1:
2
4 1 2 3 4
5 1 2 3 4 5
Output example 1:
10
15
Test code:
//Method 1: #include<stdio.h> int main(void){ int t,n,temp,sum; scanf("%d",&t); while(t){ scanf("%d",&n); sum=0; for(int i=0;i<n;i++){ scanf("%d",&temp); sum+=temp; } printf("%d\n",sum); t--; } } //Method 2: combine n and t as the condition judgment of while #include<stdio.h> int main(void) { int t, n, a,sum; scanf("%d", &t); while(scanf("%d", &n), n && t) { if(n == 0) break; sum = 0; for(int i= 0; i < n; i++) { scanf("%d", &a); sum += a; } printf("%d\n", sum); t--; } return 0; } //Method 3: two for loops #include "stdio.h" int main(){ int n; scanf("%d", &n); int b; for(int i = 0; i < n; i++){ scanf("%d", &b); int sum = 0; for(int j = 0; j < b; j++){ int num = 0; scanf("%d", &num); sum += num; } printf("%d\n", sum); } return 0; }
A+B(6)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: there are multiple groups of input data, and each line represents a group of input data.
The first integer in each line is the number of integers n (1 < = n < = 100).
Next, there are n positive integers, that is, each positive integer that needs to be summed.
Output Description: output the summation result of each group of data
Input example 1:
4 1 2 3 4
5 1 2 3 4 5
Output example 1:
10
15
Test code:
//Method 1: #include<stdio.h> int main(void){ int n,temp,sum; while(scanf("%d",&n)!=EOF) // while (EOF!=scanf("%d", &n)) { sum=0; for(int i=0;i<n;i++){ scanf("%d",&temp); sum+=temp; } printf("%d\n",sum); } }
A+B(7)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: there are multiple groups of input data, and each line represents a group of input data.
Each line may have n integers separated by spaces. (1 <= n <= 100).
Output Description: output the summation result of each group of data
Input example 1:
1 2 3
4 5
0 0 0 0 0
Output example 1:
6
9
0
Test code:
//Writing method 1: #include<stdio.h> int main(void){ int temp,sum=0; while(scanf("%d",&temp)!=EOF){ sum+=temp; if(getchar()=='\n'){ //Output to the end of a line and reset sum printf("%d\n",sum); sum=0; } } } //Code 2: #include<stdio.h> int main(){ int n,sum=0; char c; while(scanf("%d",&n)!=EOF){ c=getchar(); //Put the value of getchar into c, and then make subsequent judgment sum+=n; if(c=='\n'){ printf("%d\n",sum); sum=0; } } return 0 ; }
String sort (1)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: the input has two lines, the first line n
The second line is a string separated by n spaces
Output Description: output a row of sorted string, separated by spaces and without ending spaces
Input example 1:
5
c d a bb e
Output example 1:
a bb c d e
Test code:
//Method 1: #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ int n; scanf("%d",&n); char str[n][100]; for(int i=0;i<n;i++){ scanf("%s",&str[i]); } qsort(str,n,sizeof(str[0]),strcmp); //sort for(int i=0;i<n;i++){ printf("%s ",str[i]); } } //Method 2: make your own qsort comparison function #include <stdio.h> #define LENGTH 1000 int compar(const void *p1, const void *p2){ return strcmp((char *)p1, (char *)p2); } int main(int argc, char **argv){ int len = 0; scanf("%d", &len); char arr[len][LENGTH]; for(int i=0; i<len; i++) { scanf("%s", arr[i]); } qsort(arr, len, sizeof(arr[0]), compar); for(int i=0; i<len; i++) { printf("%s ", arr[i]); } return 0; } //Writing method 3: #include <stdio.h> #include <stdlib.h> #include <string.h> int Cmp(const void* a, const void* b) { char* newA = (char*)a; char* newB = (char*)b; return *newA - *newB; } int main() { int n; scanf("%d", &n); char str[n][100]; for (int i = 0; i < n; i++) { scanf("%s", str[i]); } qsort(str, n, sizeof(str[0]), Cmp); for (int i = 0; i < n; i++) { printf("%s ", str[i]); //Note the space here } return 0; }
String sorting (2)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: multiple test cases, one line for each test case.
Each line is separated by spaces, with n characters, n < 100
Output Description: for each group of test cases, output a row of sorted strings, each separated by a space
Input example 1:
a c bb
f dddd
nowcoder
Output example 1:
a bb c
dddd f
nowcoder
Test code:
//Writing method 1: #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ char str[100][100]; int i=0; while(scanf("%s",str[i])!=EOF){ //Here, the array can be added or not, because it is an address itself i++; if(getchar()=='\n'){ qsort(str,i,sizeof(str[0]),strcmp); for(int x=0;x<i;x++){ printf("%s ",str[x]); //Note the space here } printf("\n"); //A newline character is also required after each line of output i=0; } } } //Writing method 2: compile cmp functions by yourself #include<stdio.h> #include<stdlib.h> #include<string.h> int compare(const void* a, const void* b){ return strcmp((char*)a, (char*)b); //return *(char*)a- *(char*)b; } //int compare(const void *a,const void *b){ // char * _a=(char*)a; // char * _b=(char*)b; // return strcmp(_a,_b); //} int main(){ int n = 0, i; char str[100][100]; while (scanf("%s", &str[n]) != EOF){ n++; if (getchar() == '\n'){ qsort(str, n, sizeof(str[0]), compare); for (i = 0; i < n - 1; i++){ printf("%s ", str[i]); } printf("%s\n", str[n - 1]); n = 0; } } return 0; }
String sorting (3)
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
Input Description: multiple test cases, one line for each test case.
Each line is separated by, with n characters, n < 100
Output Description: for each group of use cases, output a row of sorted string separated by ',' and no ending space
Input example 1:
a,c,bb
f,dddd
nowcoder
Output example 1:
a,bb,c
dddd,f
nowcoder
Test code:
//Method 1: #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ char str[100][100]; int i=0;int x; while(scanf("%[^,\n]",str[i++])!=EOF){ if(getchar()=='\n'){ qsort(str,i,sizeof(str[0]),strcmp); for(x=0;x<i-1;x++){ printf("%s,",str[x]); //Note the number here } printf("%s\n",str[x]); i=0; } } } //Method 2: output different writing methods #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ int n, j; char str[100][100]; n = 0; while (scanf("%[^,\n]", str[n]) != EOF){ n++; if (getchar() == '\n'){ qsort(str, n, sizeof(str[0]), strcmp); for (j = 0; j < n - 1; j++){ printf ("%s,", str[j]); } printf ("%s\n", str[n - 1]); n = 0; } } return 0; } //Method 3:
Note: ^ indicates "not", [^ \ n] indicates that reading ends when reading newline characters. This is the regular usage of scanf. We all know that scanf cannot receive space characters. It ends reading as soon as it receives spaces. Therefore, it cannot accept a line of string like functions such as gets(), but it can read a line with% [^ \ n] until it encounters' \ n '.
Self test local pass submission is 0
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 256M, other languages 512M
In the first few online written test programming questions every year, some students always ask why I passed the local test and the self-test, but the code submission system returns a pass rate of 0.
It's not the system's fault, it may be because
1. You misunderstood the topic. Your code only passed the sample or your own data
2. There is something wrong with your code logic. Your code has only passed the sample or your own data. In short, your code has only passed the sample and self-test data. You can't see the background test data at all. Think more for yourself.
Input Description: enter multiple groups of test cases, with each group of spaces separated by two integers
Output Description: output the sum of two integers in a row for each group of data
Input example 1:
1 1
Output example 1:
2
Test code:
#include<stdio.h> int main(void){ long long int a,b; while(scanf("%lld %lld ",&a,&b)!=EOF){ printf("%lld\n",a+b); } }
Note: this pit was really shown. When I thought it was the same as the previous question, or thought it would be finished as long as I did a good job in the blank space, I found that it didn't pass completely, and I found out—— It's actually because of the data type.
Some explanation
1. EOF represents the end of file in C language, or more precisely in C standard function library. EOF is used as the end of file flag in the while loop. Therefore, while (scanf ('% d', & n)= EOF) can be used to input multiple sets of data.
2. Some questions have requirements. When n is 0, it will not be processed. You can write while (scanf ("% d", & n) & & n= 0), or you can directly write while (scanf ('% d', & n) & & n), and then become while (scanf ('% d', & n), n).
3. While (scanf ('% d', & n), n) is a comma expression in while parentheses.
- Comma expression, which connects two expressions. For example: (3 + 5, 6 + 8) is called comma expression. Its solution process starts with expression 1 and then expression 2. The whole expression value is the value of expression 2, for example: (3 + 5, 6 + 8) is 14.
- Therefore, the while (scanf ('% d', & n), n) statement is to first input an integer assignment to n (expression 1), and then the value in the while bracket is n (expression 2). If n is true, the while loop will be carried out, and if n is 0, no processing will be carried out.
summary
1,while(scanf("%d",&n)&&n!= 0) is equivalent to while (scanf ('% d', & n) & & n) is equivalent to while (scanf ('% d', & n), n).
2. The while (scanf ('% d', & n), n) statement is to input an integer and assign it to n (expression 1), and then the value in the while bracket is n (expression 2). If n is true, the while loop will be carried out, and if n is 0, no processing will be carried out.
3. Scanf ('% [^, \ n]' is a comma and \ N end encountered.
4. The scanf function returns the number of data items successfully read in, that is, the number of data items read in each time.