当前位置:网站首页>OJ输入输出练习
OJ输入输出练习
2022-07-01 07:58:00 【司六米希】
1. 练习1
1.1 题目【无输入组数,无退出要求】

1.2 代码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 注意事项
- 通过scanf(“%d %d”,&i,&d)!=EOF来控制输入停止,(EOF为错误识别符)
- 输入变量scanf(“%d %d”,&i,&d)有&地址取值符号,传东西传入时,是传到地址里面保存;而输出 printf(“%d\n”,n)则是不需要&,输出变量的值。
- 因为输入时会赋值给i、d,加法会赋值给n,所以不用int i=0;int d=0;int n=0;这样初始化赋值。
- 注意输出要换行,输入不用换行
2. 练习2
2.1 题目【告知输入组数】

2.2 代码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 注意事项
- 逗号英文
3. 练习3
3.1 题目【0 0 退出】

3.2 代码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 注意事项
- while里面的条件a!=0||b!=0,或者!(a==0 &&b ==0)
4. 练习4
4.1 题目【每组数据一行,每行的第一个整数为整数的个数n;0退出】

4.2 代码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注意事项
- 最后一次输入后面没有空格
- 数组输入加&
5. 练习5
5.1 题目【每组数据一行,每行的第一个整数为整数的个数n;告知组数】

5.2 代码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 注意事项
6. 练习6
6.1 题目【每组数据一行,每行的第一个整数为整数的个数n;无输入组数】

6.2 代码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 注意事项
7. 练习7
7.1 题目【每组数据一行,每行个数不定;无输入组数】

7.2代码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;//不能是int sum =0;
}
}
}
7.3注意事项
- 换行捕捉通过getchar() == ‘\n’
8. 练习8
8.1 题目【字符串排序,告知个数】

8.2 代码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 注意事项
- qsort英语解析:Quick sort,翻译就是快速排序,它的内部实现是通过的快速排序算法来实现的。
功能:对传入的任何数据进行排序,使其变成有序数列。【字符串排序!!!】 - 字符串使用二维数组存储/string
- sort无法排序字符串
9.练习9
9.1 题目【字符串排序,无个数,无组数】

9.2 代码C
方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;
}
方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 注意事项
- 字符串的初始化char str[100][100] = {0};
10.练习10
10.1 题目【字符串排序,无个数,无组数,字符串之间逗号分嗝】

10.2 代码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 注意事项
- 正则表达式,scanf(“%[^,\n]”,&ch[t++])!=EOF
11.练习11
11.1 题目【注意数据范围】

11.2 代码
#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 注意事项
- sum=sum+a;要在getchar()!='\n’前面
- 数字大小超过 1 0 7 10^7 107时,要用long int 以及%ld
边栏推荐
- 038 network security JS
- Latex formula code
- The triode is a great invention
- [untitled]
- Aardio - Shadow Gradient Text
- Aardio - 自己构造的getIconHandle的方法
- php laravel微信支付
- Thesis learning -- Analysis and Research on similarity query of hydrological time series
- Insufficient executors to build thread pool
- The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
猜你喜欢

Gdip - hatchbrush pattern table

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

Soft keyboard height error

Vhost kick & call principle

Office365 - how to use stream app to watch offline files at any time

Eigen matrix operation Library

Cyclic neural network

The triode is a great invention

Introduction to kubernetes resource objects and common commands (II)
随机推荐
Aardio - Method of self constructed geticonhandle
十大劵商如何开户?另外,手机开户安全么?
Li Kou daily question - day 31 -202 Happy number
getInputStream() has already been called for this request
Source code analysis of open source API gateway APIs IX
【力扣10天SQL入门】Day10 控制流
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
Aardio - 自己构造的getIconHandle的方法
Li Kou daily question - Day 32 -1822 Symbol of array element product
Sorting out tcp/udp communication problems
【入门】输入n个整数,输出其中最小的k个
如何使用layui将数据库中的数据以表格的形式展现出来
[MySQL learning notes 28] storage function
Gdip - hatchBrush图案表
Access report realizes subtotal function
[force deduction 10 days SQL introduction] Day10 control flow
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
php laravel微信支付
[kv260] generate chip temperature curve with xadc
奥迪AUDI EDI 项目中供应商需要了解哪些信息?