CS102A - Assignment2相关代码及解释

it2024-01-22  61

Problem 1. Hard Math Problem[Easy]

Description

代码

import java.util.Scanner; public class HardMathProblem { public static void main(String[] args) { Scanner input = new Scanner(System.in); double a = input.nextDouble(); //lower bound double b = input.nextDouble(); //upper bound // -10 <= a < b <= 10 int n = input.nextInt(); //non-negative number 最高次项为n 一共有n+1项 // 1 <= n <= 3 也就是说最多要求4个系数 //也就是说一共有n+1个系数cn要求 int CoefficientNumber = n + 1; double[] coefficients = new double[CoefficientNumber]; //给所有的系数赋值,其中c1对应的是coefficient[0],cn+1对应的是coefficient[n] for (int i = 0; i < coefficients.length; i++) { coefficients[i] = input.nextDouble(); } double sum = 0; int times = (int) ((b - a) / 0.0001); double variable; for (int i = 0; i < times; i++) { variable = a + 0.0001 * i; for (int j = 0; j < coefficients.length; j++) { int square = n - j; sum += coefficients[j] * (Math.pow(variable, square)) * 0.0001; } } /*double[] variables = new double[times]; for (int i = 0; i < variables.length; i++) { variables[i] = a + 0.0001 * i; } for (int i = 0; i < variables.length; i++) { //遍历每一个变量 for (int j = 0; j < CoefficientNumber; j++) { //遍历每一个系数 sum += coefficients[i]*Math.pow(variables[i],(n-j))*0.0001; } } */ System.out.printf("%.1f\n", sum); /* double[] x = new double[times]; for (int i = 0; i < times; i++) { x[i] = a + 0.0001 * i; } for (int i = 0; i < n; i++) { for (int j = 0; j < times; j++) { sum += coefficients[i] * Math.pow(x[i], (n - i)); } } */ /*double x = a; for (int j = 0; j < n + 1; j++) { for (int i = 0; i < times; i++) { sum += coefficients[j] * Math.pow(x, (n - j)); x += 0.0001 * i; } x = a; } sum = sum * 0.0001; System.out.printf("%.1f\n", sum); */ } }

Problem 2 . Shattered [Hard]

Description

代码

import java.util.Scanner; public class Main6 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] map = new int[n + 2][m + 2]; String[] NotationString = new String[n]; //以下内容duck不必 for (int i = 0; i < n; i++) { NotationString[i] = in.next(); } char[][] ZiFu = new char[n][m]; for (int i = 0; i < NotationString.length; i++) { ZiFu[i] = new char[m]; for (int j = 0; j < ZiFu[i].length; j++) { String WhatWeNeed = NotationString[i]; char[] temp = WhatWeNeed.toCharArray(); ZiFu[i][j] = temp[j]; } } for (int i = 0; i < ZiFu.length; i++) { for (int j = 0; j < m; j++) { if (ZiFu[i][j] == 'C') { map[i + 1][j + 1] = 1; } } } if (!GetTheWay(map, n, m, 0, 0)) { System.out.println("No"); } } private static boolean GetTheWay(int[][] map, int n, int m, int i, int i1) { if (n == 1 && m == 1) { System.out.println("Yes"); System.out.println("(1,1)"); return true; } int[][] delta = {{0, 0}, {1, 0}, {-1, 0}, {0, -1}, {0, 1}}; for (int a = 1; a < delta.length; a++) { int x = n + delta[a][0], y = m + delta[a][1]; if (x >= 0 && y >= 0 && (x != i || y != i1) && map[x][y] == 1) { if (GetTheWay(map, x, y, n, m)) { System.out.printf("(%d,%d)\n", n, m); return true; } } } return false; } }

Problem 3 .Harmonic Strings [Medium]

Description

代码

import java.util.Scanner; public class Main { static long P = 998244353; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n <= 1 || n > 100) return; in.nextLine(); String[] WhatWeGet = new String[n]; for (int i = 0; i < WhatWeGet.length; i++) { WhatWeGet[i] = in.nextLine(); } int[] length = new int[WhatWeGet.length]; for (int i = 0; i < WhatWeGet.length; i++) { length[i] = WhatWeGet[i].length(); } char[][] ZiFu = new char[WhatWeGet.length][]; for (int i = 0; i < WhatWeGet.length; i++) { ZiFu[i] = new char[length[i]]; for (int j = 0; j < ZiFu[i].length; j++) { String WhatWeNeed = WhatWeGet[i]; char[] temp = WhatWeNeed.toCharArray(); ZiFu[i][j] = temp[j]; } } long[][] ASCIICode = new long[WhatWeGet.length][]; for (int i = 0; i < WhatWeGet.length; i++) { ASCIICode[i] = new long[length[i]]; for (int j = 0; j < ASCIICode[i].length; j++) { ASCIICode[i][j] = ZiFu[i][j]; } } long[] freq = new long[ASCIICode.length]; for (int i = 0; i < ASCIICode.length; i++) { long l = ASCIICode[i].length; for (int j = 0; j < l; j++) { long temp = ASCIICode[i][j]; for (int m = 0; m < j; m++) { temp = (temp * l) % P; } freq[i] += temp % P; } freq[i] %= P; } long max = 0; long min = freq[0]; for (long j : freq) { if (j > max) { max = j; } if (j < min) { min = j; } } long GreatestDivisor = gcd(max, min); max = max / GreatestDivisor; min = min / GreatestDivisor; System.out.printf("%d %d", max, min); } private static long gcd(long max, long min) { return min == 0 ? max : gcd(min, max % min); } }

Problem 4. Silly Calculator [Medium]

Description

代码

import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String WhatWeGet = in.nextLine(); int countNum, countOperator = 0; for (int i = 1; i < WhatWeGet.length(); i++) { char Get = WhatWeGet.charAt(i); if (Get == '+' || Get == '-' || Get == '*' || Get == '/') { countOperator++; } } countNum = countOperator + 1; String[] Number = new String[countNum]; char[] Operator = new char[countOperator]; for (int i = 1, count2 = 0; i < WhatWeGet.length(); i++) { char Get = WhatWeGet.charAt(i); if (Get == '+' || Get == '-' || Get == '/' || Get == '*') { Operator[count2] = Get; count2++; } } int count1 = 0; Pattern p = Pattern.compile("(?<!\\d)-?\\d+(\\.\\d+)?"); Matcher m = p.matcher(WhatWeGet); while (m.find()) { Number[count1] = m.group(); count1++; } if (WhatWeGet.charAt(0) == '-') { Number[0] = Number[0]; } double[] number = new double[countNum]; for (int i = 0; i < number.length; i++) { number[i] = Double.parseDouble(Number[i]); } for (int i = 0; i < Operator.length; i++) { int result = 0; switch (Operator[i]) { case '+': result = (int) Math.floor(number[i] + number[i + 1]); break; case '-': result = (int) Math.floor(number[i] - number[i + 1]); break; case '/': result = (int) Math.floor(number[i] / number[i + 1]); break; case '*': result = (int) Math.floor(number[i] * number[i + 1]); break; } number[i + 1] = result; } System.out.println((int) number[countOperator]); } } Files

Problem 5. Jimmy’s Lottery [Medium]

Description

代码

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int m = input.nextInt(); int q = input.nextByte(); int[] standard = new int[m]; for (int i = 0; i < standard.length; i++) { standard[i] = input.nextInt(); } int[][] cases = new int[n][m]; for (int i = 0; i < cases.length; i++) { for (int j = 0; j < cases[i].length; j++) { cases[i][j] = input.nextInt(); } } int[] answers = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (cases[i][j] == standard[j]) { answers[i] += 2; } if (Math.abs(cases[i][j] - standard[j]) == 1 || Math.abs(cases[i][j] - standard[j]) == 2) { answers[i]++; } } } int StandardSum = 0; for (int i = 0; i < m; i++) { StandardSum += standard[i]; } int LuckyNumber = StandardSum % q; int[] sums = new int[n]; int[] Shang = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { sums[i] += cases[i][j]; Shang[i] = sums[i] % q; } } for (int i = 0; i < n; i++) { if (Shang[i] == LuckyNumber) { answers[i] += 2; } int abs = Math.abs(Shang[i] - LuckyNumber); if (abs == 1 || abs == 2) { answers[i]++; } System.out.println(answers[i]); } } }

Problem 6. Easy Multiplication Plus [Bonus]

Description

代码

import java.io.*; import java.util.StringTokenizer; public class Main { final static public int mod = 1000000007; public static int[][] multiply(int[][] a, int[][] b) { int[][] c = new int[a.length][b[0].length]; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { long temp = 0; for (int k = 0; k < a[0].length; k++) { temp += ((long) a[i][k] * b[k][j]) % mod; temp %= mod; } c[i][j] = (int) temp; } } return c; } public static int[][] QuickPower(int[][] a, int n) { int[][] ans = new int[a.length][a[0].length]; for (int i = 0; i < ans.length; i++) { for (int j = 0; j < a[0].length; j++) { if (i == j) { ans[i][j] = 1; } else { ans[i][j] = 0; } } } while (n != 0) { if ((n & 1) == 1) { ans = multiply(ans, a); } a = multiply(a, a); n = n >> 1; } return ans; } public static void main(String[] args) { Reader reader = new Reader(System.in); int N = reader.nextInt();//一共有N个矩阵 int k = reader.nextInt();//幂 int[][][] matrices = new int[N][][];//N个矩阵的三维数组 for (int i = 0; i < N; i++) { int row = reader.nextInt(); int col = reader.nextInt(); matrices[i] = new int[row][col]; for (int j = 0; j < row; j++) { for (int l = 0; l < col; l++) { matrices[i][j][l] = reader.nextInt(); } } } int[][] ans; if (N >= 2) { ans = multiply(matrices[0], matrices[1]); for (int i = 2; i < N; i++) { ans = multiply(ans, matrices[i]); } } else { ans = matrices[0]; } int[][] result = QuickPower(ans, k); int n = result.length, m = result[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } private static class Reader { BufferedReader in; StringTokenizer tokenizer; public Reader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } private String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
最新回复(0)