package LQseven; /*//第一题 171700 public class proGame { public static void main(String[] args){ int num = 1; int sum = 0; int n = 2; for(int i = 1;i <= 100;i++){ sum += num; num += n; n++; } System.out.println(sum); } }*/ /* //第二题 26 public class proGame { public static void main(String[] args){ int sum = 0; int nums = 236; for(int j = 1;j <= 100;j++){ sum = 0; for(int i = j;i <= 100;i++){ sum += i; if(sum == 236){ System.out.println(j); } if(sum > 236){ break; } } } } } */ //第三题 29 /* import java.util.LinkedList; import java.util.List; public class proGame { public static int count = 0; //public static LinkedList<Integer> path = new LinkedList<>(); public static boolean Compute(int[] nums){ int n = nums[0] * nums[2] * nums[4] + nums[1]*nums[4] + nums[3]*nums[2]; int m = nums[2] * nums[4]; return n == m * 10?true:false; } public static boolean IsNum(LinkedList<Integer> path){ int nums[] = new int[5]; int n = 1; for(int i = path.size()-1;i >= path.size()-3;i--){ nums[4] += path.get(i)*n; n *= 10; } n = 1; for(int i = path.size()-4;i >= path.size()-6;i--){ nums[3] += path.get(i)*n; n *= 10; } nums[2] = path.get(2); nums[1] = path.get(1); nums[0] = path.get(0); if(Compute(nums)){ return true; }else{ return false; } } public static void backtracking(LinkedList<Integer> path){ if(path.size() == 9){ if(IsNum(path)){ count++; } return; } for(int i = 1;i <= 9;i++){ if(path.contains(i)){ continue; } path.add(i); backtracking(path); path.removeLast(); } } public static void main(String[] args){ LinkedList<Integer> path = new LinkedList<>(); backtracking(path); System.out.println(count); } } */ /*import java.util.Arrays; public class proGame { public static int count = 0; public static boolean IsValid(int row,int col,int[][] map,int num){ for(int i = 0;i < map.length;i++){ for(int j = 0;j < map[0].length;j++){ if(map[i][j] == num){ return false; } } } return true; } public static void backtracking(int row,int col,int[][] map){ if(row == map.length){ count++; for(int i = 0;i < map.length;i++){ for(int j = 0;j < map[0].length;j++){ System.out.print(map[i][j]+" "); } System.out.println(); } return; } if(row == 0 && col == 0){ backtracking(row,col+1,map); } if(col == map[0].length){ backtracking(row+1,0,map); } if(row == map.length - 1 && col == map[0].length-1){ backtracking(row+1,0,map); } for(int i = 0;i <= 9;i++){ if(IsValid(row,col,map,i) && IsNum(row,col,i,map)){ map[row][col] = i; backtracking(row,col+1,map); map[row][col] = -2; } } return; } private static boolean IsNum(int row, int col, int i,int[][] map) { if(row+1 < map.length && (map[row+1][col] == i-1 || map[row+1][col] == i+1)){ return false; } if(col+1 < map[0].length && (map[row][col+1] == i-1 || map[row][col+1] == i+1)){ return false; } if(row >= 1 && (map[row-1][col] == i-1 || map[row-1][col] == i+1)){ return false; } if(col >= 1 && (map[row][col-1] == i-1 || map[row][col-1] == i+1)){ return false; } return true; } public static void main(String[] args){ int[][] map = new int[3][4]; for(int[] arr:map){ Arrays.fill(arr,-2); } backtracking(0,0,map); System.out.println(count); } }*/ //第六题 1580 /*import java.util.Arrays; public class proGame { public static int count = 0; public static boolean IsNum(int row, int col, int i,int[][] map) { if(row+1 < map.length && (map[row+1][col] == i-1 || map[row+1][col] == i+1)){ return false; } if(col+1 < map[0].length && (map[row][col+1] == i-1 || map[row][col+1] == i+1)){ return false; } if(row >= 1 && (map[row-1][col] == i-1 || map[row-1][col] == i+1)){ return false; } if(col >= 1 && (map[row][col-1] == i-1 || map[row][col-1] == i+1)){ return false; } if((col >= 1 && row >= 1) && (map[row-1][col-1] == i-1 || map[row-1][col-1] == i+1)){ return false; } if((col+1 < map[0].length && row >= 1) && (map[row-1][col+1] == i-1 || map[row-1][col+1] == i+1)){ return false; } if((col >= 1 && row + 1< map.length) && (map[row+1][col-1] == i-1 || map[row+1][col-1] == i+1)){ return false; } if((col+1 < map[0].length && row +1 < map.length) && (map[row+1][col+1] == i-1 || map[row+1][col+1] == i+1)){ return false; } return true; } public static boolean IsValid(int[][] map,int num){ for(int i = 0;i < map.length;i++){ for(int j = 0;j < map[0].length;j++){ if(map[i][j] == num){ return false; } } } // 9 7 5 // 4 0 3 1 // 2 8 6 return true; } public static void dfs(int row,int col,int[][] map,boolean[][] used,int n){ if(n == 10){ count++; return; } for(int i = 0;i <= 9;i++){ if(IsValid(map,i) && IsNum(row,col,i,map)){ map[row][col] = i; used[row][col] = true; n++; if((row+1 < map.length && used[row+1][col] == false) || (row+1 == 2 && col == 3 && n == 10)){ dfs(row+1,col,map,used,n); } if((col+1 < map[0].length && used[row][col+1] == false)){ dfs(row,col+1,map,used,n); } if((row >= 1 && used[row-1][col] == false) ){ dfs(row-1,col,map,used,n); } if((col >= 1 && used[row][col-1] == false) ){ dfs(row,col-1,map,used,n); } map[row][col] = -2; used[row][col] = false; n--; } } return; } public static void main(String[] args){ int[][] map = new int[3][4]; boolean[][] used = new boolean[3][4]; for(int[] arr:map){ Arrays.fill(arr,-2); } used[0][0] = true; used[2][3] = true; dfs(0,1,map,used,0); System.out.println(count); } }*/ /*import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class proGame { public static int count = 0; public static LinkedList<Integer> path = new LinkedList<>(); public static List<List<Integer>> result = new ArrayList<>(); public static void dfs(int row,int[][] nums,int index,boolean[][] used){ if(path.size() == 5){ result.add(new ArrayList<>(path)); return; } if(index == nums[0].length){ row++; } if(row == nums.length){ return; } for(int i = index;i < nums[0].length;i++){ if(used[row][i] == false){ used[row][i] = true; path.add(nums[row][i]); if(i +1 < nums[0].length && used[row][i+1] == false){ dfs(row,nums,i+1,used); } if(row+1 < nums.length && used[row+1][i] == false){ dfs(row+1,nums,i,used); } path.removeLast(); used[row][i] = false; } } return; } *//* public static void backtracking(){ if(path.size() == 5){ if(!result.contains(path)){ result.add(new ArrayList<>(path)); } return; } for(int i = index;) }*//* public static void main(String[] args){ int[][] nums = new int[3][4]; int num = 1; for(int i = 0;i < nums.length;i++){ for(int j = 0;j < nums[0].length;j++){ nums[i][j] = num++; } } boolean[][] used = new boolean[3][4]; dfs(0,nums,0,used); System.out.println(result.size()); } }*/ /*import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class proGame { public static LinkedList<Integer> path = new LinkedList<>(); public static List<List<Integer>> result = new ArrayList<>(); public static void backtracking(int[][] nums,int startIndex,){ if(path.size() == 5){ if(!result.contains(path)){ result.add(new ArrayList<>(path)); return; } } } public static void main(String[] args){ int[][] nums = new int[3][4]; int num = 1; for(int i = 0;i < nums.length;i++){ for(int j = 0;j < nums[0].length;j++){ nums[i][j] = num++; } } } }*/ //第五题不会做 /*public class proGame { public static void main(String[] args){ int[][] nums = new int[3][4]; int num = 1; for(int i = 0;i < nums.length;i++){ for(int j = 0;j < nums[0].length;j++){ nums[i][j] = num++; } } } }*/ /* import java.util.ArrayList; *//* import java.util.LinkedList; import java.util.List; public class proGame { public static LinkedList<Integer> path = new LinkedList<>(); public static List<List<Integer>> result = new ArrayList<>(); public static void backtracking(int sum,int target,int index){ if(sum == target && path.size() == 4){ result.add(new ArrayList<>(path)); return; } if(sum > target || path.size() > 4){ return; } for(int i = index;i <= Math.sqrt(target);i++){ if(index==0 && path.size() > 1){ i++; } sum += i*i; path.add(i); backtracking(sum,target,i); sum -= i*i; path.removeLast(); } return; } public static void main(String[] args){ backtracking(0,35,0); System.out.println(result); } }*/ //第九题 /* public class proGame { public static void backtracking(int[] nums,int sum){ } public static void main(String[] args){ int[] nums = new int[]{1,2,3}; int[] scores = new int[]{1,2,3,4,5}; } } */ //第十题 /* import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class proGame { public static int IsNum(int[] nums,int begin,int end){ int count = 0; for(int i = begin;i < end;i++){ if(nums[i] == nums[end]){ count++; } } return count; } public static int IsIndex(int begin,int end,int[] nums){ for(int i = end-1;i >= begin;i--){ if(nums[i] == nums[end]){ return i; } } return 0; } public static int SetNum(int begin,int end,int[] nums){ Set<Integer> set = new HashSet<>(); for(int i = begin+1;i < end;i++){ set.add(nums[i]); } return set.size(); } public static void main(String[] args){ int[] nums = new int[]{1,1,2,3,2,3,1,2,2,2,3,1}; int[] newNum = new int[nums.length]; for(int i = 0;i < nums.length;i++){ if(IsNum(nums,0,i) == 0){ newNum[i] = -nums[i]; }else{ int index = IsIndex(0,i,nums); int count = SetNum(index,i,nums); newNum[i] = count; } } System.out.println(Arrays.toString(newNum)); } } */
第七届蓝桥杯省赛B组部分题目代码
最新推荐文章于 2023-02-27 09:05:16 发布