Java PTA上机测试题目小结之编程题

1. 给定两个点的坐标,求解两个点的距离(10 分)
给定两个点的坐标,求解两个点的距离。

输入格式:
给定四个浮点数,作为线段的两个点。

输出格式:
输出该线段的距离。

输入样例:

0 0 1.0 1.0

输出样例:

The distance is 1.41

答案:

//给定两个点的坐标,求解两个点的距离
import java.util.*;
import java.math.*;

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

    double x1 = input.nextDouble();
    double y1 = input.nextDouble();
    double x2 = input.nextDouble();
    double y2 = input.nextDouble();

    System.out.println(String.format("The distance is "+"%.2f", Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))) );
  }

}

2.两个巨大素数(质数)的乘积(10 分)
得到两个巨大素数(质数)的乘积是简单的事,但想从该乘积分解出这两个巨大素数却是国际数学界公认的质因数分解难题。这种单向的数学关系,是不对称加密RSA算法的基本原理。 本题给出两个大素数(128bit位)的乘积和其中一个素数,请你编程求出另一个素数。

输入格式:
44022510695404470886511586569647292146578314354528108825807522926455663589709 (大素数的乘积) 189193782774204832019945226750213439577 (其中一个大素数)

输出格式:
232684764001698545563067004009755869717 (另一个素数)

输入样例:

60883665878129858935918958333091530420746054622405737630613777684610994823161
271963475875372143777333694041058521413

输出样例:

223867067745633357281812540202957589797

答案:

//两个巨大素数(质数)的乘积
import java.util.*;
import java.math.BigInteger;

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

    BigInteger a = input.nextBigInteger();
    BigInteger b = input.nextBigInteger();
    BigInteger c = a.divide(b);


    System.out.println(c);
  }
}

3.字符串替换(10 分)
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture. end (表示结束) Institute (第一个字符串,要求用第二个字符串替换) University (第二个字符串)

输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

输入样例:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University

输出样例:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

答案:

//字符串替换
import java.util.*;

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

    String str = "";
    String x = null;
    x = input.nextLine();

    while (!x.equals("end")) {
      str = str + x; 
      x = input.nextLine();
    }
    String a = input.nextLine();
    String b = input.nextLine();

    String str1 = str.replaceAll(a, b);

    System.out.println(str1);
  }
}

4.对字符串进行排序输出(10 分)
给定一个字符串,对该字符串进行排序,请输出排好序的字符串。要求能够连续输入输出的字符串。

输入格式:
在一行输入一个字符串

输出格式:
输出排好序的字符串的序列

输入样例:

fecbad

输出样例:

abcdef

答案:

// 对字符串进行排序输出
import java.util.*;

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

    String str = input.next();

    char [] a = str.toCharArray();

    for (int i = 0; i < str.length(); ++i) {
      for (int j = i + 1; j < str.length(); ++j) {
        if (a[j] < a[i]) {
          char temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }

    str = String.valueOf(a);

    System.out.println(str);

  }
}

5.查找电话号码(10 分)
文件phonebook1.txt中有若干联系人的姓名和电话号码。 高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found. 由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。

输入格式:
高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 noname (表示结束) 唐三藏 (需要查找此人的电话号码)

输出格式:
13612346666 (输出对应的电话号码)

输入样例:

白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精

输出样例:

Not found.

答案:

//查找电话号码
import java.util.*;

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

    Map<String,String> list = new HashMap<String, String>();
    String name;
    String telphone;
    name = input.next();
    while (!name.equals("noname")) {
      telphone = input.next();
      list.put(name, telphone);
      name = input.next();
    }
    String aim = input.next();
    if (list.get(aim) != null) {
      System.out.println(list.get(aim));
    }
    else {
      System.out.println("Not found.");
    }
  }
}

6.求几何形状的面积之和(10 分)
(求几何形状的面积之和)编写一个方法,求数组中所有几何形状对象的面积之和。方法签名如下: public static double sumArea(shape[] a) 编写测试程序,继承抽象类shape得到圆形类Circle和矩形类Rectangle。 abstract class shape {// 抽象类 / 抽象方法 求面积 / public abstract double getArea(); / 抽象方法 求周长 / public abstract double getPerimeter(); } 创建四个对象(两个圆和两个矩形)的数组,然后使用sumArea方法求出它们的总面积。(保留4位小数)

输入格式:
输入 1.1 (第1个圆形的半径) 1.8 (第2个圆形的半径) 2.3 3.8 (第1个矩形的宽和高) 5.9 16.8 (第2个矩形的宽和高)

输出格式:
The total area is 121.8401 (总面积,保留4位小数)

输入样例:

2.18
3.16
2.9 5.76
4.8 9.23

输出样例:

The total area is 107.3088

答案:

//求几何形状的面积之和
import java.util.*;

abstract class shape {
  public abstract double getArea();
}

class Circle extends shape {
   public double radius;

  Circle(double r) {
    this.radius = r;
  }

  public double getArea() {
    return Math.PI * radius * radius;
  }
}
class Rectange extends shape {
  public double side1, side2;

  Rectange(double s1, double s2) {
    this.side1 = s1;
    this.side2 = s2;
  }
  public double getArea() {
    return side1 * side2;
  }
}
public class Main{
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    shape[] a = new shape[4];

    double r1 = input.nextDouble();
    Circle c1 = new Circle(r1);

    double r2 = input.nextDouble();
    Circle c2 = new Circle(r2);

    double side11 = input.nextDouble();
    double side12 = input.nextDouble();
    Rectange rec1 = new Rectange(side11, side12);

    double side21 = input.nextDouble();
    double side22 = input.nextDouble();
    Rectange rec2 = new Rectange(side21, side22);

    a[0] = c1;
    a[1] = c2;
    a[2] = rec1;
    a[3] = rec2;

    double sum = 0;

    for (int i = 0; i < 4; ++i) {
      sum += a[i].getArea();
    }

   System.out.println(String.format("The total area is "+"%.4f", sum));

  }
}

7.查找成绩并折算后输出(10 分)
文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。 Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。

输入格式:
Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 noname (表示结束) Bill

输出格式:
Not found.

输入样例:

Smith  67
Anderson  75
Lewis  83
Cook  58
David  96
noname
Lewis

输出样例:

17.43

答案:

// 查找成绩并折算后输出
import java.util.*;

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

    Map<String, Double> list = new HashMap<String,Double>();

    while (true) {
      String name = input.next();
      if (name.equals("noname")) {
        break;
      }
      double score = input.nextDouble();
      list.put(name, score);
    }
    String aim = input.next();

    if (list.containsKey(aim)) {
      System.out.println(String.format("%.2f", list.get(aim) * 0.21));
    }
    else {
      System.out.println("Not found.");
    }
  }
}

8.finds the occurrences of a specified character in the string(10 分)
Write a method that finds the number of occurrences of a specified character in the string using the following header: public static int count(String str, char a) For example, count(“Welcome”, ‘e’) returns 2. Write a test program that prompts the user to enter a string followed by a character and displays the number of occurrences of the character in the string.

输入格式:
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.

答案:

//finds the occurrences of a specified character in the string
import java.util.*;

public class Main {
  public static int Count(String str, char c) {
    char[] a = str.toCharArray();
    int count = 0;
    for (int i = 0; i < str.length(); ++i) {
      if (a[i] == c) {
        count++;
      }
    }
    return count;
  }

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

    String str = input.next();
    String temp = input.next();
    char c = temp.charAt(0);

    System.out.printf("The number of occurrences is %d.", Count(str, c));
  }
}

9.数字格式异常(10 分)
(NumberFormatException数字格式异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户再次输入数字。

输入格式:
i 9 (第1次输入) l 8 (第2次输入) 5 6 (第3次输入)

输出格式:
Incorrect input and re-enter two integers: (第1次输出提示) Incorrect input and re-enter two integers: (第2次输出提示) Sum is 11 (输出结果)

输入样例:

i 9
l 8
5 6

输出样例:

Incorrect input and re-enter two integers:
Incorrect input and re-enter two integers:
Sum is 11

答案:

//数字格式异常
import java.util.*;

public class Main {
  public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int m = 0,n = 0;
        while (true) {
          try {
            m = input.nextInt();
            n = input.nextInt();
            System.out.printf("Sum is %d", m+n);
            break;
          }catch(InputMismatchException e){
            System.out.println("Incorrect input and re-enter two integers:");
            input.nextLine();
          }
        }

  }
}

10.找素数(10 分)
请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。

输入格式:
第一个整数为m,第二个整数为n;中间使用空格隔开。例如: 103 3

输出格式:
从小到大输出找到的等于或大于m的n个素数,每个一行。例如: 103 107 109

输入样例:

9223372036854775839 2

输出样例:

9223372036854775907
9223372036854775931

答案:

//找素数
import java.util.*;
import java.math.BigInteger;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    BigInteger m;
    int n, cnt = 0;

    m = input.nextBigInteger();
    n = input.nextInt();

    while (cnt < n) {
      if (m.isProbablePrime(100)) {
        System.out.println(m);
        cnt++;
      } 
      m = m.nextProbablePrime();
    }
  }
}

11.使用公历类GregorianCalendar(10 分)
使用公历类 GregorianCalendar,公历类 GregorianCalendar有方法setTimeInMillis(long);可以用它来设置从1970年1月1日算起的一个特定时间。请编程从键盘输入一个长整型的值,然后输出对应的年、月和日。例如输入:1234567898765,输出:2009-1-14

输入格式:
输入 1234567898765 (毫秒数)

输出格式:
输出 2009-1-14 (输出年、月和日,实际应该是2月,因为Java API 从0开始计算月份)

输入样例:

1450921070108

输出样例:

2015-11-24

答案:

//使用公历类GregorianCalendar
import java.util.Scanner;
import java.util.Calendar;

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

    String s = input.next();

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(Long.parseLong(s));
    System.out.println(c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONTH)+"-"+c.get(Calendar.DAY_OF_MONTH));
  }
}

12.找出最大的对象(10 分)
(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下: public static Object max(Comparable[] a) 所有对象都是Comparable接口的实例。对象在数组中的顺序是由compareTo方法决定的。 编写测试程序,从键盘输入5个字符串和5个整数,创建一个由5个字符串构成的数组、一个由5个整数构成的数组。找出数组中最大的字符串、整数并输出。

输入格式:
输入 Xi’an (输入5个字符串,每行一个) Beijing ShangHai GuangZhou ShenZhen 8 9 12 7 6 (输入5个整数,以空格分隔)

输出格式:
输出 Max string is Xi’an (输出最大的字符串) Max integer is 12 (输出最大的整数)

输入样例:

France
Japan
German
China
India
6 34 89 168 53

输出样例:

Max string is Japan
Max integer is 168

答案:

//找出最大的对象
import java.util.*;

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

    String[] s = new String [5];
    int[] a = new int[5];

    for (int i = 0; i < 5; ++i) {
      s[i] = input.next();
    }
    for (int i = 0; i < 5; ++i) {
      a[i] = input.nextInt();
    }

    Arrays.sort(s);
    Arrays.sort(a);

    System.out.println("Max string is "+s[4]);
    System.out.println("Max integer is "+a[4]);
  }
}

13.大数整除(10 分)
请编写程序,从键盘输入一个整数n,找出大于long.MAX_VALUE且能被n整除的前3个数字。

输入格式:
输入一个作为除数的整数n,例如: 17

输出格式:
输出大于long.MAX_VALUE且能被n整除的前3个数字,例如下列三个数能被17整除且大于long.MAX_VALUE: 9223372036854775816 9223372036854775833 9223372036854775850

输入样例:

103

输出样例:

9223372036854775832
9223372036854775935
9223372036854776038

答案:

//大数整除
import java.util.Scanner;
import java.math.BigInteger;

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

    BigInteger biginteger = new BigInteger(String.valueOf(Long.MAX_VALUE));
    int n = input.nextInt();
    int cnt = 0;
    while (cnt < 3) {
      if (biginteger.mod(BigInteger.valueOf(n)).intValue() == 0) {
        System.out.println(biginteger.toString());
        ++cnt;
      }
      biginteger = biginteger.add(BigInteger.valueOf(1));
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_38253837/article/details/79055736