PTA习题总结

某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。

输入格式:

在一行中一个字符串,长度不超过50个字符。例如: 13802988920

输出格式:

如果验证通过则输出Yes,否则输出No。

输入样例:

在这里给出一组输入。例如:

13812345678

输出样例:

在这里给出相应的输出。例如:

Yes
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
      
        String phonenum = input.next();
        if (NumSize(phonenum) && IsNum(phonenum) && phonenum.charAt(0) == '1') {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }

    }

    public static boolean NumSize(String phonenum) {
        if (phonenum.length() == 11) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean IsNum(String phonenum) {
        for (int i = 0; i < 11; i++) {
            if (!Character.isDigit(phonenum.charAt(i))) {
                return false;
            }
        }
        return true;

    }
}

 

7-4 设计一个简单的英汉专业词典

分数 10

全屏浏览题目

切换布局

作者 王博

单位 西安邮电大学

设计一个简单的英汉专业词典。当用户从键盘输入专业的英文单词,能够查找到对应的中文。例如:输入encapsulation能查到“封装性”;输入inheritance 能查到“继承性”;输入polymorphism能查到“多态性”;输入message能查到“消息”;输入class能查到“类”;输入object能查到“对象”;输入constructor能查到“构造方法”等等。

输入格式:

sphere

输出格式:

抱歉!没有找到sphere

输入样例:

在这里给出一组输入。例如:

multithreading

输出样例:

在这里给出相应的输出。例如:

multithreading : 多线程
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        HashMap stringHashMap = new HashMap<>();
        stringHashMap.put("encapsulation","封装性");
        stringHashMap.put("inheritance","继承性");
        stringHashMap.put("polymorphism","多态性");
        stringHashMap.put("object","对象");
        stringHashMap.put("constructor","构造方法");
        stringHashMap.put("message","消息");
         stringHashMap.put("multithreading","多线程");
        String key=scanner.next();
        if(stringHashMap.get(key) ==null){
            System.out.println("抱歉!没有找到" + key);
        }else {
            System.out.println(key+" : "+stringHashMap.get(key));
        }
    }
}

 

7-5 找出一个给定字符串某个字符出现的次数

分数 10

切换布局

作者 王博

单位 西安邮电大学

编写一个类,类中有如下的方法,找出一个在给定字符串的某个字符的出现的次数,方法的声明如下:
public static int count(String str, char a)
例如, count("Welcome", 'e') returns 2. 编写一个测试程序,特使用户输入字符串和一个字符,按照提示输出其出现的次数。 下面是输入和输出的样例,请严格按照输入输出格式进行。

输入格式:

please input the string and the character.

输出格式:

Then output the the number of occurrences of a specified character in the string.

输入样例:

Welcome e

输出样例:

The number of occurrences is 2.
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.next();
        String s2 = scanner.next();
        int count = getStrCunt(s1, s2);
        
        System.out.println("The number of occurrences is "+count+".");
    }

    private static int getStrCunt(String mainStr, String subStr) {
        // 声明一个要返回的变量
        int count = 0;
        // 声明一个初始的下标,从初始位置开始查找
        int index = 0;
        // 获取主数据的长度
        int mainStrLength = mainStr.length();
        // 获取要查找的数据长度
        int subStrLength = subStr.length();
        // 如果要查找的数据长度大于主数据的长度则返回0
        if (subStrLength > mainStrLength) return 0;
        // 循环使用indexOf查找出现的下标,如果出现一次则count++
        while ((index = mainStr.indexOf(subStr, index)) != -1) {
            count++;
            // 从找到的位置下标加上要查找的字符串长度,让指针往后移动继续查找
            index += subStrLength;
        }
        return count;
    }
}

 

给定两个字符串,求解给定字符串的后缀。按照如下的格式进行输出

输入格式:

输入为一对字符串,以空格分开。 例如:

father mather 

输出格式:

返回两个字符串的最大后缀,例如:

The common suffix is ather. 

又如:

输入样例:

Tom Jack

输出样例:

No common suffix.

import java.util.Scanner;

/*
father mather
 */
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        StringBuilder s1=new StringBuilder(sc.next());
        StringBuilder s2=new StringBuilder(sc.next());
        StringBuilder s=new StringBuilder();
        int len;
        if(s1.length()<s2.length())
            len=s1.length();
        else len=s2.length();
        s2=s2.reverse();
        s1=s1.reverse();
        for(int i=0;i<len;++i)
            if (s2.charAt(i)==s1.charAt(i)) {
                s.append(s1.charAt(i));
            } else break;
        if(0 == s.length())
            System.out.println("No common suffix.");
        else
            System.out.println("The common suffix is "+s.reverse()+".");
            sc.close();
    }
}

 

Palindrome integer) Write the following two methods
//Return the reversal of an integer, i.e. reverse(456) returns 654
public static int reverse(int number)
Return true if number is palindrome
public static boolean isPalindrome(int number)
Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts theuser to enter an integer and reports whether the integer is a palindrome.

input style :

Enter one int number.

output style:

Displays whether the integer is a palindrome.

input sample:

1214344232

output sample 1:

1214344232 is NOT a palindrome.

input sample 2:

123454321

output sample:

123454321 is a palindrome.
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if(isPalindrome(a)){
            System.out.println(a+" is a palindrome.");
        }else{
             System.out.println(a+" is NOT a palindrome.");
        }
    }
    public static int reverse(int number){
        int a = 0;
        while(number!=0){
            a =a*10+number%10 ;
            number = number/10;
             
            
        }
        return a ;
    }
    public static boolean isPalindrome(int number){
        if(number == reverse(number)){
            return true ;
        }else{
            return false ;
        }
    }
}    

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值