Java 实验一些题
Lab2_3
【问题描述】
在屏幕上显示如下杨辉三角形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
【输入形式】
从键盘输入整数n(n>=0且n<=12)
【输出形式】
在屏幕上输出n+1行杨辉三角形。
【输入样例】
3
【输出样例】
------1
----11
–121
1331
其中-和*都是空格位
普通解法
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
| public class lab2_3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int lines = scanner.nextInt() + 1; int[][] arr = new int[lines][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; arr[i][0] = 1; arr[i][i] = 1; } for (int i = 2; i < arr.length; i++) { for (int j = 1; j < i; j++) { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } } for (int i = 0; i < arr.length; i++) { for (int j = (arr.length - i) * 2; j >= 0; j--) { System.out.printf(" "); } for (int j = 0; j < arr[i].length; j++) { System.out.printf("%4d", arr[i][j]); } System.out.println(); } } }
|
队列实现
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
| class Myqueue { public int[] elem; public int usedSize; Myqueue() { this.elem = new int[10]; } public boolean isFull() { return this.elem.length == this.usedSize; } public void push(int val) { if (isFull()) { this.elem = Arrays.copyOf(this.elem, this.elem.length * 2); } else { this.elem[this.usedSize] = val; usedSize++; } } public boolean isEmpty() { return this.usedSize == 0; } public int pop() { if (isEmpty()) { return -1; } else { int res = this.elem[0]; for (int i = 0; i < elem.length - 1; i++) { elem[i] = elem[i + 1]; } usedSize--; return res; } } } public class lab2_3_P { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int lines = scanner.nextInt() + 1; Myqueue myqueue = new Myqueue(); for (int i = 0; i < lines; i++) { if (i == 0) { myqueue.push(0); myqueue.push(1); } else if (i > 0) { for (int j = 0; j < i + 2; j++) { if (j == 0) { myqueue.push(0); continue; } int e = myqueue.elem[0] + myqueue.elem[1]; myqueue.push(e); int out = myqueue.pop(); if(out != 0){ System.out.printf("%d ", out);} } System.out.println(); } } } }
|
lab7-1
现有如下格式的成绩单(文本格式)score.txt。
name:zhangsan,shuxue 72,wuli 67,yingyu 70
name:lisi,shuxue 92,wuli 98,yingyu 88
name:wangwu,shuxue 68,wuli 80,yingyu 77
要求按行读取成绩单。并在该行的后面加上该同学的总成绩。然后将该行写入到一个名字为scoreAnalysis.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
| import java.io.*; import java.util.*;
class Fenxi { public static String analyze(String line) { String[] parts = line.split(","); double sum = 0; for (int i = 1; i < parts.length; i++) { String[] pair = parts[i].split(" "); sum += Double.parseDouble(pair[1]); } return line.trim() + " zongFen:" + sum; } }
public class AnalysisResult { public static void main(String[] args) { try { Scanner scanner = new Scanner(new File("score.txt")); PrintWriter writer = new PrintWriter(new File("scoreAnalysis.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String result = Fenxi.analyze(line); writer.println(result); } scanner.close(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
|