2019/2/21每日编程-1003 我要通过!-1004 成绩排名

1003 我要通过! (20 分)

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式:

每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。

输出格式:

每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES,否则输出 NO

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO

思路:条件一最基本,先忽略。条件二,xPATx 就是正确的形式;在此基础上,如果 aPbTc 是正确的,那么 aPbATca 也是正确的。如果二者放在一起考虑,就是中间每增加一个A,后面就增加一个a,似乎数学关系出来了。我们知道aPbTc中a b c 段都只能包含“A",其长度分别为len(a)、len(b)、len(c),则其关系满足len(a)*len(b) = len(c)!这完美的契合了条件二与条件三,xPATx
 就是当len(b) = 1,(a=x,c=c,b=A)的情况,在此基础上演化到条件三B中每增加一个A,c中相应增加一段”a“以上的乘法关系式成立。
 

import java.util.Scanner;    
public class Main{    
  public static void main(String[] args){    
      Scanner sc = new Scanner(System.in);    
      int n = sc.nextInt();  
      sc.nextLine();  
      for(int i = 0 ;i < n ;i++){  
          String s = sc.nextLine();  
          String news = s;  
          if(news.contains("P")&&news.contains("A")&&news.contains("T")){  
              news = news.replace("A", "");  
              news = news.replace("P", "");  
              news = news.replace("T", "");  
              news = news.replace("\\s+", "");  
              if(news.isEmpty()){  
                  int p = s.indexOf("P");  
                  int t = s.indexOf("T");  
                  int len = s.length();  
                  int b = t - p - 1;  
                  int c = len - t - 1;  
                  if(p*b == c){  
                      System.out.println("YES");  
                  }else{  
                      System.out.println("NO");  
                  }  
              }else{  
                  System.out.println("NO");  
              }  
          }else{  
              System.out.println("NO");  
          }  
      }  
  }  
}

1004 成绩排名 (20 分)

读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:

每个测试输入包含 1 个测试用例,格式为

第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

其中姓名学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:

对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。


/

* 用结构体实现 */
#include <stdio.h>

typedef struct
{  /* 最多10个字符加上'\0' */
    char name[11];
    char id[11];
    int  score;
}Stu;

int main ()
{   
  int i = 0;
  int val = 0;
  Stu max, min, temp;
  max.score = -1;
  min.score = 101;

  if(scanf("%d", &val)!=EOF)
  {
    for (i=0; i<val; i++)
    {
        if(scanf ("%s %s %d", temp.name, temp.id, &temp.score)!=EOF)
        {
          if (temp.score < min.score)
          {
              min = temp;
          } 
          if (temp.score > max.score)
          {
              max = temp;
          } 
        }
        else
          return -1;
    }
      printf ("%s %s\n",max.name, max.id);
      printf ("%s %s\n",min.name, min.id);
      return 0;
  }
  return -1;
}

猜你喜欢

转载自blog.csdn.net/weixin_39338645/article/details/87862462