2 weixin 40152919 weixin_40152919 于 2017.09.15 12:21 提问

PAT乙级1003.我要通过 50C
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int i=0,n,j;
        int YN=1;  
        int p1=0,t1=0;
        int p2=0,t2=0;
        Scanner input =new Scanner(System.in);
        n=input.nextInt();
        input.nextLine();
        for(j=0;j<n;j++) { 
            String arr=input.nextLine();
            p2=0;t2=0;
            YN=0;
            int flag1=0,flag=0;
            char[] aa=arr.toCharArray();
            for(i=0;i<aa.length;i++){
                if(aa[i]=='P'){
                    p1=i;
                    p2++;
                }
                if(aa[i]=='T'){
                    t1=i;
                    t2++;
                }
                if(aa[i]=='A'||aa[i]=='P'||aa[i]=='T') flag1++;
            }
            if(flag1==aa.length) flag++;
            if(p1*(t1-p1-1)==(aa.length-1-t1)) flag++;
            if(p2==1&&t2==1) flag++;
            if((t1-p1)>=2) flag++;
            if(flag==4) System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

为什么全错?不至于吧?我为什么错了?

4个回答

qq_26433213
qq_26433213   2017.09.15 12:30
 #include <cstdio>
#include <cstring>
int main() {
    int n;
    char str[110];
    scanf("%d", &n);
    while (n--) {
        int flag = 0, pcount = 0, tcount = 0, pindex = -1, tindex = -1;
        scanf("%s", str);
        int len = strlen(str);
        for (int i = 0; i < len; ++i) {
            if (str[i] != 'P' && str[i] != 'A' && str[i] != 'T') {
                flag = 1;
                break;
            }
            if (str[i] == 'P') {
                pindex = i;
                pcount++;
                if (pcount > 1) {flag = 1; break;}  //若P的数量大于一个则必定为NO,T下同。
            }
            if (str[i] == 'T') {
                tindex = i;
                tcount++;
                if (tcount > 1) {flag = 1; break;}
            }

        }
        if ( (len - tindex - 1) - (pindex) * (tindex - pindex - 2) != (pindex) || (tindex - pindex - 2 < 0)) {flag = 1;}
        if (flag)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}
qq_26433213
qq_26433213   2017.09.15 12:31
qq_26433213
qq_26433213   2017.09.15 12:33
shen_wei
shen_wei   Ds   Rxr 2017.09.15 15:18
Csdn user default icon
上传中...
上传图片
插入图片