第 1 章 Java语言概述

  • Java语言有哪些特点?

    答:简单易学、面向对象、平台无关性、可靠性、安全性、支持多线程、支持网络编程、编译与解释并存

  • 什么是Java的虚拟机?

    答:任何一种可以运行Java字节码的软件均可看成是Java的虚拟机

  • 什么是字节码?采用字节码的最大好处是什么?

    答:字节码是Java虚拟机的指令组,和CPU上的微指令很相似 。字节码最大的好处是可跨平台运行

  • 什么是平台无关性?Java语言是怎样实现平台无关性的?

    答:编写的应用程序不用修改就可以在不同的软硬件平台上运行。Java语言是靠JVM在目标代码级实现平台无关性的,可以说JVM是Java平台无关的基础

  • Java语言程序有几种?他们包含哪几个方面?

    答:Application应用程序和Applet小程序

  • 什么是Java程序的主类?

    答:Java应用程序的主类必须包含一个定义为public static void main(String[] args);Java小程序的主类必须是一个继承自系统JApplet或Applet的子类,且该类必须是public类。

第 3 章 Java语言基础

  • Java语言定义了哪几种基本数据类型?

    答:8种基本数据类型。byte, short, int, long, float, double, char

  • 表示整数类型数据的关键字有哪几个?他们各占用几个字节?

    答:byte, short, int, long分别占1, 2, 4, 8个字节

  • 单精度浮点float和双精度浮点double的区别是什么?

    答:单精度浮点数的数据位是32位,双精度浮点数的数据位是64位,double的精度是float的两倍

  • 字符型常量与字符串常量的主要区别是什么?

    答:字符串常量是用一对单引号括起来的单个字符,字符串常量是用双引号括起来的一串若干个字符(可以是0个)

  • 简述Java语言对定义标识符的规定有哪些。

    答:标识符可以由字母、数字和下划线、美元符号等组合而成,标识符必须以字母、下划线或美元符号开头,不能以数字开头

  • Java语言采用何种编码方案?有何特点?

    答:Unicode字符集编码方案,便于西文字符和中文字符的处理

  • 什么是强制类型转换?在什么情况下需要强制类型转换?

    答:强制类型转换就是将长数据转换为短数据。如果要将较长的数据转换成较短的数据时,就要进行强制类型转换。

  • 自动类型转换得前提是什么?转换是从”短”到”长”的优先级顺序是怎样的?

    答:转换前的数据类型与转换后的类型兼容,转换后的数据类型的表示范围比转换前的类型大。byte→short→char→int→long→float→double

  • 数字字符串转换为数值型数据时,所使用的方法有哪些?

    答:

    转换的方法 功能说明
    Byte.parseByte(String s) 将数字字符串转换为字节型数据
    Short.parseShort(String s) 将数字字符串转换为短整型数据
    Integer.parseInteger(String s) 将数字字符串转换为整型数据
    Long.parseLong(String s) 将数字字符串转换为长整型数据
    Float.parseFloat(String s) 将数字字符串转换为浮点型数据
    Double.parseDouble(String s) 将数字字符串转让为双精度型数据
    Boolean.parseBoolean(String s) 将字符串转换为布尔型数据
  • 写出由键盘输入数据的两种基本格式。

    答:在1.5版本之前,Java用BufferedReader来读取输入数据,在1.5版本之后,Java用Scanner来读取输入数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.io.*;

    public class Buffer {
    public static void main(String[] args) throws IOException {
    String str;
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    str = buf.readLine();
    System.out.println(str);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    import java.util.*;

    public class Scan {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double num = sc.nextDouble();
    System.out.println(num);
    }
    }
  • 编写程序,从键盘上输入一个浮点数,然后将该浮点数的整数部分输出。

    答:浮点数的输入用double或者float,第一种方法用BufferedReader来读,第二种方法用Scanner来读

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.io.*;

    public class Exercise {
    public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    double num = Double.parseDouble(buff.readLine());
    int i = (int) num;
    System.out.println(i);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double t = sc.nextDouble();
    int num = (int) t;
    System.out.println(num);
    }
    }
  • 编写程序,从键盘上输入两个整数,然后计算他们相除后得到的结果并输出。

    答:这里用Scanner来读两个整数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.println(a / b);
    }
    }
  • 编写程序,从键盘上输入圆柱体的底半径r和高h,然后计算其体积并输出。 答:这里要用到Math.PI,假设题目给出的半径r和高h都是整形数值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int r = sc.nextInt();
    int h = sc.nextInt();
    System.out.println(Math.PI * r * r * h);
    }
    }
  • Java语言有哪些算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符?

    • 算术运行符包括+、-、*、/、%、++、–
    • 关系运算符包括>、<、>=、<=、==、!=
      • 逻辑运算符包括!、&&、||、&、|
      • 位运算符包括>>、<<、>>>、&、|、^、~
      • 赋值运算符包括=
  • 逻辑运算符中“逻辑与、逻辑或”和“简洁与、简洁或”的区别是什么?

    答:非简洁运算在必须计算玩左右两个表达式之后,才取结果值;而简洁运算可能只计算左右的表达式而不计算右边的表达式

  • 逻辑运算符与位运算符的区别是什么?

    答:位运算符的操作数只能为整型或字符型数据,逻辑运算符的操作数为boolean型的量

  • 什么是运算符的优先级和结合性?

    答:运算符的优先级决定了表达式中不同运算执行的先后顺序,运算符的结合性决定了并列的多个同级运算符的先后执行顺序

  • 写出下列表达式的值,设x=3, y = 17, yn = true。

    (1) x + y * x -- 3 + 17 * 3, 3 + 51, 54

    (2) -x\*y+y -3 \* 17 + 7, -51 + 7, -44

    (3) x<y&&yn 3 < 17 && true, true

    (4) x > y ||!yn 3 > 17 || !true, false

    (5) y!=++x?x:y 17 != 4 ? 4 : 17, 4

    (6) y++/--x 17 / 2, 8

第 4 章 流程控制

  • 将学生的学习成绩按不同的分数段分为优、良、中、及格和不合格五个等级,从键盘上输入一个0~100之间的成绩,输出响应的等级。要求用switch语句实现。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int score = in.nextInt();
    switch (score / 10) {
    case 10:
    case 9:
    System.out.println("优");
    break;
    case 8:
    System.out.println("良");
    break;
    case 7:
    System.out.println("中");
    break;
    case 6:
    System.out.println("及格");
    break;
    default:
    System.out.println("不合格");
    break;
    }
    }
    }
  • 设学生的学习成绩按如下的分数评定为四个等级:85100为A,7084为B,6069为C,059为D。从键盘上输入一个0~100之间的成绩,要求用switch语句根据成绩,评定并输出相应的等级。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int score = in.nextInt();
    switch (score / 10) {
    case 10:
    case 9:
    System.out.println("A");
    break;
    case 8:
    if (score % 10 >= 5)
    System.out.println("A");
    else
    System.out.println("B");
    break;
    case 7:
    case 6:
    System.out.println("C");
    break;
    default:
    System.out.println("D");
    break;
    }
    }
    }
  • 编写一个Java应用程序,输入1~100之间所有既可以被3整除,又可被7整除的数。

    1
    2
    3
    4
    5
    6
    7
    8
    public class Exercise {
    public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 7 == 0)
    System.out.println(i);
    }
    }
    }
  • 编写一个Java应用程序,在键盘上输入数n,计算并输出1!+2!+…+n!的结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    long sum = 0, temp = 1;
    for (int i = 1; i <= n; i++) {
    sum += temp * i;
    temp = i;
    }
    System.out.println(sum);
    }
    }
  • 在键盘上输入数n,编程计算sum = 1 - (1 / 2!) + (1/ 3!) -…(-1)^n-1(1/n!)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    double sum = 0;
    int sign = 1, temp = 1;
    for (int i = 1; i <= n; i++) {
    sum += sign * (1.0 / (temp * i));
    temp *= i;
    sign = -sign;
    }
    System.out.println(sum);
    }
    }
  • 水仙花数是指其个位、十位和百位三个数字的立方和等于这个三位数本身,求出所有的水仙花数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class Exercise {
    public static void main(String[] args) {
    for (int i = 100; i <= 999; i++) {
    int temp = i, sum = 0;
    while (temp != 0) {
    sum += Math.pow(temp % 10, 3);
    temp /= 10;
    }
    if (sum == i)
    System.out.println(i);
    }
    }
    }
  • 从键盘输入一个整数,判断该数是否是完全数。完全数是指其所有因数(包括1但不包括其本身)的和等于该数自身的数。例如,28=1+2+4+7+14就是一个完全数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int num = in.nextInt();
    int sum = 0;
    for (int i = 1; i < num; i++) {
    if (num % i == 0)
    sum += i;
    }
    System.out.println(sum == num);
    }
    }
  • 计算并输出一个整数各位数字之和。如,5423的各位数字之和为5+4+2+3。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int num = in.nextInt();
    int sum = 0;
    while (num != 0) {
    sum += num % 10;
    num /= 10;
    }
    System.out.println(sum);
    }
    }
  • 从键盘上输入一个浮点型数,然后将该浮点数的整数部分和小数部分分别输出。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double num = in.nextDouble();
    int digital = (int) num;
    System.out.println(digital + " " + (num - digital));
    }
    }

     

  • 设有一长为3000m的绳子,每天减去一半,问需几天时间,绳子的长度会短于5m。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Exercise {
    public static void main(String[] args) {
    int day = 0;
    double len = 3000;
    while (len >= 5) {
    len /= 2;
    day++;
    }
    System.out.println(day);
    }
    }
  • 编程输出如下的数字图案:1 3 6 10 15 2 5 9 14 4 8 13 7 12 11

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class Exercise {
    public static void main(String[] args) {
    int[][] matrix = new int[5][5];
    int k = 1;
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j <= i; j++) {
    matrix[i - j][j] = k++;
    }
    }
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5 - i; j++) {
    System.out.print(matrix[i - j][j] + " ");
    }
    System.out.println();
    }
    }
    }

第 5 章 数组与字符串

  • 从键盘输入n个数,输出这些数中大于其平均值的数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] num = new int[n];
    double aver = 0;
    for (int i = 0; i < n; i++) {
    num[i] = in.nextInt();
    aver += num[i] * 1.0 / n;
    }
    for (int i = 0; i < n; i++) {
    if (num[i] >= aver)
    System.out.println(num[i]);
    }
    }
    }
  • 从键盘键入n个数,求这n个数中得最大数与最小数并输出。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
    for (int i = 0; i < n; i++) {
    int temp = in.nextInt();
    if (temp > max)
    max = temp;
    if (temp < min)
    min = temp;
    }
    System.out.println(max + " " + min);
    }
    }
  • 求一个3阶方阵的对角线上各元素之和。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Main {
    public static void main(String[] args) {
    int[][] matrix = new int[3][3];
    int sum = 0;
    for (int i = 0; i < 3; i++) {
    sum += matrix[i][i];
    sum += matrix[3 - i][i];
    }
    System.out.println(sum);
    }
    }
  • 找出4 x 5矩阵中值最小和最大元素,并分别输出其值及所在的行号和列号。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public class Main {
    public static void main(String[] args) {
    int[][] matrix = new int[4][5];
    int minRow = 0, minCol = 0, maxRow = 0, maxCol = 0;
    for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 5; j++) {
    if (matrix[minRow][minCol] > matrix[i][j]) {
    minRow = i;
    minCol = j;
    }
    if (matrix[maxRow][maxCol] < matrix[i][j]) {
    maxRow = i;
    maxCol = j;
    }
    }
    }
    System.out.println(minRow + " " + minCol + " " + maxRow + " " + maxCol);
    }
    }
  • 产生0~ 100之间的8个随机整数,并用冒泡排序将其升序排序后输出(冒泡排序算法:每次进行相邻两数的比较,若次序不对,则交换两数的次序)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    int[] num = new int[8];
    Random random = new Random();
    for (int i = 0; i < 8; i++) {
    num[i] = random.nextInt() % 100;
    }
    for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8 - i - 1; j++) {
    if (num[j] > num[j + 1]) {
    int temp = num[j];
    num[j] = num[j + 1];
    num[j + 1] = temp;
    }
    }
    }
    for (int i = 0; i < 8; i++) {
    System.out.println(num[i] + " ");
    }
    }
    }
  • 15个红球和15个绿球排成一圈,从第1个球开始数,当数到第13个球时就拿出此球,然后再从下一个球开始数,当再数到第13个球时又取出此球,如此循环进行,直到仅剩15个球为止,问怎样排法才能使每次取出的球都是红球。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
        public class Exercise {
    public static void main(String[] args) {
    int[] ball = new int[30];
    for (int j = 0, temp = 0; temp <= 15; temp++) {
    int cnt = 1, i = j;
    while (cnt < 13 || ball[i] == 1) {
    if (ball[i] != 1)
    ​ cnt++;
    ​ i++;
    if (i == 30)
    ​ i = 0;
    ​ }

    ball[i] = 1;
    j = i + 1;
    if (j == 30)
    j = 0;
    }
    for (int i = 0; i <= 29; i++) {
    System.out.print(ball[i] + " ");
    }
    }
    }
    ​ ```

    * 编写Java应用程序,比较命令行中给出的两个字符串是否相等,并输出比较结果。

    ```Java
    public class Exercise {
    public static void main(String[] args) {
    System.out.println(args[0].equals(args[1]));
    }
    }
  • 从键盘上输入一个字符串和子串开始位置与长度,截取该字符串的子串并输出。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();
    int start = in.nextInt(), len = in.nextInt();
    System.out.println(s.substring(start, start + len));
    }
    }
  • 从键盘上输入一个字符串和一个字符,从该字符串中删除给定的字符。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.nextLine();
    char c = in.next().charAt(0);
    for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) != c)
    System.out.print(s.charAt(i));
    }
    }
    }
  • 编程统计用户从键盘输入的字符串中所包含的字母、数字和其他字符的个数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.nextLine();
    int ch = 0, digital = 0, other = 0;
    for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if ((c >= 'A' && c <= 'Z') || (c >= 'A' && c <= 'z'))
    ch++;
    else if (c >= '0' && c <= '9')
    digital++;
    else
    other++;
    }
    System.out.println(ch + " " + digital + " " + other);
    }
    }
  • 将用户从键盘输入的每行数据都显示输出,直到输入字符串”exit”,程序运行结束。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.*;

    public class Exercise {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String data = "";
    do {
    data = in.nextLine();
    if ("exit".equals(data))
    break;
    System.out.println(data);
    } while (true);
    }
    }

​    

第 6 章 类与对象

  • 类与对象的区别是什么? 类是对某一类食物的描述,是抽象的、概念上的定义 对象则是实际存在的属该类食物的具体的个体 对象是类的实例化

  • 如何定义一个类?类的结构是怎样的? 定义一个类实际上就是定义类的属性和方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    类的一般结构如下:
    \[类修饰符\] class 类名称
    {
    \[修饰符\] 数据类型 成员变量名称;
    \[修饰符\] 返回值的数据类型 方法名(参数1,参数2,……)
    {
    语句序列;
    return \[表达式\];
    }
    }
  • 定义一个类时所使用的修饰符有哪几个?每个修饰符的作用是什么?是否可以混用? 定义一个类时所使用的修饰符有4个,每个修饰符的作用如下:

    修 饰 符 含 义
    public 将一个类声明为公共类,它可以被任何对象方法
    abstract 将一个类声明为抽象类,没有实现方法,需要子类提供方法的实现,所以不能创建该类的实例
    final 将一个类声明为最终类即非继承类,表示他不能被其他类所继承
    缺省 缺省修饰符时,则表示只有在相同包中的对象才能使用这样的类
  • 成员变量的修饰符有哪些?各修饰符的功能是什么?是否可以混用? 成员变量的修饰符有8个,每个修饰符的功能如下:

    修 饰 符 含 义
    public 公共访问控制符。指定该变量为公共的,它可以被任何对象的方法访问
    private 私有访问控制符。指定该变量只允许自己类的方法访问,其他任何类(包括子类)中的方法均不能访问此变量
    protected 保护访问控制符。指定该变量只可以被它自己的类及其子类或同一包中的其他类访问,在子类中可以覆盖此变量
    缺省 缺省访问控制符时,则表示在同一个包中的其他类可以访问此成员变量,而其他包中的类不能访问该成员变量
    final 最终修饰符。指定此变量的值不能改变
    static 静态修饰符。指定该变量被所有对象共享,即所有的实例都可以使用该变量
    transient 过度修饰符。指定该变量是一个系统保留,暂无特别作用的临时性变量
    volatile 易失修饰符。指定该变量可以同时被几个线程控制和修改
  • 成员方法的修饰符有哪些?各修饰符的功能是什么?是否可以混用? 成员方法的修饰符有9个,每个修饰符的功能如下:

    修 饰 符 含 义
    public 公共访问控制符。指定该方法为公共的,它可以被任何对象的方法访问
    private 私有访问控制符。指定该方法只允许自己类的方法访问,其他任何类(包括子类)中的方法均不能访问此方法
    protected 保护访问控制符。指定该方法只可以被它自己的类及其子类或同一包中的其他类访问
    缺省 缺省访问控制符时,则表示在同一个包中的其他类可以访问此成员方法,而其他包中的类不能访问该成员方法
    final 最终修饰符。指定该方法不能被重载
    static 静态修饰符。指定不需要实例化一个对象就可以调用的方法
    abstract 抽象修饰符。指定该方法只声明方法头,而没有方法体,抽象方法需在子类中被实现
    synchronized 同步修饰符。在多线程程序中,该修饰符用于在运行前,对它所属的方法加锁,以防止其他线程访问,运行结束后解锁
    native 本地修饰符。指定此方法的方法体是用其他语言(如C)在程序外部编写的
  • 成员变量与局部变量的区别有哪些?

    • 从语法形式上看,成员变量属于类的,而局部变量是在方法中定义的变量或是方法的参数;成员变量可以被public、private、static等修饰符所修饰,而局部变量则不能被访问控制修饰符及static所修饰;成员变量和局部变量都可以被final所修饰
    • 从变量在内存中得存储方式上看,成员变量是对象的一部分,而对象是存在于堆内存的,而局部变量是存在于栈内存的。
    • 从变量在内存中的生存时间上看,成员变量是对象的一部分,它随着对象的创建而存在,而局部变量随着方法的调用而产生,随着方法调用的结束而自动消失
    • 成员变量如果没有被赋初值,则会自动以类型的默认值赋值(有一种情况例外,被final修饰但没有被static修饰的成员变量必须显示地赋值);而局部变量则不会自动赋值,必须显式地赋值后才能使用。
  • 创建一个对象使用什么运算符?对象实体与对象的引用有何不同?

    创建一个对象使用new运算符;对象实体是实在存在于对内存中的,而对象的引用是管理对象实体的句柄存在于栈内存中

  • 对象的成员如何表示?

    对象的成员通过对象名.对象成员来访问

  • 在成员变量或成员方法前加上关键字this表示什么含义?

    this特指成员变量

  • 什么是方法的返回值?返回值在类的方法里面的作用是什么?

  • 在方法调用中,使用对象作为参数进行传递时,是“传值”还是“传址”?对象作参数起到什么作用?

    传址;

  • 什么叫匿名对象?一般在什么情况下使用匿名对象? 当一个对象被创建之后,不定义对象的引用变量,而是直接调用对象的方法,这个的对象叫做匿名对象; 使用匿名对象的情况有如下两种:

    • 如果对一个对象只需要进行一次方法调用,那么就可以使用匿名对象
    • 将匿名对象作为实参传递给一个方法调用
  • 定义一个Student类,包含如下内容: 成员变量:学号,姓名,性别,班干部否,数学,语文,外语 成员方法:输入,总分,平均分 编程实现这个类,并调用相应的方法输入数据,计算总分和平均分。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    import java.util.*;

    public class Student {
    private String id;
    private String name;
    private String gender;
    private String isLeader;
    private int math;
    private int chinese;
    private int english;

    public void input() {
    Scanner in = new Scanner(System.in);
    id = in.next();
    name = in.next();
    gender = in.next();
    isLeader = in.next();
    math = in.nextInt();
    chinese = in.nextInt();
    english = in.nextInt();
    in.close();
    }

    public int total() {
    return math + chinese + english;
    }

    public double aver() {
    return total() / 3.0;
    }
    }
  • 以m行n列二维数组为参数进行方法调用,分别计算二维数据各列元素之和,返回并输出所计算的结果。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import java.util.*;

    public class Student {

    public static void main(String[] args) {
    int n = 10, m = 10;
    int[][] matrix = new int[m][n];
    int[] sum = new int[n];
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    sum[i] += matrix[j][i];
    }
    System.out.println(sum[i]);
    }
    }
    }

​  

第 7 章 Java语言类的特性

  • 一个类的公共成员与私有成员有何区别?

    私有成员即只能在类的内部访问,在类的外部访问会出现错误 公共成员则可以被其他类所访问

  • 什么方法的重载?

    有一些方法含义相同,但带有不同的参数,这些方法使用相同的名字,这就叫方法的重载

  • 一个类的构造方法的作用是什么?若一个类没有声明构造方法,该成员能正确执行吗?为什么?

    构造方法的作用是在对象被创建时初始化对象的成员的方法;可以正确执行,因为如果省略构造方法,Java编译器会自动为该类生成一个默认的构造方法

  • 构造方法有哪些特性?

    • 构造方法的方法名与类名相同
    • 构造方法没有返回值,但不能写void
    • 构造方法的主要作用是完成对类对象的初始化工作
    • 构造方法一般不能有编程人员显式地调用,而是用new来调用
    • 在创建一个类的对象的同时,系统会自动调用该类的构造方法为新对象初始化
  • 在一个构造方法内可以调用另一个构造方法吗?如果可以,如何调用?

    可以;通过this来调用,而且this关键字必须卸载构造方法内的第一行位置

  • 静态变量与实例变量有哪些不同?

    静态变量是隶属于类的变量,而不是属于任何一个类的具体对象,静态变量不需要实例化就可以使用。 实例变量是隶属于对象的变量,是属于具体的一个类的,是需要把类实例化为对象才可以使用的

  • 静态方法与实例方法有哪些不同?

    静态方法实质是属于整个类的方法,而实例方法是属于某个具体对象的方法

  • 在一个静态方法内调用一个非静态成员为什么是非法的?

    静态方法是属于整个类的,所以它不能 操纵和处理属于某个对象的成员,而只能处理属于整个类的成员,即静态方法只能访问静态成员变量或静态成员方法

  • 对象的相等于指向它们的引用相等,两者有什么不同?

  • 什么是静态初始化器?其作用是什么?静态初始化器由谁在何时执行?它与构造方法有何不同?

    静态初始化器是由关键字static修饰的一对大括号“{}”括起来的语句组;它是用来初始化工作的;静态初始化器有Java虚拟机在类初始化的时候一次执行;它与构造方法的不同:

    • 构造方法是对每个新创建的对象初始化,而静态初始化器是对类自身进行初始化。
    • 构造方法是在用new运算符创建新对象时又系统自动执行,而静态初始化器一般不能有程序来调用,它是在所属的类被加载入内存时有系统调用执行的。
    • 用new运算符创建多少个新对象,构造方法就被调用多少次,但静态初始化器则在类被加载入内存时只执行一次,与创建多少个对象无关。
    • 不同于构造方法,静态初始化器不是方法,因为没有方法名、返回值和参数。

第 8 章 继承、抽象类和接口

  • 子类将继承父类的所有成员吗?为什么?

    不是;父类的私有成员只能是父类自己使用,子类不能继承,子类只能继承父类的所有非private成员

  • 在子类中可以调用父类的构造方法吗?若可以,如何调用?

    可以;在子类的方法中通过super()来调用父类特定的构造方法

  • 在调用子类的构造方法之前,会先自动调用父类中没有参数的构造方法,其目的是什么?

    目的是为了便于初始化操作

  • 在子类中可以访问父类的成员吗?若可以,用什么方式访问?

    可以;通过super.变量名或super.方法名来方法父类的成员

  • 用父类对象变量可以访问子类的成员吗?若可以,则只限于什么情况?

    可以;只限于“覆盖”的情况发生时,也就是说,父类与子类的方法名称、参数个数与类型必须完全相同,才可通过父类的对象调用子类的方法。

  • 什么是多态机制?Java语言中是如何实现多态的?

  • 方法的覆盖与方法的重载有何不同?

    重载是指在同一个类内定义多个名称相同,但参数个数或类型不同的方法 覆盖是指在子类中,定义名称、参数个数与类型均与父类中完全相同的方法,用于实现重写父类中同名方法的功能。

  • this和super分别有什么特殊的含义?

    super是从子类调用父类的成员,而this是调用同一类中得其它成员

  • 什么是最终类与最终方法?他们的作用是什么?

    如果用final修饰成员方法,则该成员方法不能再被子类所覆盖,即该方法为最终方法 如果用final修饰一个类,则该类不能被继承,即该类是最终类

  • 什么是抽象类和抽象方法?使用时应注意哪些问题?

    抽象类是以修饰符abstract修饰的类,抽象方法是以abstract关键字开头的方法,此方法只声明返回值的数据类型、方法名称与所需的参数,但没有方法体;注意:

    • 抽象类是需要被继承的,所以abstract类不能用final来修饰。
    • abstract不能与private、static、final或native并列修饰同一个方法。
  • 什么是接口?为什么要定义接口?

    接口的数据成员都是静态的且必须初始化,接口中的方法必须全部都声明为abstract的,接口就是一种特俗的抽象类;接口的主要作用是帮助实现类的多继承

  • 如何定义接口?接口与抽象类有哪些异同? 定义接口的语法格式如下:

    1
    2
    3
    4
    5
    \[public\] interface 接口名称 \[extends 父接口名列表\]
    {
    \[public\]\[static\]\[final\] 数据类型 成员变量名 = 常量;
    \[public\]\[static\] 返回值的数据类型 方法名(参数列表);
    }

    接口与抽象类不同:

    • 接口的数据成员都是静态的且必须初始化
    • 接口中的方法必须全部都声明为abstract的,也就是说,接口不能像抽象类一样有一般方法,而必须全部是抽象方法
  • 内部类的类型有哪几种?分别在什么情况下使用?它们所起的作用有哪些?

  • 怎样使用匿名内部类对象?

  • 什么是包?它的作用是什么?如何创建包?如何引用包中的类?

    包是类的组织方式;包得作用是提供区别类名空间的机制;使用package可以创建一个包,创建语法package 包名1[.包名2[.包名3]...] ;使用import导入包

  • Java语言中怎样清除对象?能否控制Java系统中垃圾的回收时间?

    Java语言垃圾回收清除对象;不能控制Java系统中垃圾的回收时间

第 9 章 异常处理

  • 什么是异常?简述Java语言的异常处理机制。

    异常是指在程序运行中又代码产生的一种错误;异常,抛出异常,捕获异常

  • Throwable类的两个直接子类Error和Exception的功能各是什么?用户可以捕获的异常是哪个类的异常? Error类及其子类的对象,代表了程序运行时Java系统内部的错误 Exception子类则是提应用程序使用的,它是用户程序能够捕捉到得异常情况。 用户可以捕获的异常是Exception异常

  • Exception类有何作用?每个Exception类的对象代表了什么?

    Exception类对象是Java程序抛出和处理的对象,它由各种不同的子类分别对应于各种不同类型的异常。

  • 什么是运行时异常?什么是非运行时异常?

    运行时异常是程序运行时自动地对某些错误做出反应而产生的;非运行时异常是在程序运行过程中又环境原因造成的异常

  • 抛出异常有哪两种方式?

    • 系统自动抛出的异常
    • 指定方法抛出异常
  • 在捕获异常时,为什么要在catch()括号内设一个变量e?

  • 在异常处理机制中,用catch()括号内的变量e接收异常类对象的步骤有哪些?

  • 在什么情况下,方法的头部必须列出可能抛出的异常?

  • 若try语句结构中有多个catch()子句,这些子句的排列顺序与程序执行效果是否有关?为什么?

    相关;当try块抛出一个异常时,程序的流程首先转向第一个catch块,并审查当前异常对象可否被这个catch块所接受。能接收是指异常

  • 什么是抛出异常?系统定义的异常如何抛出?用户自定义的异常又如何抛出?

  • 系统定义的异常与用户自定义的异常有何不同?如何使用这两类异常?

第 10 章 Java语言的输入输出与文件处理

  • 什么是文件的输入与输出?

    文件的输入与输出即以文件作为数据输入流或数据输出流

  • 什么是流?Java语言中分为哪两种流?这两种流有何差异?

    流是指计算机各部件之间的数据流动;字节流和字符流;

  • InputStream、OutputStream、Reader和Writer四个在在功能上有何异同?

    InputStreamOutputStream是用来处理以位为单位的流

  • 利用基本输入输出流实现从键盘上读入一个字符,然后显示了屏幕上。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    public class Exercise {
    public static void main(String[] args) throws IOException {
    byte[] c = new byte[2];
    InputStream in = new DataInputStream(System.in);
    in.read(c);
    OutputStream out = new DataOutputStream(System.out);
    out.write(c);
    in.close();
    out.close();
    }
    }
  • 顺序输入输出流与管道输入输出流的区别是什么?

    顺序输入流其功能是将多个输入流顺序连接在一起,形成单一的输入数据流,没有对应的输出数据流存在。 管道流用来将一个程序或线程的输出连接到另外一个程序或线程作为输入,从而可以实现程序内部线程间的通信或不同程序间的通信

  • Java语言中定义的三个标准输入输出流是什么?他们对应什么设备?

    标准输入流、标准输出流、标准错误流; 标准输入流对应的设备是键盘 标准输出流和标准错误流对应的设备是显示器

  • 利用文件输出流创建一个文件file1.txt,写入字符“文件已被成功创建!”,然后用记事本打开该文件,看一下是否正确写入。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class Exercise {
    public static void main(String[] args) throws IOException {
    File file = new File("file1.txt");
    FileOutputStream out = new FileOutputStream(file);
    String s = new String("文件已被创建成功!");
    byte[] bytes = s.getBytes("UTF-8");
    out.write(bytes);
    out.flush();
    out.close();
    }
    }
  • 利用文件输入流打开第7题中创建的file1.txt,读出其内容并显示在屏幕上。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    public class Exercise {
    public static void main(String[] args) throws IOException {
    File file = new File("file1.txt");
    FileInputStream in = new FileInputStream(file);
    byte[] bytes = new byte[1024];
    while (in.read(bytes) != -1) {
    System.out.println(new String(bytes));
    }
    in.close();
    }
    }
  • 利用文件输入输出流打开第7题创建的文件file1.txt,然后在文件的末尾追加一行字符串“又添加了一行文字!”。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class Exercise {
    public static void main(String[] args) throws IOException {
    File file = new File("file1.txt");
    FileOutputStream out = new FileOutputStream(file, true);
    String s = "又添加了一行文字!";
    byte[] bytes = s.getBytes("UTF-8");
    out.write(bytes);
    out.close();
    }
    }
  • 产生15个20~9999之间的随机整数,然后利用BufferedWriter类将其写入文件file2.txt中;之后再读取该文件中得数据并将他们以升序排序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Arrays;

    public class Exercise {
    public static void main(String[] args) throws IOException {
    File file = new File("file2.txt");
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    for (int i = 0; i < 15; i++) {
    String s = "" + ((int) (Math.random() * 9979) + 20) + "\n";
    writer.write(s);
    }
    writer.flush();
    writer.close();

    BufferedReader reader = new BufferedReader(new FileReader(file));
    int[] nums = new int[15];
    for (int i = 0; i < 15; i++) {
    nums[i] = Integer.parseInt(reader.readLine());
    }
    reader.close();
    Arrays.sort(nums);
    writer = new BufferedWriter(new FileWriter(file));
    for (int i = 0; i < 15; i++) {
    String s = "" + (nums[i]) + "\n";
    writer.write(s);
    }
    writer.flush();
    writer.close();

    }

    }
  • Java语言中使用什么类来对文件与文件夹进行管理?

    java.io.File

The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format: N = n1^P + … nK^P where ni (i=1, … K) is the i-th factor. All the factors must be printed in non-increasing order. Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122 + 42 + 22 + 22 + 12, or 112 + 62+ 22 + 22 + 22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence { a1, a2, … aK } is said to be larger than { b1, b2, … bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL If there is no solution, simple output “Impossible”.

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible

深搜加剪枝,首先减掉一些不能整开p次方的数,然后去搜索,然后再搜索的过程中,减掉不符合条件的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

vector<int> fac, final_path, path;
int maxFacSum = -1;

void init(int n, int p) {
int i = 1, temp = 0;
while (temp <= n) {
fac.push_back(temp);
temp = pow(i, p);
i++;
}
}

void dfs(int cur, int k, int n, int facSum) {
if (k == 0 && n == 0) {
if (maxFacSum < facSum) {
maxFacSum = facSum;
final_path = path;
}
return;
}
if (k < 0 || n < 0) return;

if (cur >= 1) {
path.push_back(cur);
dfs(cur, k - 1, n - fac[cur], facSum + cur);
path.pop_back();
dfs(cur - 1, k, n, facSum);
}
}

int main() {
int k = 0, n = 0, p = 0;
scanf("%d %d %d", &n, &k, &p);
init(n, p);
dfs(fac.size() - 1, k, n, 0);
if (maxFacSum == -1) printf("Impossible");
else {
printf("%d = %d^%d", n, final_path[0], p);
for (int i = 1; i < final_path.size(); i++) {
printf(" + %d^%d", final_path[i], p);
}
}
return 0;
}

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse. First the playing order is randomly decided for NP programmers. Then every NG programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every NG winners are then grouped in the next match until a final winner is determined. For the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: NP and NG (<= 1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than NG mice at the end of the player’s list, then all the mice left will be put into the last group. The second line contains NP distinct non-negative numbers Wi (i=0,…NP-1) where each Wi is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,…NP-1 (assume that the programmers are numbered from 0 to NP-1). All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

按给定的顺序分组,找到每组中最重的老鼠,然后晋级下一轮,以此类推

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <queue>

using namespace std;

struct Mouse {
int weight, rank;
};

int main() {
int np = 0, ng = 0;
scanf("%d %d", &np, &ng);
struct Mouse *mouses = new struct Mouse[np];
for (int i = 0; i < np; i++) {
scanf("%d", &mouses[i].weight);
}
queue<int> compet;
for (int i = 0; i < np; i++) {
int t = 0;
scanf("%d", &t);
compet.push(t);
}
while (compet.size() > 1) {
int temp = compet.size(), group;
if (temp % ng == 0) group = temp / ng;
else group = temp / ng + 1;
for (int i = 0; i < group; i++) {
int k = compet.front();
for (int j = 0; j < ng; j++) {
if (i * ng + j >= temp) break;
int f = compet.front();
if (mouses[k].weight < mouses[f].weight) k = f;
mouses[f].rank = group + 1;
compet.pop();
}
compet.push(k);
}
}
mouses[compet.front()].rank = 1;
printf("%d", mouses[0].rank);
for (int i = 1; i < np; i++) {
printf(" %d", mouses[i].rank);
}
delete[] mouses;
return 0;
}

Given any permutation of the numbers {0, 1, 2,…, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way: Swap(0, 1) => {4, 1, 2, 0, 3} Swap(0, 3) => {4, 1, 2, 3, 0} Swap(0, 4) => {0, 1, 2, 3, 4} Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (<=105) followed by a permutation sequence of {0, 1, …, N-1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10 3 5 7 2 6 4 9 0 8 1

Sample Output:

9

这里当0处于0号位置时,查找要花费比较多得时间,所以用一个变量记录上次查找到得位置,那么下次查找就从这个记录的位置开始查找就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdio>
#include <algorithm>

using namespace std;

int main() {
int n = 0;
scanf("%d", &n);
int *pos = new int[n];
int unsort = 0;
for (int i = 0; i < n; i++) {
int num = 0;
scanf("%d", &num);
pos[num] = i;
if (i != pos[i] && i != 0) unsort++;
}
int cnt = 0, k = 0;
while (unsort > 0) {
if (pos[0] != 0) {
swap(pos[0], pos[pos[0]]);
cnt++;
unsort--;
} else {
while (k < n) {
if (k != pos[k]) {
swap(pos[0], pos[k]);
cnt++;
break;
}
k++;
}
}
}
printf("%d\n", cnt);
delete[] pos;
return 0;
}

reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given any two positive integers N (<105​) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

题意:如果一个数本身是素数并且这个数的反转也是素数,那么这个数就是反转素数。 首先将十进制的n转换成d进制的数,然后把这个数进行翻转,最后把翻转过的d进制的数转换成十进制的数。判断原先的数和反转过的数是否都是素数,即可得到结果。 根据进制转换的规则,这里省略了翻转的过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

bool is_prime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int convert_new_num(int n, int d) {
string s;
do {
s += to_string(n % d);
n /= d;
} while (n != 0);
int result = 0, radix = 1;
for (int i = (int) s.size() - 1; i >= 0; i--) {
result += radix * (s[i] - '0');
radix *= d;
}
return result;
}

int main() {
int n = 0, d = 0;
while (true) {
scanf("%d", &n);
if (n < 0) break;
scanf("%d", &d);
printf("%s\n", is_prime(n) && is_prime(convert_new_num(n, d)) ? "Yes" : "No");
}
return 0;
}

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<231).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format “factor[1]*factor[2]*…*factor[k]“, where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
5*6*7

直接遍历2~sqrt(n)的所有数字去查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>
#include <cmath>

int main() {
int num = 0;
scanf("%d", &num);
int sq = sqrt(num), index = 0, len = 0;
for (int i = 2; i <= sq; i++) {
long temp = 1, j = i;
while (true) {
temp *= j;
if (num % temp != 0) break;
if (j - i + 1 > len) {
index = i;
len = j - i + 1;
}
j++;
}
}
if (len == 0) printf("1\n%d", num);
else {
printf("%d\n%d", len, index);
for (int i = 1; i < len; i++) {
printf("*%d", index + i);
}
}
}

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student. For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space. The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority. If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

对全体学生按C、M、E、A排4次序,得到四个排名,使用map记录最后一次排完序的学生在数组中的下标位置。给出一个学生的id,如果这个id不在map中,输入N/A,否则通过map找到学生的位置,在print_rank函数中打印学生C、M、E、A的最好成绩。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>

using namespace std;

struct student {
char id[7];
int score[4], rank[4];
};

bool cmp_0(student a, student b) {
return a.score[0] > b.score[0];
}

bool cmp_1(student a, student b) {
return a.score[1] > b.score[1];
}

bool cmp_2(student a, student b) {
return a.score[2] > b.score[2];
}

bool cmp_3(student a, student b) {
return a.score[3] > b.score[3];
}

void print_rank(student s) {
int rank = s.rank[0], index = 0;
for (int i = 1; i <= 3; i++) {
if (rank > s.rank[i]) {
index = i;
rank = s.rank[i];
}
}
printf("%d ", rank);
switch (index) {
case 0:
printf("A\n");
break;
case 1:
printf("C\n");
break;
case 2:
printf("M\n");
break;
case 3:
printf("E\n");
break;
}
}

int main() {
int n = 0, m = 0;
scanf("%d %d", &n, &m);
struct student *students = new struct student[n];
for (int i = 0; i < n; i++) {
scanf("%s %d %d %d", students[i].id, &students[i].score[1], &students[i].score[2], &students[i].score[3]);
students[i].score[0] = (students[i].score[1] + students[i].score[2] + students[i].score[3]) / 3.0 + 0.5;
}

// sort by C Programming Language
sort(students, students + n, cmp_1);
for (int i = 0; i < n; i++) {
students[i].rank[1] = i + 1;
if (i != 0 && students[i].score[1] == students[i - 1].score[1]) {
students[i].rank[1] = students[i - 1].rank[1];
}
}

// sort by Mathematics
sort(students, students + n, cmp_2);
for (int i = 0; i < n; i++) {
students[i].rank[2] = i + 1;
if (i > 0 && students[i].score[2] == students[i - 1].score[2]) {
students[i].rank[2] = students[i - 1].rank[2];
}
}

// sort by English
sort(students, students + n, cmp_3);
for (int i = 0; i < n; i++) {
students[i].rank[3] = i + 1;
if (i > 0 && students[i].score[3] == students[i - 1].score[3]) {
students[i].rank[3] = students[i - 1].rank[3];
}
}

// sort by Average
sort(students, students + n, cmp_0);
map<string, int> position;
for (int i = 0; i < n; i++) {
position[students[i].id] = i + 1;
students[i].rank[0] = i + 1;
if (i > 0 && students[i].score[0] == students[i - 1].score[0]) {
students[i].rank[0] = students[i - 1].rank[0];
}
}

for (int i = 0; i < m; i++) {
char id[7];
scanf("%s", id);
if (position[id] == 0) {
printf("N/A\n");
} else {
print_rank(students[position[id] - 1]);
}
}
delete[] students;
return 0;
}

每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。

输入格式:

输入第一行给出一个正整数 N(≤10 5),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:

准考证号 得分 学校

其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。

输出格式:

首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

排名 学校 加权总分 考生人数

其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5整数部分考生人数是该属于单位的考生的总人数。 学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。

输入样例:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

输出样例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

使用map将每个学校的分数累计起来,使用另一个map统计人数,处理完数据后将这些数据存储到结构体数组中,按照总分的整数部分排序,如果总分相等则按照人数进行排序,如果人数相等,则按照学校名字的字典序排序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdio>
#include <string>
#include <cctype>
#include <map>
#include <algorithm>

using namespace std;

struct department {
string school;
int score, cnt;

department() {}

department(string _school, int _score, int _cnt) : school(_school), score(_score), cnt(_cnt) {}
};

bool cmp(department a, department b) {
return a.score == b.score ? (a.cnt == b.cnt ? a.school < b.school : a.cnt < b.cnt) : a.score > b.score;
}

string to_lower(string s) {
for (int i = 0; i < s.size(); i++) {
if (isupper(s[i])) s[i] += 32;
}
return s;
}


int main() {
char id[10], school[10];
int score = 0, n = 0, idx = 0, rank = 1;
scanf("%d", &n);
map<string, double> m;
map<string, int> m_cnt;
for (int i = 0; i < n; i++) {
scanf("%s %d %s", id, &score, school);
string s = to_lower(school);
m_cnt[s]++;
if (id[0] == 'T') {
m[s] += score * 1.5;
} else if (id[0] == 'A') {
m[s] += score;
} else if (id[0] == 'B') {
m[s] += score / 1.5;
}
}

department departments[100010];
for (auto it = m.begin(); it != m.end(); it++) {
departments[idx++] = department(it->first, (int) it->second, m_cnt[it->first]);
}
sort(departments, departments + m.size(), cmp);
printf("%lu\n", m.size());
for (int i = 0; i < m.size(); i++) {
if (i >= 1 && departments[i - 1].score != departments[i].score) {
rank = i + 1;
}
printf("%d %s %d %d\n", rank, departments[i].school.c_str(), departments[i].score, departments[i].cnt);
}
return 0;
}

外观数列是指具有以下特点的整数序列:

d, d1, d111, d113, d11231, d112213111, …

它从不等于 1 的数字 d 开始,序列的第 n+1 项是对第 n 项的描述。比如第 2 项表示第 1 项有 1 个 d,所以就是 d1;第 2 项是 1 个 d(对应 d1)和 1 个 1(对应 11),所以第 3 项就是 d111。又比如第 4 项是 d113,其描述就是 1 个 d,2 个 1,1 个 3,所以下一项就是 d11231。当然这个定义对 d = 1 也成立。本题要求你推算任意给定数字 d 的外观数列的第 N 项。

输入格式:

输入第一行给出 [0,9] 范围内的一个整数 d、以及一个正整数 N(≤ 40),用空格分隔。

输出格式:

在一行中给出数字 d 的外观数列的第 N 项。

输入样例:

1 8

输出样例:

1123123111

使用字符串进行处理,比较后一个字符和前一个字符是否相等,如果相等,则cnt++,否则cnt=1,并将字符和cnt合并进结果数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdio>
#include <string>

using namespace std;

string generate_sequence(string s) {
if (s.size() == 1) return s + to_string(1);
string result;
int cnt = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] == s[i - 1]) cnt++;
else {
result += s[i - 1] + to_string(cnt);
cnt = 1;
}
}
if (s[s.size() - 1] == s[s.size() - 2]) {
result += s[s.size() - 1] + to_string(cnt);
} else {
result += s[s.size() - 1] + to_string(1);
}
return result;
}


int main() {
int d = 0, n = 0;
scanf("%d %d", &d, &n);
string s = to_string(d);
for (int i = 1; i < n; i++) {
s = generate_sequence(s);
}
printf("%s\n", s.c_str());
}

给定 N 张卡片,正面分别写上 1、2、……、N,然后全部翻面,洗牌,在背面分别写上 1、2、……、N。将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差?

输入格式:

输入第一行给出一个正整数 N(2 ≤ N ≤ 10 000),随后一行给出 1 到 N 的一个洗牌后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字。

输出格式:

按照“差值 重复次数”的格式从大到小输出重复的差值及其重复的次数,每行输出一个结果。

输入样例:

8
3 5 8 6 2 1 4 7

输出样例:

5 2
3 3
2 2

这里可以利用map的key是有序的特点,将差值和重复次数存储在map,反向输出即可得到答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <map>
#include <cmath>

using namespace std;

int main() {
int n = 0, x = 0;
scanf("%d", &n);
map<int, int> m;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
m[abs(x - i)]++;
}
for (auto it = m.rbegin(); it != m.rend(); it++) {
if (it->second != 1)
printf("%d %d\n", it->first, it->second);
}
return 0;
}
0%