团队程序设计天梯赛模拟赛里的题,好像没在题单上看到
n个样例
输入一行话,如果不是问句,就输出enen
如果是问句,如果里面含有PTA,就输出"Yes",否则输出"No"
因为用scanf输入遇到空格会停止输入,所以考虑用getchar()
可以搞个结构体来存储不同的字符串,这样每次存字符串就不用重置了
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
const int N=100010;
struct Node{
char s[100];
}q[N];
int cnt;
int main()
{
int n;
cin>>n;
getchar();
char ch;
for(int i=0;i<n;i++){
cnt=0;
while((ch=getchar())!='\n'){
q[i].s[cnt++]=ch;
}
int len=strlen(q[i].s);
if(q[i].s[len-1]!='?'){
printf("enen\n");
}
else{
bool flag=false;
for(int j=0;j<len-3;j++){
if(q[i].s[j]=='P'&&q[i].s[j+1]=='T'&&q[i].s[j+2]=='A'){
if(!flag) printf("Yes!\n");
flag=true;
}
}
if(!flag) printf("No.\n");
}
}
return 0;
}
未重置字符串,也可以一个字符一个字符输出,但要控制好输出字符的数量
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int cnt;
char s[100];
int main()
{
int n;
cin >> n;
getchar();//用于吸收换行符(输入n后按下的回车)
char ch;
while (n--) {
cnt = 0;
while ((ch = getchar()) != '\n') {
s[cnt++] = ch;
}
printf("一个字符一个字符控制好数量输出:");
for (int i = 0; i < cnt; i++) {
printf("%c", s[i]);
}
printf("\n");
printf("直接输出整个字符串:%s\n", s);
}
return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int cnt;
char s[100];
int main()
{
int n;
cin >> n;
getchar();
char ch;
while (n--) {
cnt = 0;
while ((ch = getchar()) != '\n') {
s[cnt++] = ch;
}
printf("一个字符一个字符未控制好数量输出:");
for (int i = 0; i < strlen(s); i++) {
printf("%c", s[i]);
}
printf("\n");
printf("直接输出整个字符串:%s\n", s);
}
return 0;
}
重置字符串char s[100],不能用等号赋值,得用字符串函数strcpy
使用string s可以用等号赋值,但是不能给某个元素字符赋值
可以用memset重置字符串(char s[100]可以用memset(s,0,sizeof s)来重置,string s不能)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int cnt;
char s[100];
int main()
{
int n;
cin >> n;
getchar();
char ch;
while (n--) {
memset(s, 0, sizeof s);
cnt = 0;
while ((ch = getchar()) != '\n') {
s[cnt++] = ch;
}
printf("直接输出整个字符串:%s\n", s);
}
return 0;
}
奇怪的是,用strcpy重置字符串却会出错
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int cnt;
char s[100];
int main()
{
int n;
cin >> n;
char ch;
while (n--) {
strcpy(s, "");
cnt = 0;
ch = getchar();
s[cnt] = ch;
while ((ch = getchar()) != '\n') {
s[++cnt] = ch;
}
printf("\n");
printf("直接输出整个字符串:%s\n", s);
}
return 0;
}
这让我百思不得其解,为此,我决定跟踪一下它的轨迹
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
using namespace std;
int cnt;
char s[100];
int main()
{
int n;
cin >> n;
getchar();
char ch;
while (n--) {
strcpy(s, "");
printf("重置后字符串:%s\n", s);
printf("s[0]=%c,s[1]=%c,s[2]=%c,s[3]=%c,s[4]=%c,s[5]=%c,s[6]=%c,s[7]=%c\n", s[0],s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
cnt = 0;
while ((ch = getchar()) != '\n') {
s[cnt++] = ch;
}
printf("输入后字符串:%s\n", s);
printf("s[0]=%c,s[1]=%c,s[2]=%c,s[3]=%c,s[4]=%c,s[5]=%c,s[6]=%c,s[7]=%c\n", s[0],s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
}
return 0;
}
发现strcpy确实将字符串重置了,但是它的字符元素除了首元素被清空了,其它元素仍在,看来strcpy这一字符串函数是对整个字符串进行操作的,而其中的字符元素是不变的(除了首元素)