科大讯飞笔试题
1.一个M*N棋盘,每个格子上放着一个礼物,只能往下或者往右,到右下角拿到的礼物价值最多是多少。
2.选择排序
3.除去字符串多余的_,比如____ab__d_dss___变为ab_d_dss
4.质因数分解
1.
import java.util.Scanner; public class text { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] num = scanner.nextLine().split(","); int m = Integer.parseInt(num[0]); int n = Integer.parseInt(num[1]); int[][] grid = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ grid[i][j] = scanner.nextInt(); } } int[] dp = new int[n + 1]; dp[0] = 0; for(int i = 1; i <= m; i++){ for(int j =1; j <= n; j++){ dp[j] = Math.max(dp[j], dp[j - 1]) + grid[i - 1][j - 1]; } } System.out.println(dp[n]); } }2
import java.util.Scanner; public class text3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); scanner.nextLine(); String s = scanner.nextLine(); String[] ss = s.split(","); int[] num = new int[m]; for(int i = 0; i < m; i++){ num[i] = Integer.parseInt(ss[i]); } for(int i = 0; i < num.length ; i++){ int index = i; for(int j = i + 1; j < num.length ; j++){ if(num[index] > num[j]){ index = j; } } if(index != i){ int temp = num[i]; num[i] = num[index]; num[index] = temp; } } for (int i = 0; i < m - 1; i++){ System.out.print(num[i] + ","); } System.out.print(num[m - 1]); } }3.
public class text2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); char[] chars = s.toCharArray(); int left , right ; for (left = 0; chars[left] == '_'; left++); for(right = chars.length - 1; chars[right] == '_'; right--); StringBuilder str = new StringBuilder(); for(int i = left; i <= right; i++){ if(chars[i] != '_'){ str.append(chars[i]); }else{ while(chars[i + 1] == '_'){ i++; } str.append('_'); } } System.out.println(str.toString()); } }4.
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); List<Integer> res = new ArrayList<>(); int b = num; for(int i = 2; i <= b; ){ if(num % i == 0){ res.add(i); num = num / i; }else{ i++; } } StringBuilder s = new StringBuilder(); for(int i = 0; i < res.size() - 1; i++){ s.append(res.get(i)).append("*"); } System.out.println(s.append(res.get(res.size() - 1))); } }