当前位置:网站首页>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
边栏推荐
- Differential: definition of total differential, partial derivative, gradient
- How to get a SharePoint online site created using the office365 group template
- Gdip - hatchBrush图案表
- Aardio - Shadow Gradient Text
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- 奥迪AUDI EDI 项目中供应商需要了解哪些信息?
- 【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
- PWN attack and defense world int_ overflow
- H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
- php laravel微信支付
猜你喜欢

2022 test questions and mock examinations for main principals of hazardous chemicals business units

Android screen adaptation (using constraintlayout), kotlin array sorting

凸印的印刷原理及工艺介绍

Thesis learning -- Analysis and Research on similarity query of hydrological time series

Software testing methods and techniques - overview of basic knowledge

三极管是一项伟大的发明

Eigen matrix operation Library

【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion

2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform

软件测试方法和技术 - 基础知识概括
随机推荐
[R language] two /n data merge functions
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
Aardio - Method of self constructed geticonhandle
EDA开源仿真工具verilator入门6:调试实例
Aardio - 阴影渐变文字
web254
力扣每日一题-第32天-1822.数组元素积的符号
Gdip - hatchBrush图案表
Aardio - [problem] the problem of memory growth during the callback of bass Library
[introduction] approximate value
Scala language learning-07-constructor
[getting started] input n integers and output the smallest K of them
IMDB practice of emotion classification (simplernn, LSTM, Gru)
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Cyclic neural network
【mysql学习笔记26】视图
Cmake I two ways to compile source files
Sorting out tcp/udp communication problems
[force deduction 10 days SQL introduction] Day10 control flow