13省7- 错误票据
link.
这个题目的难点在多处对字符串的处理,
如输入多行数,并对换行符进行处理(getchar()一定不能少),对输出的字符串进行分割(c++没有写好的split函数,需要自己实现),把字符串转成整型数处理(s2i()模板)
把输入分割后的数存储在数组里,数组下标表示这个数,对应的值表示这个数出现的次数
#include<bits/stdc++.h>
using namespace std;
int id[10001]={0};
int main(){
int line,mi=100000,mx=0;
cin>>line;
getchar();//多行输入
for(int i=0;i<line;i++){
string a;
getline(cin,a);//输入一行a
stringstream ss(a);//把a的空格去掉,放入ss流
// ss<<a;
int idx;
while(ss>>idx){//ss字符串转换为int
id[idx]++;
if(mi>idx) mi=idx;
if(mx<idx) mx=idx;
}
}
for(int i=mi;i<=mx;i++){
if(id[i]==0) cout<<i<<" ";
if(id[i]==2) cout<<i<<endl;
}
return 0;}
getchar();获取一个无符号字符
getline(cin, inputLine),其中 cin 是正在读取的输入流,而 inputLine 是接收输入字符串的 string 变量的名称。需要注意的是,它不是类方法。
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
string name;
string city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Enter the city you live in: ";
getline(cin, city);
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}
查了一些资料,到此,我发现了一个巨好用的模板
string字串去掉空格,string类型转其他类型
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main(){
string str="i am a boy"; //一个string类字符串变量str
istringstream is(str); //把str变量去掉空格绑定is流
string s; //定义一个string变量
while(is>>s) { //把
cout<<s<<endl;
}
}
似乎只要那个while里面>>s,s是什么类型的就可以这样转换成什么类型
#include<bits/stdc++.h>
//#include<sstream> //istringstream 必须包含这个头文件
//#include<string>
using namespace std;
int main(){
string str="1 2 3 4"; //一个string类字符串变量str
stringstream is(str); //把str变量去掉空格绑定is流
double s; //定义一个string变量
while(is>>s) { //把空格转s
printf("%f\n",s);
}
}