比赛链接:https://www.nowcoder.com/acm/contest/90#question
找规律、后面的台阶等于前面的台阶达到方式数+1、
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class A {
static long [] num = new long [31];
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
num[0] = 0;
// num[1] = 1;
// num[2] = 2;
// num[3] = 4;
long sum = 0;
for(int i = 0; i < 30; i++){
sum += num[i];
num[i + 1] = sum + 1;
}
in.nextToken(); int t = (int)in.nval;
while(t-- != 0){
in.nextToken(); int n = (int)in.nval;
out.println(num[n]);
out.flush();
}
}
}
最长公共子序列的题目
代码一:
import java.util.Scanner;
public class D_ {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String s = input.next();
int len = s.length();
s = s.toLowerCase();
int[][] dp = new int[len + 1][len + 1]; // 第i字符到第j字符, 这个子串的最长回文子序列长度
for(int i = 1; i <= len; i++){
for(int j = 1; j <= len; j++){
if(s.charAt(i - 1) == s.charAt(len - j)){
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
System.out.println(len - dp[len][len]);
}
}
}
代码二:
import java.util.Scanner;
public class D {
static int N = 1240;
static int dp[][] = new int[N][N];
static char c1[] = new char[N];
static char c2[] = new char[N];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String s = input.next();
s = s.toUpperCase();
c1 = s.toCharArray();
int n = s.length();
for (int i = 0; i < n; i++) {
c2[i] = c1[n - i - 1];
}
Init();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (c1[i] == c2[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
}
System.out.println(n - dp[n][n]);
}
}
public static void Init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i][j] = 0;
}
}
}
}
枚举所有星星两两之间的距离、然后找两者相距相等的、有很多都是一样的、就这些中取2个 c (2, n) * 2 // 前后和后前算两种、
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class E {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Node node[] = new Node[1005];
for(int i = 0; i < 1005; i++){
node[i] = new Node();
}
int d[] = new int[1005];
in.nextToken();
int T = (int) in.nval;
int xx, yy;
while (T-- != 0) {
int ans = 0;
in.nextToken();
int n = (int) in.nval;
for (int i = 1; i <= n; i++) {
in.nextToken();
node[i].x = (int) in.nval;
in.nextToken();
node[i].y = (int) in.nval;
}
for (int i = 1; i <= n; i++) {
int cnt = 0;
for (int j = 1; j <= n; j++) {
if (i == j) {
continue;
}
d[cnt++] = dis(node[i], node[j]);
}
Arrays.sort(d, 0, cnt);
int res = 1;
for (int k = 1; k < cnt; k++) {
if (d[k] == d[k - 1]) {
res++;
} else {
ans += res * (res - 1); // 从 res 个中选择2个、 (交换位置也算一个)、 res* (res- 1) / 2 * 2
res = 1;
}
}
ans += res * (res - 1);
}
if (ans == 0) {
out.println("WA");
} else {
out.println(ans);
}
out.flush();
}
}
public static int dis(Node a, Node b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
public static class MyComparator implements Comparator<Node> { // 自己定义的比较器、、、
@Override
public int compare(Node a, Node b) {
return (a.x == b.x) ? (a.y - b.y) : (a.x - b.x);
}
}
static class Node {
int x, y;
public Node() {
}
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class F {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int t = nextInt();
while (t-- != 0) {
int n = nextInt();
long ans = 1;
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if (n % i == 0) {
int tmp = 0;
while (n % i == 0) {
tmp++;
n /= i;
}
ans *= (2 * tmp + 1);
}
}
if (n != 1) {
ans *= 3;
}
out.println((ans + 1) / 2);
out.flush();
}
}
}
模拟一遍即可(我的方向感好差啊)
import java.util.Scanner;
public class G {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while (T-- != 0) {
int n = input.nextInt();
int m = input.nextInt();
char c[][] = new char[n][m];
for (int i = 0; i < n; i++) {
c[i] = input.next().toCharArray();
}
int turn = 0;
char[] s = input.next().toCharArray();
for (int i = 0; i < s.length; i++) {
if (s[i] == 'L') {
turn--;
} else {
turn++;
}
}
turn = (turn % 4 + 4) % 4;
if (turn == 0) {
System.out.println(n + " " + m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(c[i][j]);
}
System.out.println("");
}
} else if (turn == 2) {
System.out.println(n + " " + m);
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
System.out.print(c[i][j]);
}
System.out.println("");
}
} else if (turn == 1) { // R旋转、、
System.out.println(m + " " + n);
for (int i = 0; i < m; i++) {
for (int j = n - 1; j >= 0; j--) {
if(c[j][i] == '|'){
System.out.print("-");
}else if(c[j][i] == '-'){
System.out.print("|");
}else{
System.out.print(c[j][i]);
}
}
System.out.println("");
}
}else if(turn == 3){ // L旋转、、、
System.out.println(m + " " + n);
for(int i = m - 1; i >= 0; i--){
for(int j = 0; j < n; j++){
if(c[j][i] == '|'){
System.out.print("-");
}else if(c[j][i] == '-'){
System.out.print("|");
}else{
System.out.print(c[j][i]);
}
}
System.out.println("");
}
}
System.out.println("");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class J {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int t = (int) in.nval;
while (t-- > 0) {
int n = nextInt();
int[] a = new int[n];
int min = Integer.MAX_VALUE, max = -1;
for (int i = 0; i < n; i++) {
a[i] = nextInt();
max = Math.max(max, a[i]);
min = Math.min(min, a[i]);
}
long step = 0;
for (int i = 0; i < n; i++) {
step += (a[i] - min);
}
long ans = step + min;
out.println(step + " " + ans);
out.flush();
}
}
}
}
找规律、模拟、分别输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class K {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static char c[] = new char[100005];
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int t = nextInt();
while (t-- != 0) {
int n = nextInt();
c = next().toCharArray();
// for(int i = 0; i < c.length; i++){
// out.print(c[i]);
// }
// out.println();
// out.flush();
if (n == 1) {
System.out.println(String.valueOf(c));
} else {
int len = c.length; // 字符串的长度、、、
int tmp = 2 * n - 2; // 完整的一列 隔壁的一列相距的字符数、
for (int i = 0; i < len; i += tmp) { // 单独处理、第一行的、、、
out.print(c[i]);
}
int x = tmp;
for (int i = 1; i < n - 1; i++) { // 处理中间的、、
x -= 2;
for (int j = i; j < len; j += tmp) {
out.print(c[j]);
if (j + x < len) {
out.print(c[j + x]);
}
}
}
for (int i = n - 1; i < len; i += tmp) { // 处理最后一行的、、、
out.print(c[i]);
}
out.println();
}
out.flush();
}
}
}
解法一:枚举几个素数进行取模快速求解
解法二:分解质因数然后判断每个质因数的数量是否相等
解法三:取对数直接判断相等、注意误差
代码一:
import java.math.BigInteger;
import java.util.Scanner;
public class L {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
BigInteger MOD = new BigInteger("100000007");
while (T-- != 0) {
BigInteger x, a, y, b, ans1, ans2;
x = input.nextBigInteger();
a = input.nextBigInteger();
y = input.nextBigInteger();
b = input.nextBigInteger();
ans1 = x.modPow(a, MOD);
ans2 = y.modPow(b, MOD);
// System.out.println(ans1 + " " + ans2);
if (ans1.compareTo(ans2) == 0) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
代码二:
import java.util.Scanner;
public class L_ {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t-- != 0) {
int x, a, y, b;
x = input.nextInt();
a = input.nextInt();
y = input.nextInt();
b = input.nextInt();
System.out.println(solve(x, a, y, b));
}
}
public static String solve(int x, int a, int y, int b) {
if (x > y && a > b) {
return "No";
}
if (x < y && a < b) {
return "No";
}
if (a < b) { // 大的开小的方、、
solve(y, b, x, a);
}
if (Math.abs(Math.pow(x, a * (1.0) / b) - y) < 0.0000001) {
return "Yes";
}
return "No";
}
}
代码三:
import java.util.Scanner;
public class L_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t-- != 0) {
int x, a, y, b;
x = input.nextInt();
a = input.nextInt();
y = input.nextInt();
b = input.nextInt();
boolean flag = true;
while (true) {
if (x == y && a == b) {
break;
} else if (x == y || a == b) {
flag = false;
break;
}
if (x < y) { // 交换 、 x 为大的、、
int tmp = x;
x = y;
y = tmp;
tmp = a;
a = b;
b = tmp;
}
if (x % y != 0) {
flag = false;
break;
}
if (a >= b) {
flag = false;
break;
}
x /= y;
b -= a;
}
if (flag) {
System.out.println("Yes");
} else {
if (x == y && x == 1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
}
代码四:
import java.util.Scanner;
public class L_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-- != 0){
int x, a, y, b;
x = input.nextInt();
a = input.nextInt();
y = input.nextInt();
b = input.nextInt();
double ans1,ans2;
ans1 = a * Math.log(x);
ans2 = b * Math.log(y);
if(Math.abs(ans1 - ans2) < 0.00001){ // 需要说的是 牛客网的数据很水 只要 在0.1 -> 0.00001都可以通过
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
}