//1003 我要通過! (20分)
pta上的一道題

#include<stdio.h>
#include<string.h>
int main()
{
char c;
int num;
scanf("%d", &num);
while(getchar() != '\n'); //來清除scanf快取中的垃圾資料
int i=0;//count表示A分別在幾個位置 的數量
char pri[num][3];
for(i=0;i<num;i++)
{
int pos=0,count[3]={0,0,0};//,count[3]={0,0,0}不能放上面
while((c=getchar())!='\n'){
if(c=='A') count[pos]++;
else if(c=='P'&&pos==0) pos=1;
else if(c=='T'&&pos==1) pos=2;
else break;
}
if(c=='\n'&&pos==2&&count[1]&&count[2]==count[1]*count[0]){
strcpy(pri[i],"YES");
}
else {
strcpy(pri[i],"NO");
}
if(c!='\n')
while(getchar()!='\n'); //來清除getchar快取中的垃圾資料
}
for(int j=0;j<num;j++){
printf("%s\n",pri[j]);
}
return 0;
}
我在輸出是出現問題

前4ge輸出應該只有一個YES,不知道為什么會這么多
uj5u.com熱心網友回復:
修改如下,供參考:#include<stdio.h>
#include<string.h>
int main()
{
char c;
int num;
scanf("%d", &num);
while(getchar() != '\n');
int i=0;
char pri[11][4];//char pri[num][3];這個陣列設定錯誤導致
for(i=0;i<num;i++)
{
int pos=0,count[3]={0};//count[3]={0,0,0};//,count[3]={0,0,0}不能放上面
while((c=getchar())!='\n'){
if(c=='A') count[pos]++;
else if(c=='P'&&pos==0) pos=1;
else if(c=='T'&&pos==1) pos=2;
else break;
}
if(c=='\n'&&pos==2&&count[1]&&count[2]==count[1]*count[0]){
strcpy(pri[i],"YES");
}
else {
strcpy(pri[i],"NO");
}
if(c!='\n')
while(getchar()!='\n');
}
for(int j=0;j<num;j++){
printf("%s\n",pri[j]);
}
return 0;
}
//8
//PAT
//PAAT
//AAPATAA
//AAPAATAAAA
//xPATx
//PT
//Whatever
//APAAATAA
//YES
//YES
//YES
//YES
//NO
//NO
//NO
//NO
//請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/261880.html
標籤:C語言
下一篇:Qt制作視頻播放器遇到的問題
