PAT (Basic Level) Practice (中文)有关字符串函数的部分题

考察点

考察字符串相关知识,如逆转、字母与数字的判断与转化、字符串拼接、字符串比较

1002

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String s=sc.nextLine();
        int n = 0;
        for (int i = 0; i <s.length() ; i++) {
            n=n+(s.charAt(i)-'0');//计算和
        }
        String [] py ={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
        String s2= String.valueOf(n);//将int类型转为String类型
        char c[] = s2.toCharArray();//再把String类型转为字符数组
        for (int i = 0; i<c.length-1 ; i++) {
            System.out.print(py[c[i]-'0']+" ");//输出拼音
        }
        System.out.print(py[c[c.length-1]-'0']);//最后一个拼音做特殊处理,因为不能输出有空格
    }
}

1006

ublic class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int B = n/100%10; //百位
        int S = n/10%10;//十位
        int g = n%10;//个位
        for (int i = 0; i <B ; i++) {
            System.out.print("B");
        }
        for (int i = 0; i <S ; i++) {
            System.out.print("S");
        }
        for (int i = 1; i <=g ; i++) {
            System.out.print(i);
        }

    }
}

1009

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s=scanner.nextLine();
        String a[] =s.split(" ");//以空格分割成字符串数组
        for (int i = a.length-1; i >0 ; i--) {//反向遍历输出
            System.out.print(a[i]+" ");
        }
        System.out.print(a[0]);//特殊处理,输出最后一个
    }
}

1014

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //将周几导进数组
        String [] weeks = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
        //每行字符串的输入
        String l1 =sc.nextLine();
        String l2 =sc.nextLine();
        String l3 =sc.nextLine();
        String l4 =sc.nextLine();
        //判断是否找到周几和小时
        boolean isFindWeek = false;
        boolean isFindHour = false;
        //遍历字符串,寻找周几和小时
        for (int i = 0; i <l1.length()&&i<l2.length() ; i++) {
            char c1 = l1.charAt(i);
            char c2 = l2.charAt(i);
            //如果找到相同的字符,再判断前提条件
            //先找周几
            if (c1 == c2) {
                //没有找到周末,并且字符在题目的范围内
                if (!isFindWeek && c2 >= 'A' && c2 <= 'G') {
                    System.out.print(weeks[c2 -'A'] + " ");
                    isFindWeek = true;
                    continue;
                }
                //寻找小时
                if (!isFindHour && isFindWeek) {
                    if (c2 <= '9' && c2 >= '0') {
                        System.out.printf("%02d:", c2 - '0');
                        isFindHour = true;
                    } else if (c2 >= 'A' && c2 <= 'N') {
                        System.out.print((c2 - 55) + ":");
                        isFindHour = true;
                    }
                }
            }
        }
        //寻找分钟
        for (int i = 0; i <l3.length()&&i<l4.length() ; i++) {
            char c3 = l3.charAt(i);
            char c4 = l4.charAt(i);
            if (c3 == c4) {
                if (c3 >= 'A' && c3 <= 'Z' || c3 >= 'a' && c3 <= 'z') {
                    System.out.printf("%02d", i);
                }
            }
        }
    }
}

1021

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int a [] = new int[10];//工具数组,用来计算对应数字出现的次数
        int n=0;
        for (int i = 0; i <s.length() ; i++) {
            n = s.charAt(i)-'0';//将字符串转为int类型的数字
            a[n]++;//次数
        }
        for (int i = 0; i <10 ; i++) {
            if(a[i]!=0){
                System.out.println(i+":"+a[i]);
            }
        }
    }
}

1039

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] tz = br.readLine().toCharArray();//卖家的珠子
        char[] mj = br.readLine().toCharArray();//买家需要的珠子
        //判断是否匹配
        boolean [] sell = new boolean[tz.length];//判断卖家的珠子是否已经匹配
        boolean [] buy  = new boolean[mj.length];//判断买家需要的珠子是否已经匹配

        int lack=0;//计算缺少多少个珠子

        //遍历买家需要的珠子,在卖家的珠子中寻找
        for (int i = 0; i <mj.length; i++) {
            for (int j = 0; j <tz.length ; j++) {
                //如果卖家的珠子还没被匹配,并且这个珠子和买家需要的珠子相同,那么就匹配上
                if (sell[j] == false && tz[j] == mj[i]) {
                    sell[j] = buy[i] = true;//表示已经匹配
                    break;
                }
            }
                if(buy[i] == false){//如果在卖家的珠子中找完了,还没匹配上,说明缺少珠子
                    lack++;
                }
            }
        if(lack>0){//如果缺少珠子
            System.out.println("No"+" "+lack);
        }else {
            System.out.println("Yes"+" "+(tz.length-mj.length));
        }
     }
}

1043

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int [] PATest = new int [1000];//用来作输出的数组
        char[] c = sc.next().toCharArray();//输入的字母数组
        for (int i = 0; i <c.length ; i++) {
            PATest[c[i]]++;//记录每个字母的个数
        }
        while(PATest['P']>0||PATest['A']>0||PATest['T']>0||PATest['e']>0||PATest['s']>0||PATest['t']>0){
            if(PATest['P']-->0) System.out.print("P");
            if(PATest['A']-->0) System.out.print("A");
            if(PATest['T']-->0) System.out.print("T");
            if(PATest['e']-->0) System.out.print("e");
            if(PATest['s']-->0) System.out.print("s");
            if(PATest['t']-->0) System.out.print("t");
        }
    }
}

1054

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String [ ] s= br.readLine().split(" ");
        double sum =0;
        int count =0;
        for (int i = 0; i <n ; i++) {
            try {
                double x = Double.parseDouble(s[i]);//转变成double型
                int width = 0;
                if (s[i].contains(".")) {
                    width = s[i].length() - s[i].indexOf('.') - 1;
                }
                if (x < -1000 || x > 1000 || width > 2) {
                    throw new NumberFormatException();
                }
                sum += x;
                count++;
            } catch (NumberFormatException e) {
                System.out.println("ERROR: " + s[i] + " is not a legal number");
            }
        }
            if(count==0){
                System.out.println("The average of 0 numbers is Undefined");
            }else if(count==1){
                System.out.printf("The average of 1 number is %.2f",sum);
            }else {
                System.out.printf("The average of %d number is %2f",count,sum/count);
            }
        }
    }

1079

mport java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static BigDecimal add(String a,String b){//构造一个两数相加函数 BigDecimal类可以避免误差
        BigDecimal c =new BigDecimal(a);
        BigDecimal d =new BigDecimal(b);
        return c.add(d);
    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String s =scanner.nextLine();
        BigDecimal sum ;
        int count = 0;//计数
        while(true){
            String s1 ="";//工具串
            for (int i = s.length()-1; i >=0 ; i--) {
                s1 = s1 + s.charAt(i) + "";//倒序
            }
            if(s.equals(s1)){//判断是否是回文数
                System.out.println(s+" is a palindromic number.");
                break;
            }else{//说明不是回文数,继续相加
                sum=add(s,s1);
                System.out.println(s+" + "+s1+" = "+sum);
                s=String.valueOf(sum);//重新赋值
            }
            count++;//计数,依题意到了10次就跳出
            if(count==10){
                System.out.println("Not found in 10 iterations.");
                break;
            }
        }
    }
}

1081

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

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String []string = new String[n];
        for (int i = 0; i < n; i++) {
            string[i] = br.readLine();
        }
        for (int i = 0; i < n; i++) {
            if(string[i].length()<6){
                System.out.println("Your password is tai duan le.");
                continue;
            }
            if(!string[i].matches("[a-zA-Z0-9.]+")){
                System.out.println("Your password is tai luan le.");
                continue;
            }
            if(!string[i].matches(".*[0-9].*")){
                System.out.println("Your password needs shu zi.");
                continue;
            }
            if(!string[i].matches(".*[a-zA-Z].*")){
                System.out.println("Your password needs zi mu.");
                continue;
            }
            System.out.println("Your password is wan mei.");
        }
    }
}

1086

import java.util.Scanner;

public class _1086 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int count = n * m;
        StringBuilder s = new StringBuilder();//创建StringBuilder
        s.append(count);//将数值添加进这个类
        String s1 = s.reverse().toString();//反转后转为字符串
        int s2 =Integer.parseInt(s1);
        System.out.println(s2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值