以下只有部分题,太简单的就没写,太难的不会╮(╯▽╰)╭
特别数的和
题目:
输入一个数N,判断N之前的数中是否有“2”“0”“1”“9”,求出所有符合标准的数的和
思路:
用遍历N之前的每一个数,然后放到check方法中去判断
代码:
import java.util.Scanner;
import java.util.zip.CheckedInputStream;
public class 特别数的和 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int ans = 0;
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
if (chech(i))
ans += i;
}
System.out.println(ans);
}
private static boolean chech(int k) {
while (k > 0) {
if (k % 10 == 1 || k % 10 == 2 || k % 10 == 0 || k % 10 == 9) {
return true;
}//只判断个位数是否符合条件
k /= 10;//不符合去掉个位再次判断
}
return false;
}
}
数列求值
题目:
1,1,1,3,5,9…这样一串数列,求第20190324项的后四位
思路:
从第四个数开始,为前三项的和,其实就是一个变形的斐波那契数列
只不过要求的数太大了,我一开始采用的大数类型,但后来想起来每次返回后四位根本不会影响结果啊
所以每次返回时返回num%10000即可
代码:
public class 数列求值 {
public static void main(String[] args) {
int[] arr=new int[20190325];
arr[1]=1;arr[2]=1;arr[3]=1;
for (int i = 4; i < arr.length; i++) {
arr[i]+=(arr[i-1]+arr[i-2]+arr[i-3]);
arr[i]%=10000;
}
System.out.println(arr[20190324]);
}
}
数的分解
题目:
把2019分解成三个各不相同的整数之和,每个整数都不包含2或4
思路:
假设三个整数为 i,j,k 。
令 i < j < k 。
再枚举出 i & j ,
通过 k = 2019 - i - j 得出 k。
最终我们check 一下三个数字中是否含有 2 or 4.
如果符合条件即进行计数
代码:
public class 数的分解 {
static boolean check(int k){
while (k>0) {
if (k%10==2||k%10==4)
return false;
else
k/=10;
}
return true;
}
public static void main(String[] args) {
int sum=2019,ans=0;
for (int i = 1; i < 1000; i++) {
sum-=i;
for (int j = i+1; j < 1500; j++) {
sum-=j;
if (sum>j&&check(i)&&check(j)&&check(sum))
ans++;
sum+=j;
}
sum+=i;
}
System.out.println(ans);
}
}
不同字串
题目:
一个字符串的非空子串是指字符串中长度至少为1 的连续的一段字符组成的串。
例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共7 个。
注意在计算时,只算本质不同的串的个数。
请问,字符串0100110001010001 有多少个不同的非空子串?
思路:
将substring(i,j)放入循环然后枚举出所有的字符串,放到Hash Set中,因为HashSet不允许重复,相同的字符会覆盖,最后输出hashset的长度
代码:
public class 不同字串 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
HashSet<String> hashSet=new HashSet<String>();//不存在重复元素
for (int i = 0; i < s.length(); i++) {
for (int j = i+1; j <=s.length() ; j++) {
//SubString 包括begin,不包括end,从i的后1位开始枚举
hashSet.add(s.substring(i,j));
}
}
System.out.println(hashSet.size());
}
}
迷宫
思路:这个我第一反应是固定的地图用excel肯定最快,后来看了一下dfs和bfs的代码,发现真的不如excel(好,闹着玩的,但蓝桥杯的填空题只要答案,又是多换几种方法也未尝不可)
DFS:
public class 迷宫 {
public static int endx=30,endy=50;
public static String ans="";
public static int [][] arr = new int[endx][endy];
public static char [][] map = new char[endx][endy];
public static boolean [][] mark = new boolean[endx][endy];
public static String min(String s1,String s2){
if(s1.length()>s2.length()){
return s2;
}else if(s1.length()<s2.length()){
return s1;
}else{
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
for(int i=0;i<c1.length;i++){
if(c1[i]>c2[i])
return s2;
else if(c1[i]<c2[i])
return s1;
}
}
return s1;
}
public static void dfs(int x,int y,String cur,int step){
arr[x][y]=step;
if(endx==x+1&&endy==y+1){
ans=ans.equals("")?cur:min(ans,cur);
return;
}
if(x+1>=0&&x+1<endx&&arr[x+1][y]<=step&& mark[x+1][y]==false){
mark[x+1][y]=true;
dfs(x+1,y,cur+"D",step+1);
mark[x+1][y]=false;
}
if(x-1>=0&&x-1<endx&&arr[x-1][y]<=step&& mark[x-1][y]==false){
mark[x-1][y]=true;
dfs(x-1,y,cur+"U",step+1);
mark[x-1][y]=false;
}
if(y+1>=0&&y+1<endy&&arr[x][y+1]<=step&& mark[x][y+1]==false){
mark[x][y+1]=true;
dfs(x,y+1,cur+"R",step+1);
mark[x][y+1]=false;
}
if(y-1>=0&&y-1<endy&&arr[x][y-1]<=step&& mark[x][y-1]==false){
mark[x][y-1]=true;
dfs(x,y-1,cur+"L",step+1);
mark[x][y-1]=false;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int i=0;i<endx;i++){
map[i]=sc.next().toCharArray();//按行输入字符,方便复制
for(int j=0;j<endy;j++){
if(map[i][j]=='1'){//如果是障碍,给他一个较大值
arr[i][j]=Integer.MAX_VALUE;
}
}
}
mark[0][0]=true;
dfs(0,0,"",0);
System.out.println(ans);
}
}
BFS:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class 迷宫_bfs {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
String s = "01010101001011001001010110010110100100001000101010"
+ "00001000100000101010010000100000001001100110100101"
+ "01111011010010001000001101001011100011000000010000"
+ "01000000001010100011010000101000001010101011001011"
+ "00011111000000101000010010100010100000101100000000"
+ "11001000110101000010101100011010011010101011110111"
+ "00011011010101001001001010000001000101001110000000"
+ "10100000101000100110101010111110011000010000111010"
+ "00111000001010100001100010000001000101001100001001"
+ "11000110100001110010001001010101010101010001101000"
+ "00010000100100000101001010101110100010101010000101"
+ "11100100101001001000010000010101010100100100010100"
+ "00000010000000101011001111010001100000101010100011"
+ "10101010011100001000011000010110011110110100001000"
+ "10101010100001101010100101000010100000111011101001"
+ "10000000101100010000101100101101001011100000000100"
+ "10101001000000010100100001000100000100011110101001"
+ "00101001010101101001010100011010101101110000110101"
+ "11001010000100001100000010100101000001000111000010"
+ "00001000110000110101101000000100101001001000011101"
+ "10100101000101000000001110110010110101101010100001"
+ "00101000010000110101010000100010001001000100010101"
+ "10100001000110010001000010101001010101011111010010"
+ "00000100101000000110010100101001000001000000000010"
+ "11010000001001110111001001000011101001011011101000"
+ "00000110100010001000100000001000011101000000110011"
+ "10101000101000100010001111100010101001010000001000"
+ "10000010100101001010110000000100101010001011101000"
+ "00111100001000010000000110111000000001000000001011"
+ "10000001100111010111010001000110111010101101111000";
int[][] labyrinth = new int[30][50];
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 50; j++) {
labyrinth[i][j] = s.charAt(50 * i + j) - '0';
}
}
System.out.println(BFS(labyrinth, 30, 50));
} catch (Exception e) {
input.close();
}
}
public static String BFS(int[][] labyrinth, int row, int column) {
int[][] stepArr = { { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 0 } };
String[] direction = { "U", "R", "L","D"};
int[][] visit = new int[row][column];// 标记是否已经访问过
StringBuilder sb = new StringBuilder();
Node node = new Node(0, 0, -1, -1, 0, null);
Queue<Node> queue = new LinkedList<Node>();
Stack<Node> stack = new Stack<Node>();
queue.offer(node);
while (!queue.isEmpty()) {
Node head = queue.poll();
stack.push(head); // 用于回溯路径
visit[head.x][head.y] = 1;
for (int i = 0; i < 4; i++) {
int x = head.x + stepArr[i][0];
int y = head.y + stepArr[i][1];
String d = direction[i];
// exit
if (x == row - 1 && y == column - 1 && labyrinth[x][y] == 0 && visit[x][y] == 0) {
// 打印路径
Node top = stack.pop();
sb.append(d);
sb.append(top.direction);
int preX = top.preX;
int preY = top.preY;
while (!stack.isEmpty()) {
top = stack.pop();
if (preX == top.x && preY == top.y) {
if (top.direction != null)
sb.append(top.direction);
preX = top.preX;
preY = top.preY;
}
}
return sb.reverse().toString();
}
// bfs
if (x >= 0 && x < row && y >= 0 && y < column && labyrinth[x][y] == 0 && visit[x][y] == 0) {
Node newNode = new Node(x, y, head.x, head.y, head.step + 1, d);
queue.offer(newNode);
}
}
}
return null;
}
}
class Node {
int x, y;
int step;
int preX, preY;
String direction;
Node(int x, int y, int preX, int preY, int step, String direction) {
this.x = x;
this.y = y;
this.preX = preX;
this.preY = preY;
this.step = step;
this.direction = direction;
}
}