1.这个要注意long,题目真是埋坑啊...
/*把数组的每个数拆成素数,拆后最多有多少个数*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MaxPrimeNum { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long len = Long.parseLong((br.readLine().trim().split(" "))[0]); String[] strs = br.readLine().trim().split(" "); long count = 0; for (int i = 0; i < len; i++) { long tmp = Long.parseLong(strs[i]); count += tmp /2; } System.out.println(count); } }2.借鉴了一个牛友的思想,merge
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; /* * 给一个长度为m的序列T,求一个长度为n的序列S,要求T是S的子序列(T是由S删除数字得到的)求S按字典序排序的最小序列。 * */ public class MinDictionarySequence { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] strs = br.readLine().trim().split(" "); int n = Integer.parseInt(strs[0]); int m = Integer.parseInt(strs[1]); int[] nums = new int[m]; //[2,1,5] int[] res = new int[n]; HashSet<Integer> set = new HashSet<>(); strs = br.readLine().trim().split(" "); for (int i = 0; i < nums.length; i++) { nums[i] = Integer.parseInt(strs[i]); set.add(nums[i]); } int max = Math.max(n, maxOfNums(nums)); int[] temp = new int[max-m]; int a = 0; for(int i = 1; i <= max; i++) { if(set.contains(i)) continue; else temp[a++] = i; }//temp = [3,4] Arrays.sort(temp); //Merge int nums_i = 0, temp_i = 0, res_i = 0; while(nums_i < m && temp_i < temp.length ) { if(nums[nums_i] < temp[temp_i]) res[res_i++] = nums[nums_i++]; else res[res_i++] = temp[temp_i++]; } while(nums_i < m) res[res_i++] = nums[nums_i++]; while(temp_i < temp.length ) res[res_i++] = nums[temp_i++]; for (int i = 0; i < res.length; i++) { if(i != res.length - 1) System.out.print(res[i] + " "); else System.out.print(res[i]); } } public static int maxOfNums(int[] nums) { int max = Integer.MIN_VALUE; for (int i = 0; i < nums.length; i++) { max = Math.max(nums[i], max); } return max; } }4.有没有大哥补一个答案,不会写
全部评论
(0) 回帖