目录
1.互质
public class Main {
public static void main(String[] args) {
int count = 0;
for(int i = 1;i <= 2020;i++) {
if(gcd(i,2020) == 1) {
count++;
}
}
System.out.println(count);
}
private static int gcd(int a, int b) {
if(b == 0)
return a;
return gcd(b,a%b);
}
}
2.ASCII码
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q |
65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |
3.Pierce序列
解法一
public class Main {
public static void main(String[] args) {
int v = 1160;
int p = 2021;
int ans = 1;
while(p % v != 0) {
v = p % v;
++ans;
}
System.out.println(ans);
}
}
解法二
public class Main {
private static int count = 0;
public static void main(String[] args) {
fun(1160,2021);
System.out.println(count);
}
private static void fun(int v, int p) {
int[] a = new int [100];
a[1] = v;
for(int i = 2;i <= 50;i++) {
if(a[i-1] != 0) {
a[i] = p % a[i-1];
}
}
for(int i = 1;i <= 50;i++) {
if(a[i-1] != 0) {
count ++;
//System.out.println(a[i]);
}
}
}
}
4.二叉树的叶结点
5.Excel的列名
解法一
public class Main {
public static void main(String[] args) {
int n = 2021;
StringBuilder s = new StringBuilder("");
while(n != 0) {
s.append((char)(((n - 1) % 26) + 'A'));
n = (n - 1) / 26;
}
System.out.println(s.reverse().toString());
}
}
解法二
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[] ans = new char[1000000];
int count = 0;
while(n > 0) {
int temp = n % 26;
if(temp == 0) {
ans[count++] = 'Z';
n = n / 26 - 1;
}else {
ans[count++] = (char) ('A' + temp -1);
n = n / 26;
}
}
for(int i = count -1;i >= 0;i--) {
System.out.println(ans[i]);
}
}
}
6.增加分割符
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long v = sc.nextLong();
char[] ch = new String(v + "").toCharArray();
if(ch.length <= 3) {
System.out.println(v);
}else if(ch.length % 3 == 0){
System.out.print("" + ch[0] + ch[1] + ch[2]);
for(int i = 3;i < ch.length;i += 3) {
System.out.print("," + ch[i] + ch[i+1] + ch[i+2]);
}
}else {
for(int i = 0;i < ch.length % 3;++i) {
System.out.print(ch[i]);
}
for(int i = ch.length % 3;i < ch.length;i += 3) {
System.out.print("," + ch[i] + ch[i+1] + ch[i+2]);
}
}
}
}
7.斐波那契数列
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
BigInteger two = new BigInteger("2");
if((F(N).mod(two)).compareTo(BigInteger.ZERO) == 0) {
System.out.println(0);
}else {
System.out.println(1);
}
}
private static BigInteger F(int N) {
BigInteger F1 = BigInteger.ONE;
BigInteger F2 = BigInteger.ONE;
BigInteger F3 = BigInteger.ZERO;
for(int i = 3;i <= N;i++) {
F3 = F1.add(F2);
F1 = F2;
F2 = F3;
}
return F3;
}
}
8.矩阵
import java.util.Scanner;
public class Main {
static int[][] v;
static int total = 0,n,m;
static int r = 0,c = 0,d = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
v = new int [n][m];
for(int i = 0;i < n;++i) {
for(int j = 0;j < m;++j) {
v[i][j] = sc.nextInt();
total += v[i][j];
}
}
int[][] dp = new int[n + 1][m + 1];
for(int i = 1;i <= n;++i) {
for(int j = 1;j <= m;++j) {
dp[i][j] = dp[i-1][j] + dp[i][j-1] -dp[i-1][j-1] + v[i-1][j-1];
check(dp[i][j],i,j);
}
}
System.out.println(r + " "+ c);
}
private static void check(int x, int i, int j) {
if(Math.abs(2 * x -total) < d || Math.abs(2 * x - total) == d && i * j < r * c) {
r = i;
c = j;
d = Math.abs(2 * x - total);
}
}
}
9.像素
10.购买物品
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
// 模拟二进制,三种属性C、B、A,让它们分别代表4、2、1,
//即1<<2、1<<1、1<<0(注1<<n是位运算),
//当i==5时,dp[i]表示拥有CA属性时代价最小
int[] dp = new int[7+1];
//7(十进制)==111(二进制)==CBA,最终dp[7]就表示同时拥有ABC属性时代价最小
Arrays.fill(dp, Integer.MAX_VALUE);
for(int i = 0;i < N;++i) {
int c = sc.nextInt(),v = 0;//v用来记录当前物品有哪几种属性
char[] ch = sc.next().toCharArray();
for(int j = 0;j < ch.length;++j) {
v |= 1 << (ch[j] - 'A');
}
for(int j = 1;j < dp.length;++j) {//j从1到7,分别表示A、B、C、BA、CA、CB、CBA
if((j & v) != v && dp[j] != Integer.MAX_VALUE && c + dp[j] < dp[v | j])
//j==5,v==4,则v中拥有的属性,j也有,j没必要与v进行组合
dp[v | j] = c + dp[j];//当dp[j] == Integer.MAX_VALUE时,代表还没有j这种组合
}
dp[v] = Math.min(dp[v],c);
}
System.out.println(dp[7] == Integer.MAX_VALUE ? -1 : dp[7]);
}
}
11.上升子序列
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int MOD = 1000007;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;i++) {
a[i] = sc.nextInt();
}
int[][] dp = new int[n][k+1];
for(int i = 0;i < n;i++) {
dp[i][1] = 1;
}
for(int i = 1;i < n;i++) {
for(int j = 0;j < i;j++) {
if(a[i] > a[j]) {
for(int t = 1;t <= k;t++) {
if(dp[j][t-1] != 0) {
dp[i][t] = (dp[i][t] + dp[j][t-1]) % MOD;
}
}
}
}
}
int ans = 0;
for(int i = 0;i < n;i++) {
ans = (ans + dp[i][k] % MOD);
}
System.out.println(ans);
}
}