当前位置:网站首页>OJ input and output exercise
OJ input and output exercise
2022-07-01 08:04:00 【Si liumishi】
OJ Input output exercises
1. practice 1
1.1 subject 【 No number of input groups , No exit requirements 】

1.2 Code C++
#include<iostream>
int main (){
int i,d,n;
while(scanf("%d %d",&i,&d)!=EOF){
n=i+d;
printf("%d\n",n);}
return 0;
}
1.3 matters needing attention
- adopt scanf(“%d %d”,&i,&d)!=EOF To control input stop ,(EOF Is the error identifier )
- The input variable scanf(“%d %d”,&i,&d) Yes & Address value symbol , When something is passed in , It is sent to the address to save ; And output printf(“%d\n”,n) There is no need to &, The value of the output variable .
- Because it will be assigned to i、d, Addition is assigned to n, So there's no need to int i=0;int d=0;int n=0; This initializes the assignment .
- Note that the output should wrap , Input without line breaks
2. practice 2
2.1 subject 【 Tell the number of input groups 】

2.2 Code C++
#include<iostream>
using namespace std;
int main(){
int a,b,c, n;
scanf("%d",&a);
for (int t1=0;t1<a;t1++){
scanf("%d %d",&b,&c);
n=b+c;
printf("%d\n",n);
}
}
2.3 matters needing attention
- Comma English
3. practice 3
3.1 subject 【0 0 sign out 】

3.2 Code C++
#include<iostream>
using namespace std;
int main (){
int a,b,c;
scanf("%d %d",&a,&b);
while (a!=0||b!=0){
c=a+b;
printf("%d\n",c);
scanf("%d %d",&a,&b);
}
}
3.3 matters needing attention
- while Conditions inside a!=0||b!=0, perhaps !(a==0 &&b ==0)
4. practice 4
4.1 subject 【 One line for each group of data , The first integer in each line is the number of integers n;0 sign out 】

4.2 Code C++
#include<iostream>
using namespace std;
int main(){
int a=1;
int b[1000]={
0};
int sum=0;
scanf("%d ",&a);
while(a!=0){
for(int t1=0;t1<a;t1++){
if(t1!=a-1){
scanf("%d ",&b[t1]);
sum=sum+b[t1];
}else{
scanf("%d",&b[t1]);
sum=sum+b[t1];
}
}
printf("%d\n",sum);
scanf("%d ",&a);
sum=0;
int b[1000]={
0};
}
}
4.3 matters needing attention
- There is no space after the last input
- Array input plus &
5. practice 5
5.1 subject 【 One line for each group of data , The first integer in each line is the number of integers n; Number of notification groups 】

5.2 Code C++
#include<iostream>
using namespace std;
int main(){
int a=1;
int b[1000]={
0};
int sum=0;
int c=0;
scanf("%d",&c);
scanf("%d ",&a);
while(c!=0){
for(int t1=0;t1<a;t1++){
if(t1!=a-1){
scanf("%d ",&b[t1]);
sum=sum+b[t1];
}else{
scanf("%d",&b[t1]);
sum=sum+b[t1];
}
}
printf("%d\n",sum);
scanf("%d ",&a);
sum=0;
int b[1000]={
0};
c--;
}
}
5.3 matters needing attention
6. practice 6
6.1 subject 【 One line for each group of data , The first integer in each line is the number of integers n; No number of input groups 】

6.2 Code C++
#include<iostream>
using namespace std;
int main(){
int a=1;
int b[1000]={
0};
int sum=0;
while( scanf("%d ",&a)!=EOF){
for(int t1=0;t1<a;t1++){
if(t1!=a-1){
scanf("%d ",&b[t1]);
sum=sum+b[t1];
}else{
scanf("%d",&b[t1]);
sum=sum+b[t1];
}
}
printf("%d\n",sum);
sum=0;
int b[1000]={
0};
}
}
6.3 matters needing attention
7. practice 7
7.1 subject 【 One line for each group of data , The number of each line is variable ; No number of input groups 】

7.2 Code C++
#include<iostream>
int main(){
int a;
int sum=0;
while(scanf("%d",&a)!=EOF){
sum=sum+a;
if(getchar() == '\n'){
printf("%d\n",sum);
sum=0;// It can't be int sum =0;
}
}
}
7.3 matters needing attention
- Newline capture through getchar() == ‘\n’
8. practice 8
8.1 subject 【 String sort , Tell me the number 】

8.2 Code C
#include <stdio.h>
#include <string.h>
int main(){
int n;
int ch[100][100];
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",&ch[i]);
}
qsort(ch,n,sizeof(ch[0]),strcmp);
for(int j=0;j<n;j++){
printf("%s ",ch[j]);
}
return 0;
}
8.3 matters needing attention
- qsort English analysis :Quick sort, Translation is quick sorting , Its internal implementation is realized by fast sorting algorithm .
function : Sort any incoming data , Make it an ordered sequence .【 String sort !!!】 - The string is stored in a two-dimensional array /string
- sort Cannot sort string
9. practice 9
9.1 subject 【 String sort , No number , No group number 】

9.2 Code C
Fang 1:
#include<stdio.h>
#include<string.h>
int main(){
char ch[1000][1000]={
0};
int t1;
int t=0;
while(scanf("%s",&ch[t++])!=EOF){
if(getchar()=='\n'){
qsort(ch,t,sizeof(ch[0]),strcmp);
for(t1=0;t1<t;t1++){
if(t1!=t-1){
printf("%s ",ch[t1]);}
else{
printf("%s\n",ch[t1]);}
}
t=0;
}
}
return 0;
}
Fang 2:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100][100] = {
0};
int number = 0;
while (scanf("%s", str[number++]) != EOF) {
if (getchar() == '\n') {
qsort(str, number, sizeof(str[0]), strcmp);
for (int i = 0; i < number; i++) {
printf("%s ", str[i]);
}
printf("\n");
number = 0;
}
}
return 0;
}
9.3 matters needing attention
- Initialization of string char str[100][100] = {0};
10. practice 10
10.1 subject 【 String sort , No number , No group number , There are commas between strings 】

10.2 Code C
#include<stdio.h>
#include<string.h>
int main(){
char ch[1000][1000]={
0};
int t1;
int t=0;
while(scanf("%[^,\n]",&ch[t++])!=EOF){
if(getchar()=='\n'){
qsort(ch,t,sizeof(ch[0]),strcmp);
for(t1=0;t1<t;t1++){
if(t1!=t-1){
printf("%s,",ch[t1]);}
else{
printf("%s\n",ch[t1]);}
}
t=0;
}
}
return 0;
}
10.3 matters needing attention
- Regular expressions ,scanf(“%[^,\n]”,&ch[t++])!=EOF
11. practice 11
11.1 subject 【 Pay attention to the data range 】

11.2 Code
#include<iostream>
using namespace std;
int main(){
long int a=0;
long int sum =0;
while(scanf("%ld",&a)!=EOF){
sum=sum+a;
if(getchar()!='\n'){
}else{
printf("%ld\n",sum);
sum=0;
}
}
return 0;
}
11.3 matters needing attention
- sum=sum+a; To be in getchar()!='\n’ front
- The number size exceeds 1 0 7 10^7 107 when , Use long int as well as %ld
边栏推荐
- 【mysql学习笔记26】视图
- STM32 uses esp01s to go to the cloud, mqtt FX debugging
- Li Kou daily question - day 31 -202 Happy number
- [getting started] input n integers and output the smallest K of them
- How to use layui to display the data in the database in the form of tables
- 良心安利万向轮 SolidWorks模型素材网站
- Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
- 如何使用layui将数据库中的数据以表格的形式展现出来
- How outlook puts together messages with the same discussion
- uni 热更新
猜你喜欢
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

038 network security JS

Instead of houses, another kind of capital in China is rising

软键盘高度报错

web254
![[kv260] generate chip temperature curve with xadc](/img/fc/e5e4648b09b1123b2d494b75a9f8f7.png)
[kv260] generate chip temperature curve with xadc

2022 mobile crane driver test exercises and online simulation test

Learn the knowledge you need to know about the communication protocol I2C bus

SQL number injection and character injection

base64
随机推荐
OJ输入输出练习
base64
Source code analysis of open source API gateway APIs IX
She is the "HR of others" | ones character
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
源代码加密的意义和措施
EDA开源仿真工具verilator入门6:调试实例
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
Vhost kick & call principle
base64
AArdio - 【问题】bass库回调时内存增长的问题
力扣每日一题-第31天-1502.判断能否形成等差数列
038 network security JS
良心安利万向轮 SolidWorks模型素材网站
Download xshell and xftp
Aardio - 自己构造的getIconHandle的方法
The triode is a great invention
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
Saving db4i depth camera pictures with MATLAB