java编程强化练习(四)

1.最长连续升序整数序列

【问题描述】

输入一组无序的整数(整数都大于等于0),编程求出其中最长的连续升序子序列(该序列中后一个整数比前一个整数多1,序列的长度是指序列中整数的个数,长度应大于等于2)。例如输入13个整数:3520 0 3 89 56 88 3521 9 90 1 99 2 87,其中连续升序子序列有3个:3520 3521,0 1 2 3和87 88 89 90,长度分别为2、4、4,所以后两个子序列都是最长的连续升序子序列。

【输入形式】

先从标准输入读入整数的个数(大于等于1,小于等于100),然后在下一行输入这些整数,各整数之间以一个空格分隔。

【输出形式】

先向标准输出输出最长连续升序子序列的长度,然后从下一行开始按升序分行输出最长连续升序子序列,各整数之间以一个空格分隔,每行最后一个整数后也要有一个空格。

若没有连续升序子序列,直接输出数字0。

【样例输入】

13

3520 0 3 89 56 88 3521 9 90 1 99 2 87

【样例输出】

4

0 1 2 3 

87 88 89 90 

【样例说明】

输入的整数个数为13,其中最长连续升序子序列的长度为4,有两个最长连续升序子序列,分别按升序分行输出。

【评分标准】该程序要求输出最长连续升序子序列。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class LongestAscendingSequence {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = scanner.nextInt();
        }
        scanner.close();

        Arrays.sort(numbers); // 对输入数据进行排序

        ArrayList<ArrayList<Integer>> allSequences = new ArrayList<>();
        ArrayList<Integer> currentSequence = new ArrayList<>();
        int maxLength = 0;

        for (int i = 0; i < n - 1; i++) {
            if (numbers[i + 1] - numbers[i] == 1) {
                if (currentSequence.isEmpty()) {
                    currentSequence.add(numbers[i]);
                }
                currentSequence.add(numbers[i + 1]);
            } else {
                if (currentSequence.size() > maxLength) {
                    allSequences.clear();
                    allSequences.add(new ArrayList<>(currentSequence));
                    maxLength = currentSequence.size();
                } else if (currentSequence.size() == maxLength) {
                    allSequences.add(new ArrayList<>(currentSequence));
                }
                currentSequence.clear();
            }
        }

        if (currentSequence.size() > maxLength) {
            allSequences.clear();
            allSequences.add(new ArrayList<>(currentSequence));
            maxLength = currentSequence.size();
        } else if (currentSequence.size() == maxLength) {
            allSequences.add(new ArrayList<>(currentSequence));
        }

        if (maxLength >= 2) {
            System.out.println(maxLength);
            for (ArrayList<Integer> sequence : allSequences) {
                for (int num : sequence) {
                    System.out.print(num + " ");
                }
                System.out.println();
            }
        } else if (maxLength == 1) {
            System.out.println("2"); // 如果只有两个连续的整数,输出长度为2
            for (ArrayList<Integer> sequence : allSequences) {
                for (int num : sequence) {
                    System.out.print(num + " ");
                }
                System.out.println();
            }
        } else {
            System.out.println("0");
        }
    }
}

2.整数出现次数

【问题描述】

输入一组无序的整数,编程输出其中出现次数最多的整数及其出现次数。

【输入形式】

先从标准输入读入整数的个数(大于等于1,小于等于100),然后在下一行输入这些整数,各整数之间以一个空格分隔。

【输出形式】

在标准输出上输出出现次数最多的整数及其出现次数,两者以一个空格分隔;若出现次数最多的整数有多个,则按照整数升序分行输出。

【样例输入】

10

0 -50 0 632 5813 -50 9 -50 0 632

【样例输出】

-50 3

0 3

【样例说明】

输入了10个整数,其中出现次数最多的是-50和0,都是出现3次。

【评分标准】该程序要求输出出现次数最多的整数和出现次数。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeSet;

public class MostFrequentInteger {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        scanner.close();

        Map<Integer, Integer> countMap = new HashMap<>();

        for (int num : arr) {
            if (countMap.containsKey(num)) {
                countMap.put(num, countMap.get(num) + 1);
            } else {
                countMap.put(num, 1);
            }
        }

        int maxCount = 0;
        TreeSet<Integer> maxCountNumbers = new TreeSet<>();

        for (int num : countMap.keySet()) {
            int count = countMap.get(num);
            if (count > maxCount) {
                maxCount = count;
                maxCountNumbers.clear();
                maxCountNumbers.add(num);
            } else if (count == maxCount) {
                maxCountNumbers.add(num);
            }
        }

        for (int num : maxCountNumbers) {
            System.out.println(num + " " + maxCount);
        }
    }
}

3.消除游戏

【问题描述】

消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消除。当有多处可以被消除时,这些地方的棋子将同时被消除。

现在给定一个n行m列的棋盘,棋盘中的每一个方格上有一个棋子(用数字1-9表示各种颜色的棋子),请给出经过消除后的棋盘。

请注意:一个棋子可能在某一行和某一列同时被消除。

【输入形式】

从标准输入读取数据,第一行包含两个整数n和m,分别表示棋盘的行数和列数(行数和列数都大于等于3,小于等于9),以一个空格分隔这两个整数。

接下来输入n行,每行m个整数,用一个空格分隔各个整数,这些整数分别表示每一个方格中棋子的颜色(大于等于1,小于等于9)。

【输出形式】

向标准输出上输出n行,每行m个整数,相邻整数之间以一个空格分隔,表示经过消除后的棋盘。如果一个方格中的棋子被消除,则对应的方格输出数字0,否则输出代表原棋子颜色的整数。每行最后一个整数后也要有一个空格。

【样例1输入】

4 5

2 2 3 1 2

3 4 5 1 4

2 3 2 1 3

2 2 2 4 4

【样例1输出】

2 2 3 0 2 

3 4 5 0 4 

2 3 2 0 3 

0 0 0 4 4 

【样例1说明】

棋盘中第4列的1和第4行的2可以被消除,其它方格中的棋子均被保留。

【样例2输入】

4 5

2 2 3 1 2

3 1 1 1 1

2 3 2 1 3

2 2 3 3 3

【样例2输出】

2 2 3 0 2 

3 0 0 0 0 

2 3 2 0 3 

2 2 0 0 0 

【样例2说明】

棋盘中第4列的1,第二行的1和第4行的3可以被消除,其它方格中的棋子均被保留。

【评分标准】该程序要求输出消除后的棋盘状态。

import java.util.Scanner;

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

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] board = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                board[i][j] = scanner.nextInt();
            }
        }
        scanner.close();
        eliminate(board, n, m);
        printBoard(board, n, m);
    }

    public static void eliminate(int[][] board, int n, int m) {
        boolean[][] eliminated = new boolean[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int color = board[i][j];

                if (color != 0) {

                    int count = 1;
                    for (int k = j + 1; k < m && board[i][k] == color; k++) {
                        count++;
                    }
                    if (count >= 3) {
                        for (int k = j; k < j + count; k++) {
                            eliminated[i][k] = true;
                        }
                    }

                    count = 1;
                    for (int k = i + 1; k < n && board[k][j] == color; k++) {
                        count++;
                    }
                    if (count >= 3) {
                        for (int k = i; k < i + count; k++) {
                            eliminated[k][j] = true;
                        }
                    }
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (eliminated[i][j]) {
                    board[i][j] = 0;
                }
            }
        }
    }

    public static void printBoard(int[][] board, int n, int m) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(board[i][j]);
                if (j < m - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

4.小数分数转换

【问题描述】

从标准输入中输入一个小数,编写程序将其转换成相应的分数显示,即转换为几又几分之几。
要求:
1、输入的小数包括整数部分、小数点和小数部分;整数部分和小数部分最多有7位数字;整数部分可以为0,若整数部分大于等于1,则其最高位不为0;小数部分的末尾数字不为零。
2、输出的分数应为最简分数,由三个数字组成:第一个数字代表整数部分(若分数小于1,则为0,否则输出的整数的最高位不为0),第二个数字代表分子,第三个数字代表分母,分子比分母小且不能再约分。

【输入形式】

从标准输入中输入一个小数后打回车。

【输出形式】

将三个整数输出到标准输出,并且分别以一个空格分隔,最后一个整数后没有空格,有回车换行。

【样例1输入】

0.35

【样例1输出】

0 7 20

【样例2输入】

1050.0144

【样例2输出】

1050 9 625

【样例说明】

样例1中输入的小数为0.35,整数部分为0,小数部分转换为分数为35/100,约分成最简分数为7/20。
样例2中输入的小数为1050.0144,整数部分为1050,小数部分转换为分数为144/10000,约分成最简分数为9/625。


【评分标准】该题要求编程实现小数到分数的转换。

import java.util.Scanner;

public class DecimalToFraction {


    public static int gcd(int a, int b) {
        int t = 0;
        if(a < b){
            t = a;
            a = b;
            b = t;
        }
        int c = a % b;
        if(c == 0){
            return b;
        }
        else{
            return gcd(b, c);
        }
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);

        String number;
        int decimal;
        int a, b,c;
        int g;

        number = in.next();

        String[] array = new String[2];
        array = number.split("\\.");
        a = Integer.parseInt(array[0]);
        decimal = Integer.parseInt(array[1]);
//		System.out.println(decimal);

        int length = array[1].length();
        b = decimal;
        c = (int)Math.pow(10, length);
//		System.out.println(b);
//		System.out.println(c);
        g = gcd(b,c);
        System.out.println(a + " " + b/g +" " + c/g);

    }

}

5.最大数
【输入形式】从键盘输入三个整数,以空格间隔
【输出形式】输出最大的一个整数
【样例输入】1 2 3回车
【样例输出】3
【样例说明】
【评分标准】10

import java.util.Scanner;

public class MaxNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();
        int num3 = scanner.nextInt();
        scanner.close();

        int max = Math.max(num1, Math.max(num2, num3));

        System.out.println(max);
    }
}

6. 程序结构抽取

【问题描述】

有一种比较两程序是否相似的方法:将程序中控制结构信息按出现次序抽取出来形成一个控制流串,然后依据两个程序的控制流串来计算两个程序的相似度。

编写一程序,抽取一个C程序的控制流串。要求只考虑if, else, for, while, switch, case等控制流关键字,其它控制流不被考虑。被处理的C程序满足:
1. 能够通过C编译;
2. 一行可能有多条语句;
3. 注释、字符串常量及其它标识符中不含控制流关键字串;

【输入形式】

要处理的C程序保存在当前目录下,文件名为:in.c。

【输出形式】

按出现的先后顺序将控制流关键字串输出到当前目录下的out.txt文件中,各关键字串之间没有任何分隔符。若没有控制流关键字,则将No answer输出到文件中。

【样例输入1】

假设当前目录下in.c的内容为:
#include <stdio.h>
int main()
{
    int n,a,c1,c2,i;
    scanf("%d",&n);
    c1=c2=0;
    for ( i=0; i<n; i++ )
    {
        scanf("%d",&a);
        if ( a>=0 )
           c1++;
        else
           c2++;
    }
    printf("%d %d",&c1,&c2);
}

【样例输出1】

在当前目录下将创建out.txt文件,其内容应为:
forifelse

【样例输入2】

假如当前目录下in.c源程序风格不太好,内容如下:
#include <stdio.h>
main()
{
int a,b,max,min;
scanf("%d%d",&a,&b);
if(a>b){max=a;min=b;}else if(a<b){max=b;min=a;}else{max=min=a;}
printf("%d %d",max,min);
}

【样例输出2】

在当前目录下将创建out.txt文件,其内容应为:
ifelseifelse

【样例说明】

样例1中源程序只包含for、if、else三个控制流关键字,因此按照出现顺序将这三个关键字输出到文件out.txt中;样例2中控制流关键字出现顺序为:if、else、if、else,故将这四个关键字输出到out.txt中。

算法提示:从in.c中读取标识符时,可将除字母、数字、下划线之外的其它字符作为标识符的分隔符。

【评分标准】

该题要求按顺序输出控制流关键字,共有5个测试点。


import java.io.*;
import java.util.regex.*;

public class ControlFlowExtractor {
    public static void main(String[] args) {
        try {
            FileReader fileReader = new FileReader("in.c");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            FileWriter fileWriter = new FileWriter("out.txt");

            String line;
            String controlFlowString = "";
            boolean insideComment = false;

            Pattern controlFlowPattern = Pattern.compile("\\b(if|else|for|while|switch|case)\\b");

            while ((line = bufferedReader.readLine()) != null) {
                line = line.trim();

                if (line.startsWith("/*")) {
                    insideComment = true;
                }

                if (line.endsWith("*/")) {
                    insideComment = false;
                }

                if (!insideComment) {
                    Matcher matcher = controlFlowPattern.matcher(line);
                    while (matcher.find()) {
                        controlFlowString += matcher.group();
                    }
                }
            }

            if (!controlFlowString.isEmpty()) {
                fileWriter.write(controlFlowString);
            } else {
                fileWriter.write("No answer");
            }

            bufferedReader.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7.单词索引编排

【问题描述】

打开一英文文章(保存在一个现有文件in.txt中),为该文件生成词汇表(存到另一个文件out.txt中),要求词汇表中的单词以字典序由小到大存放(只由连续字母组成,且全为小写字母,不重复)。
假设:
1、该文章有可能没有经过排版,格式有可能杂乱无章,也有可能没有写完整。
2、文章中的单词个数不超过1000个,每个单词的长度不超过50个字母。

【输入形式】

保存英文文章的文件in.txt位于当前目录下。

【输出形式】

将生成的词汇表以字典序由小到大输出到当前目录下的文件out.txt中,每个单词单独占一行。

【样例输入1】

假设文件in.txt内容为:

There are two versions of the international standards for C.
The first version was ratified in 1989 by the American National
Standards Institue (ANSI) C standard committee.It is often
referred as ANSI C or C89. The secand C standard was completed
in 1999. This standard is commonly referred to as C99. C99 is a
milestone in C's evolution into a viable programming language
for numerical and scientific computing.

【样例输出1】
文件out.txt中的词汇表应为:

a
american
and
ansi
are
as
by
c
committee
commonly
completed
computing
evolution
first
for
in
institue
international
into
is
it
language
milestone
national
numerical
of
often
or
programming
ratified
referred
s
scientific
secand
standard
standards
the
there
this
to
two
version
versions
viable
was

【样例输入2】

假设文件in.txt内容为:

There are two versions of the international standards for

【样例输出2】
文件out.txt中的词汇表应为:

are
for
international
of
standards
the
there
two
versions

【样例说明】

将in.txt中出现的所有由字母组成的单词(出现的单个字母也作为单词)全部转换成小写后,按照字典序输出到文件out.txt中(每个单词独占一行,重复出现的只输出一次)。
注意:样例2中in.txt文件内容还不完整,单词for后没有任何字符。

【评分标准】本题要求为指定的文件编写单词索引。

import java.io.*;
import java.util.*;

public class WordIndexer  {

    public static void main(String[] args) {
        try {

            BufferedReader reader = new BufferedReader(new FileReader("in.txt"));
            String line;
            StringBuilder content = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                content.append(line).append(" ");
            }
            reader.close();


            String[] words = content.toString().toLowerCase().split("\\P{L}+");


            Set<String> uniqueWords = new TreeSet<>(Arrays.asList(words));


            BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt"));
            for (String word : uniqueWords) {
                writer.write(word);
                writer.newLine();
            }
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

8.单词检查

【问题描述】

已知有一个正确单词索引表(保存在当前目录下的文件index.txt中,且全为小写字母,按照字典序由小到大排列,每个单词独占一行),编写程序利用该单词表对某一英文文章(保存在当前目录下的另一个文件in.txt中)进行单词正确性检查,若该英文文章中出现的单词(只有连续字母组成)没有出现在单词索引文件中(检查时大小写无关),则将该出错的单词(其中的字母全部转换为小写)输出到当前目录下的另一文件error.txt中,每个单词独占一行,并且以字典序由小到大的顺序输出。
假设:
1、in.txt中的文章有可能没有经过排版,格式有可能杂乱无章,也有可能没有写完整。
2、index.txt中的单词个数不超过1000个,每个单词的长度不超过50个字母。
3、若出错的单词多次出现,则多次输出。

【输入形式】

保存单词索引表的文件index.txt和保存英文文章的文件in.txt都位于当前目录下。

【输出形式】

将出错的单词以字典序由小到大的顺序输出到当前目录下的文件error.txt中,每个单词单独占一行,多次出错的单词多次输出。若没有出现错误单词,则什么也不输出。

【样例输入1】

假设文件in.txt内容为:

There are two verrsions of the international standards for C.
Thee first version was ratified in 1989 by the American National
Standards Institue (ANS1) C standard committee.It is often
referred as ANS1 C or C89. The secand C standard was completed
in 1999. This standard is comonly referred to as C99. C99 is a
milestone in C's evolution into a viable programing languga
for numerical and scientific computing.

文件index.txt中的单词索引表内容为:

a
american
and
ansi
are
as
by
c
committee
commonly
completed
computing
evolution
first
for
in
institue
international
into
is
it
language
milestone
national
numerical
of
often
or
programming
ratified
referred
s
scientific
secand
standard
standards
the
there
this
to
two
version
versions
viable
was

【样例输出1】

文件error.txt中出错的单词应为:

ans
ans
comonly
languga
programing
thee
verrsions

【样例1说明】

用index.txt中的单词索引表对in.txt中出现的每一个单词进行检查,检查时大小写无关,所以第一个单词There出现在索引表中,不是错误单词;单词verrsions没有出现在索引表中,拼写错误,所以作为出错单词输出;单词ANSI拼写成了ANS1,将其中字母都转换为小写后输出,并且多次出现,多次输出;其他出错单词类似。错误单词输出按照字典序由小到大输出到error.txt文件中。

【样例输入2】

假设文件in.txt内容为:

There are two versions of the international standard fo

文件index.txt中的单词索引表内容为:

are
for
international
of
standards
the
there
two
versions

【样例输出2】

文件error.txt中出错的单词应为:

fo
standard

【样例2说明】

文件in.txt中的单词standard没有出现在索引表文件index.txt中,所以作为错误单词输出。
注意:样例2中in.txt文件内容还不完整,最后的单词fo后没有任何字符,fo也没有出现在索引表中,所以也作为错误单词输出。

【评分标准】本题要求对文章的单词进行检查。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WordChecker {
    public static void main(String[] args) {
        String indexFile = "index.txt";
        String inputFile = "in.txt";
        String outputFile = "error.txt";

        List<String> indexWords = readIndexWords(indexFile);
        Set<String> indexSet = new HashSet<>(indexWords);
        List<String> errorWords = findErrorWords(inputFile, indexSet);
        writeErrorWords(outputFile, errorWords);
    }

    private static List<String> readIndexWords(String indexFile) {
        List<String> indexWords = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(indexFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                indexWords.add(line.toLowerCase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWords;
    }

    private static List<String> findErrorWords(String inputFile, Set<String> indexSet) {
        List<String> errorWords = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] words = line.split("[^a-zA-Z]+"); // Split by non-alphabet characters
                for (String word : words) {
                    if (!word.isEmpty() && !indexSet.contains(word.toLowerCase())) {
                        errorWords.add(word.toLowerCase());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collections.sort(errorWords); // Sort error words in alphabetical order
        return errorWords;
    }

    private static void writeErrorWords(String outputFile, List<String> errorWords) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
            for (String word : errorWords) {
                writer.write(word);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值