PAT (Basic Level) Practice 1001-1005

1001害死人不偿命的(3n+1)猜想

image-20220416090836388

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            int n, s = 0;
            n = int.Parse(Console.ReadLine());
            while (n != 1) {
                // 判断奇数
                if (n % 2 == 1) {
                    n = n * 3 + 1;
                }
                else {
                    // 偶数 n砍掉一半
                    n /= 2;
                    // 次数累加
                    s++;
                }
            }
            Console.WriteLine(s);
        }
    }
}

image-20220416085635938

Java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        while (n != 1) {
            count ++;
            if (n % 2 == 0) {
                n /= 2;
            } else {
                n = (3 * n + 1) / 2;
            }
        }
        System.out.print(count);
    }

}

image-20220416085940613

C

#include <stdio.h>
int main()
{
  int n;
  scanf("%d",&n);
  int sum=0;
  while(n>1){
    if(n%2==0)  n=n/2;
    else  n=(3*n+1)/2;
    sum++;
  }
  printf("%d\n",sum);
  return 0;
}

image-20220416091836797

1002写出这个数

image-20220416101528487

C#

using System;
namespace ConsoleApplication6 {
    class Program {
        static void Main(string[] args) {
            string n = Console.ReadLine();
            int sum = getSum(n);
            int first = 0;//最高位
            int[] bits = getDecBits(sum, ref first);
            outResult(bits,ref first);
        }
        //方法一
        //求字符串数字每一位之和
        static int getSum(string n) {
            int sum = 0, i;
            for (i = 0; i < n.Length; i++) {
                sum += n[i] - '0'; //'9' - '0' = 9
            }
            return sum;
        }
        static int[] getDecBits(int sum, ref int first) {//fist最高位
            int[] bits = new int[10];//估算10个
            first = 0;
            while (sum != 0) {
                bits[first] = sum % 10;
                first++;
                sum /= 10;
            }
            return bits;
        }
        static void outResult(int[] bits, ref int first) {
            string[] py = new string[] { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
            for (int b = first - 1; b > 0; b--) {
                Console.Write("{0} ",py[bits[b]]);
            }
            Console.Write("{0}", py[bits[0]]);
        }
    }
}

image-20220416101147761

Java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            count += input.charAt(i) - '0';
        }
        input = String.valueOf(count);
        String[] strings = new String[] {"ling", "yi", "er",
                "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            if (i != 0) out.append(" ");
            out.append(strings[input.charAt(i) - '0']);
        }
        System.out.printf("%s", out);
    }

}

image-20220416101650374

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        // 求和
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            count += Integer.parseInt(input.substring(i, i +1));
        }
        // 和数,转为字符串
        input = String.valueOf(count);
        String[] pinyin = new String[] {"ling", "yi", "er",
                "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        StringBuilder out = new StringBuilder();
        // 第一个拼音前不加空格
        out.append(pinyin[Integer.parseInt(input.substring(0, 1))]);
        for (int i = 1; i < input.length(); i++) {
            // 每一位的数字
            int number = Integer.parseInt(input.substring(i, i + 1));
            // 拼音前加空格
            out.append(" ").append(pinyin[number]);
        }
        System.out.printf("%s", out);
    }

}

image-20220416112139892

1003我要通过!

image-20220417064800493

image-20220417064913970

分析:

  • 包含PAT三个字符
  • xPATx,x可以表示为空、A、AA、AAA等。如正确样例:AAPATAA
  • 如果aPbTc是正确的,那么aPbATca也是正确的。a,b,c分别可以表示为空、A、AA、AAA等。所以空PAT空是正确的,那么空PbATc空=>空PAATc空=>空PAAT空空是正确的。如正确示例:PAAT;所以APATA是正确的,那么APbATcA=>APAATcA=>APAATAA是正确的,所以APAATAA是正确的,那么APbATcA=>APAAATcA=>APAAATAAA是正确的。如错误示例:APAAATAA;所以AAPATAA是正确的,那么AAPbATcAA=>AAPAATcAA=>AAPAATAAAA是正确的。如正确示例:AAPAATAAAA。

Java

1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            String line = scanner.next();
            // 1.只有PAT这三种字符。2.xPATx是正确的。
            boolean isRight = line.matches("A*PA+TA*");
            // 3.如果aPbTc是正确的,那么aPbATca也是正确的,这里有规律:即a*b=c,这也是测试案例中 APAAATAA为NO的原因
            // 获取P前面A的个数,如果P为0则前面的个数为0,其他同理。
            int a = line.indexOf("P");
            int index = line.indexOf("T");
            // 获取T前面A的个数
            int b = index - a - 1;
            // 获取T后面A的个数
            int c = line.length() - index - 1;
            System.out.println(isRight && (a * b == c) ? "YES" : "NO");
        }
    }
}

2

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int len = Integer.parseInt(in.nextLine());
        String[] strings = new String[len];
        for (int i = 0; i < len; i++) {
            strings[i] = in.nextLine();
        }
 
        int k;
        boolean flag;
 
        for (String s : strings) {
            k = 0;
            flag = true;
            int[] a = new int[3];
            char[] chars = s.toCharArray();
            for (char ch : chars) {
                if (ch == 'A') {
                    a[k]++;
                } else if (ch == 'P' && k == 0) {
                    k = 1;
                } else if (ch == 'T' && k == 1) {
                    k = 2;
                } else {
                    flag = false;
                }
            }
            /**
             * 包含的每个字符必须是p a t中的某一个
             * p在t前
             * p t各一个,至少一个a
             * n0*n1=n2
             */
            if (flag && k == 2 && a[1] >= 1 && a[0] * a[1] == a[2]) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

3

import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n>0){
            n--;
            String s = sc.next();
            //System.out.println(s.length());
            int p = 0;//字符串中P的个数
            int t = 0;//字符串中T的个数
            // aPbTc,a:a中A的个数,下标,b:b中A的个数,下标,c:c中A的个数,下标。
            int a = 0,b=0,c=0;
            int other = 0;//其他字符
            for (int i = 0; i<s.length(); i++){
                if(s.charAt(i) == 'P'){
                    p++;a = i;
                }else if(s.charAt(i) == 'T'){
                    t++;b = i;
                }else if(s.charAt(i) != 'A'){
                    other++;//统计不包含PAT三个字符的其他字符
                }
            }
            if(p!= 1||t!=1 || other > 0 || b-a <=1){//b-a<=1,P和T之间不包含任何字符直接返回 NO
                //System.out.println(p+" "+t+" "+other+" "+b-a+" ");
                //System.out.println(s.length()+" "+p+" "+t+" "+other+" "+(b-a));
                System.out.println("NO");
                continue;
            }
            if( a*(b-a-1) == s.length()-1-b)
                System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

4

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str = "";
        // 右边A的个数
        int rightACount;
        // 中间A的个数
        int centerACount;
        // 左边A的个数
        int leftACount;
        // P的个数
        int pCount;
        // T的个数
        int tCount;
        for (int i = 0; i < n; i++) {
            str = scanner.next();
            rightACount = 0; centerACount = 0; leftACount = 0; pCount = 0; tCount = 0;
            for (int j = 0; j < str.length(); j++) {
                char charAt = str.charAt(j);
                // 检查每一个字符是否仅是pat这三种字符
                boolean b = charAt == 'P' || charAt == 'A' ||
                        charAt == 'T' ;
                if (!b) break;
                // 检查形如xPATx
                if (charAt == 'P') {
                    leftACount = j;
                    pCount++;
                } else if (charAt == 'T') {
                    centerACount = j - leftACount - 1;
                    rightACount = str.length() - j - 1;
                    tCount++;
                }
            }
            // P和T各有一个,中间的A最少一个,中间的A的数量乘以左边的A的数量等于右边A的数量(看输入样例)
            if (pCount == 1 && tCount == 1 && centerACount >= 1 && leftACount * centerACount == rightACount) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

image-20220417105903323

1004成绩排名

image-20220419134858434

Java

1

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String name;
        String id;
        int score;
        // treeMap键按字典顺序排序,默认升序
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            name = scanner.next();
            id = scanner.next();
            score = scanner.nextInt();
            treeMap.put(score, name + " " + id);
        }
        Map.Entry<Integer, String> entry = treeMap.lastEntry();
        System.out.println(entry.getValue());
        entry = treeMap.firstEntry();
        System.out.println(entry.getValue());
    }
}

image-20220420150438208

2

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt(); //键盘输入获取学生个数
        int max = 0, min = 100;   //设置最高分和最低分的初始值
        String maxInfo = "", minInfo = "";
        //nextLine()读取了上一次输入nextInt()留下的“\n”,所以没有给用户输入(软件认为用户输了一个”\n”),而是直接换行了
        scanner.nextLine();//消除换行符影响,提前吞掉空格,防止后面吞掉
        while (number > 0) {
            //使用split()方法将输入的字符串以空格为间隔符将字符串分成字符数组
            String[] str = scanner.nextLine().split(" ");
            //str[2]中存取的是分数字符,转换为整型
            int score = Integer.parseInt(str[2]);
            String info = str[0] + " " + str[1];//将姓名和学号合并一个字符串
            //比较分数,交换分数以及字符串信息
            if (score > max) {
                max = score;
                maxInfo = info;
            }
            if (score < min) {
                min = score;
                minInfo = info;
            }
            number--;
        }
        //输出结果
        System.out.println(maxInfo);
        System.out.println(minInfo);
    }
}

C++

1

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int n, i, j;
    typedef struct Stu
    {
        char name[15];
        char id[15];
        int score;
    };
    Stu stu[10000];
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%s%s%d", stu[i].name, stu[i].id, &stu[i].score);
    }
    int max = 0, maxi = 0, min = 100, mini = 0;
    for(i = 0; i < n; i++)
    {
        if(stu[i].score > max)
        {
            max = stu[i].score;
            maxi = i;
        }
        if(stu[i].score < min)
        {
            min = stu[i].score;
            mini = i;
        }
    }
    printf("%s %s\n", stu[maxi].name, stu[maxi].id);
    printf("%s %s", stu[mini].name, stu[mini].id);
    return 0;
}

2

#include <iostream>
using namespace std;
int main()
{
    string temp1,temp2,max_stu,min_stu;
    int n,score,smax=-1,smin=10000;
    cin>>n;
    while(n--){
        cin>>temp1>>temp2>>score;
        if(score>smax){
            smax = score;
            max_stu = temp1+" "+temp2;
        }
        if(score<smin){
            smin = score;
            min_stu = temp1+" "+temp2;
        }
    }
    cout<<max_stu<<endl;
    cout<<min_stu<<endl;
    return 0;
}

1005继续(3n+1)猜想

image-20220420214035671

Java

1

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        // 关键数字
        Set<Integer> keyNumber = new HashSet<>();
        // 验证数字
        TreeSet<Integer> verificationNumber = new TreeSet<>((m, n) -> n - m);
        for (int i = 0; i < k; i++) {
            int n = scanner.nextInt();
            verificationNumber.add(n);
        }
        for (Integer n : verificationNumber) {
            while (n != 1) {
                if (n % 2 == 0) {
                    n /= 2;
                } else {
                    n = (3 * n + 1) / 2;
                }
                keyNumber.add(n);
            }
        }
        verificationNumber.removeAll(keyNumber);
        boolean flag = true;
        for (Integer verification : verificationNumber) {
            if (flag) {
                flag = false;
                System.out.print(verification);
            } else {
                System.out.print(" " + verification);
            }
        }
    }

}

image-20220424030253742

2

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        // 关键数字
        int[] key = new int[5000];
        // 验证数字
        int[] v = new int[k];
        for (int i = 0; i < k; i++) {
            v[i] = scanner.nextInt();
        }
        for (int j : v) {
            int temp = j;
            while (temp != 1) {
                if (temp % 2 == 0) {
                    temp /= 2;
                } else {
                    temp = (temp * 3 + 1) / 2;
                }
                key[temp] = 1;
            }
        }
        Arrays.sort(v);
        boolean flag = true;
        for (int i = v.length - 1; i >= 0; i--) {
            if (key[v[i]] == 0) {
                if (flag) {
                    System.out.print(v[i]);
                    flag = false;
                } else {
                    System.out.print(" " + v[i]);
                }
            }
        }
    }

}

image-20220424030142338

3

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        // 关键数字
        List<Integer> keyNumber = new ArrayList<Integer>();
        // 验证数字
        List<Integer> verificationNumber = new ArrayList<Integer>();
        int[] n = new int[k];
        for (int i = 0; i < k; i++) {
            n[i] = scanner.nextInt();
        }
        for (int i : n) {
            while (i != 1) {
                if (i % 2 == 0) {
                    i /= 2;
                } else {
                    i = (i * 3 + 1) / 2;
                }
                if (keyNumber.contains(i)) break;
                keyNumber.add(i);
            }
        }
        for (int i : n) {
            if (!keyNumber.contains(i)) {
                verificationNumber.add(i);
            }
        }
        Object[] objects = verificationNumber.toArray();
        Arrays.sort(objects);
        for (int i = objects.length - 1; i > 0; i--) {
            System.out.print(objects[i] + " ");
        }
        System.out.print(objects[0]);

    }

}

image-20220424030100685

End.

谢谢您的阅读!

Copyright 2022 Zichen

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.