本文整理ACM模式的各种输入形式。
示例:
方法1
#include
#include
using namespace std;
int main(){
int n;
cin >> n;
vector nums(n);
for (int i = 0; i < n; ++i){
cin >> nums[i];
}
//测试:打印数组
cout<<"test c++ input:"<
方法2
#include
#include
using namespace std;
int main(){
int n;
cin >> n;
vector nums;
nums.resize(n);
for (int i = 0; i < n; ++i){
cin >> nums[i];
}
//测试:打印数组
cout<<"test c++ input:"<
方法3
#include
#include
using namespace std;
int main(){
int n;
cin >> n;
vector nums;
for (int i = 0; i < n; ++i){
int val;
cin >> val;
nums.push_back(val);
}
//测试:打印数组
cout<<"test c++ input:"<
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1 2 3
test c++ input:
1 2 3
方法1
#include
#include
using namespace std;
int main(){
vector nums;
int num;
while(cin>>num){
nums.push_back(num);
if(getchar() == '\n')
break;
}
//测试:打印数组
cout<<"test c++ input:"<
方法2
#include
#include
using namespace std;
int main(){
//代码通过cin.get()从缓存中读取一个字节,这样就扩充了cin只能用空格和TAB两个作为分隔符。
vector nums;
int num;
while(cin>>num){
nums.push_back(num);
if(cin.get() == '\n')
break;
}
//测试:打印数组
cout<<"test c++ input:"<
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3 4
test c++ input:
1 2 3 4
示例:
#include
#include
using namespace std;
int main(){
int m;
cin >> m;
char sep;
vector nums(m);
for (int i = 0; i < m - 1; ++i){
cin >> nums[i] >> sep;
}
cin >> nums[m - 1];
//测试:打印数组
cout<<"test c++ input:"<
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1,2,3
test c++ input:
1 2 3
示例:
方法
#include
#include
using namespace std;
int main(){
string str;
vector strs;
while (cin >> str) {
strs.push_back(str);
if (getchar() == '\n') { //控制测试样例
for (auto& str : strs) {
cout << "current character:"<< str << " ";
}
cout << endl;
strs.clear();
}
}
return 0;
}
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
aaa bbb
current character:aaa current character:bbb
方法:使用getline 读取一整行字符串到字符串input中,然后使用字符串流stringstream,读取单个数字或者字符。每个字符中间用','间隔
#include
#include
#include
#include
using namespace std;
int main(){
string input;
while (getline(cin, input)) { //读取一行
vector strs;
string str;
stringstream ss(input);
while(getline(ss, str,',')){
strs.push_back(str);
}
sort(strs.begin(), strs.end());
for (auto& str : strs) {
cout << str << " ";
}
cout << endl;
}
return 0;
}
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
d,v,b,c,f
b c d f v
#include
#include
#include
using namespace std;
int main(){
string input;
while (getline(cin, input)) { //读取一行
stringstream data(input); //使用字符串流
int num = 0, sum = 0;
while (data >> num) {
sum += num;
}
cout << sum << endl;
}
return 0;
}
正确性测试:
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3
6
示例(假定每行输入5个数)
3
1,2,3,3,2
4,5,6,1,3
7,8,9,3,4
#include
using namespace std;
int main()
{
int n;
cin >> n;
vector> vec(n,vector(5));
for (int i = 0; i < n;++i){//注意这里一定要有i控制,用while容易一直输入导致错误
string s;
cin >> s;
replace(s.begin(), s.end(), ',', ' ');
istringstream input(s);
string tmp;
for (int j = 0; j < 5;++j){//内层循环也很重要
input >> tmp;
vec[i][j] = stoi(tmp);
}
}
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[0].size(); j++)
{
cout << "postion "<< i << j << " output is:"<< vec[i][j] << endl;
}
}
return 0;
}
正确性测试
C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
2
1,2,3,4,5,
6,7,8,9,0
postion 00 output is:1
postion 01 output is:2
postion 02 output is:3
postion 03 output is:4
postion 04 output is:5
postion 10 output is:6
postion 11 output is:7
postion 12 output is:8
postion 13 output is:9
postion 14 output is:0
Process finished with exit code 0
相对于leetcode模式,ACM模式需要自己写输入输出,但是常见输入输出也是几类。熟悉后,就不会在这方面浪费宝贵的时间了。