P1008 [NOIP1998 普及组] 三连击
import java.util.HashMap;
public class P1008_NOIP1998普及组_三连击 {
public static void main(String[] args) {
HashMap<Integer,Integer> hashMap = new HashMap<>();
for (int i = 100; i <=333; i++) {
hashMap.put(1,i%10);
if (hashMap.containsValue(i/10%10)){
hashMap.clear();
continue;
}else {
hashMap.clear();
hashMap.put(2,i/10%10);
}
if (hashMap.containsValue(i/100%10)){
hashMap.clear();
continue;
}else{
hashMap.put(3,i/100%10);
}
int i2 = i*2;
if (hashMap.containsValue(i2%10)){
hashMap.clear();
continue;
}else{
hashMap.put(4,i2%10);
}
if (hashMap.containsValue(i2/10%10)){
hashMap.clear();
continue;
}else {
hashMap.put(5,i2/10%10);
}
if (hashMap.containsValue(i2/100%10)){
hashMap.clear();
continue;
}else{
hashMap.put(6,i2/100%10);
}
int i3 = i*3;
if (hashMap.containsValue(i3%10)){
hashMap.clear();
continue;
}else{
hashMap.put(7,i3%10);
}
if (hashMap.containsValue(i3/10%10)){
hashMap.clear();
continue;
}else {
hashMap.put(8,i3/10%10);
}
if (hashMap.containsValue(i3/100%10)){
hashMap.clear();
continue;
}else{
hashMap.put(9,i3/100%10);
}
if (hashMap.containsValue(0)){
hashMap.clear();
continue;
}
System.out.println(i+" "+i*2+" "+i*3);
hashMap.clear();
}
}
}
用hashmap硬一个一个匹配,一层for循环解决
P1010 [NOIP1998 普及组] 幂次方
import java.util.Scanner;
public class P1010_NOIP1998普及组_幂次方 {
static int[] mark = new int[1000];
public static void func(int n) {
int flag = 0;
while (n >= mark[flag]) {
flag++;
}
flag--;
if (flag == 1) {
System.out.print("2");
} else if (flag == 0) {
System.out.print("2(0)");
} else {
System.out.print("2(");
func(flag);
System.out.print(")");
}
n -= mark[flag];
if (n != 0) {
System.out.print("+");
func(n);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
mark[0] = 1;
for (int i = 1; i < 17; i++) {
mark[i] = mark[i - 1] * 2;
}
func(n);
}
}
递归
P1014 [NOIP1999 普及组] Cantor 表
import java.util.Scanner;
public class P1014_NOIP1999普及组_Cantor表 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 1) {
System.out.println("1/1");
return;
}
if (n == 2) {
System.out.println("1/2");
return;
}
// Z字遍历, 从第三项开始
// x
// ———
// y
int x = 1;
int y = 2;
int count = 2;
int judge = 2; // 遇见边界次数,两次就转折,初始就是转折
int flag = 1; // 0 表示左下走,1表示右上走
while (count != n) {
// x等于1,第一次进边界,不转折
if (x == 1 && judge != 2) {
y++;
judge++;
count++;
continue;
}
// x等于1,第二次进边界,转折
if (x == 1 && judge == 2) {
x++;
y--;
judge = 0;
if (y==1){
judge++;
}
flag=0;
count++;
continue;
}
if (y == 1 && judge != 2) {
x++;
judge++;
count++;
continue;
}
//进入y边界2次
if (y == 1 && judge == 2) {
x--;
y++;
judge = 0;
flag=1;
count++;
continue;
}
if (flag==1){
x--;
y++;
if (x==1){
judge++;
}
count++;
continue;
}
if (flag==0){
x++;
y--;
if (y==1){
judge++;
}
count++;
continue;
}
}
System.out.println(x + "/" + y);
}
}
暴力,经过一次次改正,终于成功,需要不断debug
P1015 [NOIP1999 普及组] 回文数
import java.util.*;
public class P1015_NOIP1999普及组_回文数 {
static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
step(m);
if (count != -1) {
System.out.println("STEP=" + count);
} else {
System.out.println("Impossible!");
}
}
public static int step(int m) {
if (count > 30) {
return -1;
}
StringBuffer rev = new StringBuffer();
rev = new StringBuffer(Integer.toString(m));
rev = rev.reverse();
int revM = Integer.parseInt(rev.toString());
int tenM = Integer.parseInt(Integer.toString(m), 10);
// System.out.println("tenM = "+tenM);
int tenRevM = Integer.parseInt(Integer.toString(revM), 10);
// System.out.println("tenRevM = "+ tenRevM);
if (isHuiWen(tenM + tenRevM)) {
count++;
return count;
} else {
step(tenM + tenRevM);
count++;
}
return -1;
}
public static boolean isHuiWen(int n) {
Stack<Integer> stack = new Stack<>();
int mid = Integer.toString(n).length() / 2;
int i = 0;
if (Integer.toString(n).length() % 2 == 0) {
while (i != mid) {
stack.push(n % 10);
n /= 10;
i++;
}
while (!stack.isEmpty()) {
if (stack.pop() != n % 10) {
return false;
}
n = n / 10;
}
} else { // 34543
while (i != mid) {
stack.push(n % 10);
n /= 10;
i++;
}
n /= 10;
while (!stack.isEmpty()) {
if (stack.pop() != n % 10) {
return false;
}
n /= 10;
}
}
return true;
}
}
没有通过,但是写了一个判断回文的方法
P1022 [NOIP2000 普及组] 计算器的改良
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int n=1000;
StringBuilder[] splitArray = new StringBuilder[n];
// StringBuilder使用前要初始化,很重要!!
for (int i = 0; i < n; i++) {
splitArray[i] = new StringBuilder();
}
char symbol = 0;
for (int i = 0; i < s.length(); i++) {
symbol = s.charAt(i);
if (symbol>='a'&& symbol<='z'){
break;
}
}
int j = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '-') {
j++;
splitArray[j].append(c);
} else if (c == '+') {
j++;
} else if (c == '=') {
j++;
splitArray[j].append(c);
j++;
} else {
splitArray[j].append(c);
}
}
boolean isLeft = true;
float left = 0; // a的系数
float right = 0; // 常数项
for (StringBuilder s1 :
splitArray) {
if (s1.compareTo(new StringBuilder("=")) == 0) {
isLeft = false;
continue;
}
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if (s1.charAt(s1.length() - 1) == symbol) {
s1.deleteCharAt(s1.length()-1);
if (isLeft){
if (s1.toString().compareTo("-")==0){
left -=1;
break;
}
left += Integer.parseInt(s1.toString());
break;
}else{
if (s1.toString().compareTo("-")==0){
left +=1;
break;
}
left -= Integer.parseInt(s1.toString());
break;
}
}else{
if (isLeft){
right -= Integer.parseInt(s1.toString());
break;
}else{
right += Integer.parseInt(s1.toString());
break;
}
}
}
}
float a = right/left;
System.out.println(symbol+"="+String.format("%.3f",a));
}
}
通过4/6部分用例代码
P1028 [NOIP2001 普及组] 数的计算
import java.util.Scanner;
public class P1028_NOIP2001普及组_数的计算 {
public static int Recursion(int n) {
/*
n
1 1 1种
2 2
2 1 2种
3 3
3 1 2种
4 4
4 1
4 2
4 2 1 4种
5 5
5 1
5 2
5 2 1 4种
7 7
7 1
7 2
7 3
7 2 1
7 3 1 6种
8 8
8 1
8 2
8 3
8 4
8 2 1
8 3 1
8 4 1
8 4 2
8 4 2 1 10种
f(1) = 1
f(2) = 2 = f(1) + 1
f(3) = 2 = f(1) + 1
f(4) = 4 = f(1) + f(2) + 1
f(5) = 4 = f(1) + f(2) + 1
f(6) = 6 = f(1) + f(2) + f(3) + 1
f(7) = 6 = f(1) + f(2) + f(3) + 1
f(8) = 10 = f(1) + f(2) + f(3) + f(4) + 1
*/
if (n == 1) {
return 1;
}
if (n == 2 || n == 3) {
return 2;
}
if (n % 2 != 0) {
n -= 1;
}
int mid = n / 2;
int sum = 0;
for (int i = 1; i <= mid; i++) {
sum += Recursion(i);
}
return sum + 1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(Recursion(n));
}
}
超时,没全AC,但是算法没问题
P1112 波浪数
import java.util.Scanner;
public class P1112波浪数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int r = sc.nextInt();
int L = sc.nextInt();
int R = sc.nextInt();
int k = sc.nextInt();
System.out.println();
// for (int i = L; i <=R ; i++) {
// if (ChongShu(l,r,i)==k){
// System.out.println(i);
// }
// }
}
public static int ChongShu(int l,int r,int n){
int cnt =0;
for (int i = l; i <=r ; i++) {
String s = Integer.toString(n,i);
if (s.length()<=2){
cnt ++;
continue;
}
int c1 = s.charAt(0);
int c2 = s.charAt(1);
boolean judge = true;
for (int j = 2; j < s.length(); j++) {
if (j%2==0){ // 奇数位
if (s.charAt(j)!=c1){
judge = false;
break;
}
}else { // 偶数位
if (s.charAt(j)!=c2){
judge = false;
break;
}
}
}
if (judge){
cnt++;
}
}
return cnt;
}
}
有问题
只AC三个
P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题
import java.util.Scanner;
public class P1029_NOIP2001普及组_最大公约数和最小公倍数问题 {
/*
注意!!!!
两个数的乘积等于 GCD 和 LCM 的乘积----->剪枝
*/
// public static int GCD(int a, int b) { // 最大公约数 Greatest Common Divisor
// for (int i = Math.min(a, b); i >= 2; i--) {
// if (a % i == 0 && b % i == 0) {
// return i;
// }
// }
// return -1;
// }
public static int GCD(int n, int m) {
if (n % m == 0) return m;
else return GCD(m, n % m);
}
/* 辗转相除法(c++)
int gcd(int n,int m)
{
if(n%m==0) return m;
else return gcd(m,n%m);
}
*/
public static int LCM(int a, int b) { // 最小公倍数 Least Common Multiple
for (int i = Math.max(a, b); i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x0 = sc.nextInt();
int y0 = sc.nextInt();
int max = Math.max(x0, y0);
int min = Math.min(x0, y0);
int count = 0;
int m = x0 * y0;
int memI = 0, memJ = 0; // 记录解,剪一半枝
// for (int i = min; i <= max; i++) {
// for (int j = min; j <= max; j++) {
// if (i * j != m) // 通过定理 两个数的乘积等于 GCD 和 LCM 的乘积 剪枝
// continue;
// if (GCD(i, j) == x0 && LCM(i, j) == y0) {
// if (memI == j && memJ == i) {
// System.out.println(count * 2);
// return;
// }
// memI = i;
// memJ = j;
// count++;
// }
// }
// }
for (int i = min; i <= max; i++) {
int j = m / i; // 通过定理 两个数的乘积等于 GCD 和 LCM 的乘积 而不需要两重 for 循环
if (GCD(i, j) == x0 && LCM(i, j) == y0) {
if (memI == j && memJ == i) {
System.out.println(count * 2);
return;
}
memI = i;
memJ = j;
count++;
}
}
System.out.println(count);
}
}
全AC,注意辗转相除法的GCD,效率更高
还有定理剪枝
P1035 [NOIP2002 普及组] 级数求和
import java.util.Scanner;
public class P1035_NOIP2002普及组_级数求和 {
public static double Sn(int n) {
if (n == 1) {
return 1;
}
return Sn(n - 1) + 1.0 / n;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
// 不要用递归!!!!!递归需要计算更多的步骤
// int i=1;
// while (Sn(i)<=k){
// i++;
// }
int i = 1;
double sum = 0;
for (; i < 10000000; i++) {
sum += 1.0 / i;
if (sum > k) {
break;
}
}
System.out.println(i);
}
}
AC
P1135 奇怪的电梯
DFS:
带剪枝的DFS
import java.util.Scanner;
public class P1135奇怪的电梯 {
static int N;
static int A;
static int B;
static int[] up = new int[1000];
static boolean[] vis = new boolean[1000];
static int res = Integer.MAX_VALUE;
public static void dfs(int floor, int cnt) {
if (floor == B || cnt > res) {
res = Math.min(res, cnt);
return;
}
if (!vis[floor]){
vis[floor] = true;
if (floor + up[floor]<=B)
dfs(floor + up[floor], cnt + 1);
if (floor - up[floor]>=A)
dfs(floor - up[floor], cnt + 1);
vis[floor] = false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
A = sc.nextInt();
B = sc.nextInt();
for (int i = 1; i <= N; i++) {
up[i] = sc.nextInt();
}
dfs(A, 0);
if (res!=Integer.MAX_VALUE){
System.out.println(res);
} else {
System.out.println(-1);
}
}
}
P1152 欢乐的跳
入门题,但是用了哈希表
import java.util.HashMap;
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[1005];
int[] b = new int[n];
HashMap<Integer,Integer> hashMap = new HashMap<>();
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
}
for (int i = 0; i < n-1; i++) {
int cha = Math.abs(b[i]-b[i+1]);
hashMap.put(i,cha);
}
for (int i = 1; i <= n-1; i++) {
if (!hashMap.containsValue(i)){
System.out.println("Not jolly");
return;
}
}
System.out.println("Jolly");
}
}