C++ char 转 string处理,提升速度,快速处理!
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100;
char str1[N], str2[N];
string s1, s2;
int main()
{
scanf("%s%s",str1,str2);
s1 = str1, s2= str2;
cout<<s1<<endl<<s2<<endl;
//只能转为string类型处理,如果想输出sting类型,还是得用到cout,这样速度就又慢了。
// printf("%s\n%s\n",s1,s2);
return 0;
}
c++中 字符串的分隔符如何处理
1、先读取一整行,然后用sscanf()读取
如下题目:
地址: 航班时间
代码如下:
// 21:32~
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int get_second(int h, int m, int s)
{
return h*3600 + m *60 + s;
}
int get_time()
{
int h1, m1, s1, h2, m2, s2, d;
string ss;
getline(cin, ss);
if(ss.back() != ')') ss += " (+0)";
sscanf(ss.c_str(),"%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
return (get_second(h2, m2, s2) - get_second(h1, m1, s1) + d * 24 * 3600);
}
int main()
{
int n;
scanf("%d", &n);
string line;
getline(cin, line);
while(n --)
{
int time1 = get_time(), time2 = get_time(), time = (time2 + time1)/2;
printf("%02d:%02d:%02d\n",time/3600, time%3600/60, time%60);
}
return 0;
}