hdoj 1048
涉及读取一行字符的情况(包括空格),有两个函数:
①
<span style="font-family:Verdana;font-size:18px;">char a[100];
fgets(a,100,stdin);
int l=strlen(a);
for(int i=0;i<n;i++)
cout<<a[i];
</span>
fgets()这个函数会将换行符一起保存在字符串中,如果fgets(a,4,stdin),而输入了1234的话,只会保存123空格,按上述方法,别管最后的换行符,能得到你想要的!
②
gets()这个函数遇到换行符结束,但不会保存换行符到数组中,当然它没有输入大小的限制,所以数组要尽量开大才能保证不溢出。
最后,在oj上做题的时候,如果遇到先输入测试用例个数再读取一行字符的时候,切记先用getchar()将换行符读掉。
贴出hdoj 1048的代码吧。
ps:今日开始了动归的训练,结果一天都耗在1003上了,T^T,加油少年,明天争取ac两个动归!
<span style="font-family:Verdana;font-size:18px;">#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[200];
while(gets(a))
{
getchar();
if(strcmp(a,"ENDOFINPUT")==0)break;
if(strcmp(a,"START")==0)
{
gets(a);
int l=strlen(a);
for(int i=0;i<l;i++){
if(a[i]>=65&&a[i]<=90){
int s=a[i]+21;
if(s>=65&&s<=90)
cout<<char(a[i]+21);
else
cout<<char(a[i]-5);
}
else
cout<<a[i];
}
cout<<endl;
}
if(strcmp(a,"END")==0)
continue;
}
return 0;
}
</span>