wust_Q 2022-11-23 10:14 采纳率: 0%
浏览 11

PAT乙级1003 测试点2

PAT乙级1003 测试点2 不通过 JAVA
原题链接:https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805323154440192

img

img

我的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class pat1003 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int firstP, lastT, len;
        String str;
        String first_str, medium_str, last_str;
        while (n-- > 0) {
            str = br.readLine();
            len = str.length();

            if (str.equals("PAT")) {
                System.out.println("YES");
                continue;
            }

            if (str.contains("P") && str.contains("T")) {
                firstP = str.indexOf('P');
                lastT = str.indexOf('T');
                first_str = str.substring(0, firstP);
                medium_str = str.substring(firstP + 1, lastT);
                last_str = str.substring(lastT + 1, len);

                if (OnlyA(first_str) && OnlyA(medium_str) && OnlyA(last_str) && medium_str.contains("A")
                        && first_str.length() * medium_str.length() == last_str.length()) {
                    System.out.println("YES");
                    continue;
                }
            }
            System.out.println("NO");
        }
        br.close();
    }

    public static boolean OnlyA(String s) {
        if (s.equals(""))
            return true;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != 'A')
                return false;
        }
        return true;
    }
}


  • 写回答

2条回答 默认 最新

  • honestman_ 2022-11-23 10:57
    关注
    import java.util.Scanner;
    public class Main{
        public static void main(String[] args){
            Scanner scan = new Scanner(System.in);
            int num =scan.nextInt();
            String str[] =new String[num];
            for(int i =0;i<num;i++) {
                str[i]=scan.next();
                
            }
            for(int i=0;i<num;i++) {
                TrueFalse(str[i]);
            }
            
                
            
        }
        public static void TrueFalse(String s) {
            int numa=0;
            int nump=0;
            int numt=0;
            int destp=0;
            int destt=0;
                for(int j = 0;j<s.length();j++){
                   
                    if(s.charAt(j)=='A'){
                        numa+=1;
                    }else if(s.charAt(j)=='P'){
                        nump+=1;
                        destp=j;
                    }else if(s.charAt(j)=='T'){
                        numt+=1;
                        destt=j;
                    }
                    
                }
                if(numa+nump+numt==s.length()&&nump==1&&numt==1&&destt-destp>1 && numa!=0&& destp*(destt-destp-1)==s.length()-destt-1) {
                    System.out.println("YES");
                }else {
                    System.out.println("NO");
                }
            
        }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月23日

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题