c语言数组问题求教!!!

鱼鱼鱼的记录 2021-02-21 03:06:13
//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,不知道为什么会这么多
...全文
953 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
m0_55844471 2021-03-07
  • 打赏
  • 举报
回复
学习学习使我快乐
鱼鱼鱼的记录 2021-03-07
  • 打赏
  • 举报
回复
引用 2 楼 自信男孩 的回复:
[code=c]#include<stdio.h> #include<string.h> int main() { #if 1 char buf[100]; int num, cnt; int i, j; scanf("%d", &num); while (getchar() != '\n') ; for (i = 0; i < num; i++) { fgets(buf, sizeof(buf), stdin); cnt = 0; for (j = 0; buf[j] != '\n' && buf[j]; j++) { if (buf[j] == 'P' && cnt == 0) cnt++; if (buf[j] == 'A' && cnt == 1) cnt++; if (buf[j] == 'T' && cnt == 2) break; } if (cnt >= 2) printf("YES\n"); else printf("NO\n"); } #else 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]); } #endif return 0; } [quote=引用 6 楼 weixin_45544561 的回复:][quote=引用 楼主 想学精编程的人的回复:]//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,不知道为什么会这么多
你好,楼主。我也爱好c语言。不过是初学者。我想问下,你提到的PTA是什么?是一个能做题的学习网站吗?还是什么呀?能告诉我吗?[/quote]你直接搜就行,可以做题的
快乐胖电工 2021-02-27
  • 打赏
  • 举报
回复
小白路过,学习了
weixin_45544561 2021-02-26
  • 打赏
  • 举报
回复
引用 楼主 想学精编程的人的回复:
//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,不知道为什么会这么多
你好,楼主。我也爱好c语言。不过是初学者。我想问下,你提到的PTA是什么?是一个能做题的学习网站吗?还是什么呀?能告诉我吗?
鱼鱼鱼的记录 2021-02-24
  • 打赏
  • 举报
回复
引用 4 楼 qzjhjxj的回复:
[quote=引用 3 楼 想学精编程的人 的回复:][quote=引用 1 楼 qzjhjxj 的回复:]修改如下,供参考:
#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
//请按任意键继续. . .
我发现把这里//char pri[num][3];这个数组设置错误导致//里面的3改成4就能成功了,请问能解答一下原因吗[/quote] "YES" "NO"都是字符串,系统在保存时,自动会在串尾加上结束标志'\0',所以字符串"YES\0"的长度是4,"NO\0" 的长度为3。当把字符串数组char pri[num][3]长度设置为3时,保存时串尾结束标志就丢了,字符数组就是这样的: pri[num][3]={"YES","YES","YES","YES","NO\0","NO\0","NO\0","NO\0"},当执行 printf("%s\n",pri[j]);的时候, 输出字符串时以'\0'作为结束标志,所以pri[0][3]= "YESYESYESYESNO",pri[1][3]="YESYESYESNO",...。正确的 pri[num][4]={"YES\0","YES\0","YES\0","YES\0","NO\0","NO\0","NO\0","NO\0"},输出时自然就对了。[/quote] 牛逼懂了谢谢
qzjhjxj 2021-02-23
  • 打赏
  • 举报
回复
引用 3 楼 想学精编程的人 的回复:
[quote=引用 1 楼 qzjhjxj 的回复:]修改如下,供参考:
#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
//请按任意键继续. . .
我发现把这里//char pri[num][3];这个数组设置错误导致//里面的3改成4就能成功了,请问能解答一下原因吗[/quote] "YES" "NO"都是字符串,系统在保存时,自动会在串尾加上结束标志'\0',所以字符串"YES\0"的长度是4,"NO\0" 的长度为3。当把字符串数组char pri[num][3]长度设置为3时,保存时串尾结束标志就丢了,字符数组就是这样的: pri[num][3]={"YES","YES","YES","YES","NO\0","NO\0","NO\0","NO\0"},当执行 printf("%s\n",pri[j]);的时候, 输出字符串时以'\0'作为结束标志,所以pri[0][3]= "YESYESYESYESNO",pri[1][3]="YESYESYESNO",...。正确的 pri[num][4]={"YES\0","YES\0","YES\0","YES\0","NO\0","NO\0","NO\0","NO\0"},输出时自然就对了。
鱼鱼鱼的记录 2021-02-23
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj 的回复:
修改如下,供参考:
#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
//请按任意键继续. . .
我发现把这里//char pri[num][3];这个数组设置错误导致//里面的3改成4就能成功了,请问能解答一下原因吗
自信男孩 2021-02-22
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>

int main()
{


#if 1
char buf[100];
int num, cnt;
int i, j;


scanf("%d", &num);

while (getchar() != '\n')
;

for (i = 0; i < num; i++) {
fgets(buf, sizeof(buf), stdin);

cnt = 0;
for (j = 0; buf[j] != '\n' && buf[j]; j++) {
if (buf[j] == 'P' && cnt == 0)
cnt++;
if (buf[j] == 'A' && cnt == 1)
cnt++;
if (buf[j] == 'T' && cnt == 2)
break;
}
if (cnt >= 2)
printf("YES\n");
else
printf("NO\n");
}

#else
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]);
}
#endif
return 0;
}

供参考~
qzjhjxj 2021-02-21
  • 打赏
  • 举报
回复
修改如下,供参考:
#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
//请按任意键继续. . .

69,322

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧