0%

面试之ACM输入输出

https://blog.csdn.net/weixin_30399797/article/details/95473860?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.pc_relevant_paycolumn_v3&spm=1001.2101.3001.4242.1&utm_relevant_index=3

一、四种基本输入形式

1.一组输入数据,一行,用空格隔开

C++语法:

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
int a ,b;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}

注意:程序的输入输出必须用stdin(Standard Input) 和stdout (Standard Output),C++就用cin输入,用cout输出。

2. 多组输入数据,不说明多少组,直到读至输入文件末尾为止

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
int a ,b;
while (cin>>a>>b)
cout<<a+b<<endl;
return 0;

3. 多组输入数据,不说明多少组,以某特殊输入为结束标志。

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
int a ,b;
while(cin>>a>>b&&(a||b)) //当a和b都为0时,跳出循环
{cout<<a+b<<endl;}
return 0;

4. 多组输入数据,开始输入一个N,接下来是N组数据

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{
int a ,b,n;
cin>>n
while(n--)
{
cin>>a>>b;
cout<<a+b<<endl;
}
return 0;
}

二、字符串输入

单个字符串

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
cout << s;
}
//输入:abcd
//输出:abcd

多个字符串,例如一个英语句子

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin,s);//读取一整行,如果用cin直接读取,会在遇到第一个空格时就停止
cout << s << endl;
}
//输入:I love you
//输出:I love you

常用api

字母小写tolower(char)

1
2
char a;
a = tolower(getchar());