【天梯每日练习之小白养成记+附java题解】

天梯每日练习之小白养成记+附java题解

  • 2022-12-19
  • 2022-12-20
  • 2022-12-21
  • 2022-12-22

2022-12-19

202209-1 如此编码

import java.io.*;

public class Main {
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException{
		int n = nextInt();
		int m = nextInt();
		int[] a = new int[n+1];
		int[] c = new int[n+1];
		int[] b = new int[n+1];
		c[0] = 1;
		int temp = 0;
		for(int i=1; i<=n; i++) {
			a[i] = nextInt();
			c[i] = c[i-1]*a[i]; //计算前缀
			b[i] = (m % c[i] - temp) / c[i-1]; //计算b[i]
			temp += c[i-1] * b[i]; //存储b[i-1]
			out.print(b[i]);
			if(i != n) {
				out.print(" ");
			}
		}
		out.close();
	}
	
	public static int nextInt() throws IOException{
		in.nextToken();
		return (int)in.nval;
	}
}

2022-12-20

L1-085 试试手气

import java.io.*;

public class Main {
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException{
		int[] a = new int[6];
		for(int i=0; i<6; i++) {
			a[i] = nextInt();
		}
		int n = nextInt();
		int max = 7-n;
		for(int i=0; i<6; i++) {
			if(a[i]>=max) {
				a[i] = max-1; //遇到初始值输出为7-n-1
			}else {
				a[i] = max; //遇不到初始值输出为7-n
			}
			out.print(a[i]);
			if(i!=5) {
				out.print(" ");
			}
		}
		out.close();
	}
	
	public static int nextInt() throws IOException{
		in.nextToken();
		return (int)in.nval;
	}
}

2022-12-21

L1-076 降价提醒机器人

import java.io.*;

public class Main {
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException{
		int n = (int)nextInt();
		int m = (int)nextInt();
		double[] p = new double[n];
		for(int i=0; i<n; i++) {
			p[i] = nextInt();
			if(p[i]<m) {
				out.println("On Sale! "+Math.round(p[i]*10)/10.0);
			}
		}
		out.close();
	}
	
	public static double nextInt() throws IOException{
		in.nextToken();
		return in.nval;
	}
}

L1-086 斯德哥尔摩火车上的题

import java.io.*;
public class Main{
	
	public static void main(String[] args) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		char[] s1 = in.readLine().toCharArray(); //String转char[]
		char[] s2 = in.readLine().toCharArray(); //String转char[]
		
		String a = f(s1);
		String b = f(s2);
		if(a.equals(b)) {
			System.out.println(a);
		}else {
			System.out.println(a);
			System.out.println(b);
		}
	}
	
	public static String f(char[] s1) {
		StringBuilder s = new StringBuilder();
		for(int i=1; i<s1.length; i++) {
			int a = s1[i] - '0'; //char转int
			int b = s1[i-1] - '0'; //char转int
			if(a%2 == b%2) {
				s.append(Math.max(a, b));
			}
		}
		return s.toString(); //StringBuilder转String
	}
}

L1-087 机工士姆斯塔迪奥

import java.io.*;
public class Main{
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException{
		int n = nextInt();
		int m = nextInt();
		int q = nextInt();
		int[][] arr = new int[n+1][m+1];
		int a, b;
		for(int i=1; i<=q; i++) {
			a = nextInt();
			b = nextInt();
			if(a==0) {
				for(int j=1; j<=m; j++) {
					arr[b][j] = 1;
				}
			}else {
				for(int j=1; j<=n; j++) {
					arr[j][b] = 1;
				}
			}
		}
		int res = 0;
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++) {
				if(arr[i][j]==0) {
					res++;
				}
			}
		}
		out.print(res); //只能纯暴力,n*m-r*m-c*n+r*m会报错??
		out.close();
	}
	
	public static int nextInt() throws IOException {
		in.nextToken();
		return (int)in.nval;
	}
}

L1-088 静静的推荐

import java.io.*;
public class Main{
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException{
		int n = nextInt(), k = nextInt(), s = nextInt();
		int score = 0, pat = 0;
		int count = 0;
		int[] a = new int[291];
		for(int i=1; i<= n; i++) {
			score = nextInt();
			pat = nextInt();
			if(score >= 175) {
				if(pat >= s) { //pat分数足够直接内推
					count++;
				}else {
					if(a[score] < k) { //其他同学按流程录取前k名
						a[score]++;
						count++;
					}
				}
			}
		}
		out.print(count);
		out.close();
	}
	 
	public static int nextInt() throws IOException {
		in.nextToken();
		return (int)in.nval;
	}
}

2022-12-22

L1-067 洛希极限

在这里插入代码片

你可能感兴趣的:(算法练习,java,算法)