我是靠谱客的博主 拼搏老鼠,最近开发中收集的这篇文章主要介绍java 练手= = 做杭电的一些简单题学习java。,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HDU 2012


//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int n = cin.nextInt();
int m = cin.nextInt();
if (n == 0 && m == 0)
break;
if (judge(n, m))
System.out.println("OK");
else
System.out.println("Sorry");
}
}
public static boolean judge(int a, int b) {
int ok = 1;
for (int i = a; i <= b; i++) {
if (check(get(i)) == false) {
ok = 0;
}
}
if (ok == 0)
return false;
else
return true;
}
public static int get(int a) {
return a * a + a + 41;
}
public static boolean check(int a) {
if (a == 2 || a == 3)
return true;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
}





HDU 2011




//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while (t-->0) {
DecimalFormat df=new DecimalFormat("#0.00");
int n = cin.nextInt();
System.out.println(df.format(solve(n)));
}
}
public static boolean check(int a) {
int[] b = new int[3];
int len = 0;
int res = a;
while (a != 0) {
b[len++] = a % 10;
a /= 10;
}
int ans = 0;
for (int i = 0; i < len; i++)
ans += b[i] * b[i] * b[i];
return ans == res;
}
public static double solve(int m) {
int ok = 0;
double fm = 1;
double sum = 0;
for (int i = 1; i <= m; i++) {
ok++;
if (ok % 2 == 1) {
sum = sum + (1 / fm);
fm++;
} else {
sum = sum - (1 / fm);
fm++;
}
//System.out.println(i/fm);
}
return sum;
}
}





HDU 2013


//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int n=cin.nextInt();
int a[]=new int[n];
int answer=1;
for(int i=2;i<=n;i++)
answer=(answer+1)*2;
System.out.println(answer);
}
}
public static boolean judge(int a, int b) {
int ok = 1;
for (int i = a; i <= b; i++) {
if (check(get(i)) == false) {
ok = 0;
}
}
if (ok == 0)
return false;
else
return true;
}
public static int get(int a) {
return a * a + a + 41;
}
public static boolean check(int a) {
if (a == 2 || a == 3)
return true;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
}




HDU 2010



//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int n = cin.nextInt();
int m = cin.nextInt();
int ok = 0;
int j = 0;
for (int i = n; i <= m; i++)
if (check(i)) {
ok = 1;
j = i + 1;
System.out.print(i);
break;
}
if (ok == 0) {
System.out.println("no");
continue;
}
for (j = j; j <= m; j++) {
if (check(j)) {
System.out.print(" " + j);
}
}
System.out.println();
}
}
public static boolean check(int a) {
int[] b = new int[3];
int len = 0;
int res = a;
while (a != 0) {
b[len++] = a % 10;
a /= 10;
}
int ans = 0;
for (int i = 0; i < len; i++)
ans += b[i] * b[i] * b[i];
return ans == res;
}
}





HDU 2026


//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
// cin.useDelimiter("n");
输入带有空格的字符串。
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
cin.useDelimiter("n");
String str = cin.nextLine();
char[] s = new char[205];
int len = 0;
for (int i = 0; i < str.length(); i++) {
s[i] = str.charAt(i);
len++;
}
if (s[0] >= 'a' && s[0] <= 'z')
s[0] -= 32;
System.out.print(s[0]);
for (int i = 1; i < len; i++) {
if (s[i] == ' ') {
if (s[i + 1] >= 'a' && s[i + 1] <= 'z')
s[i + 1] -= 32;
}
System.out.print(s[i]);
}
System.out.println();
}
}
}




HDU 5907



//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
// cin.useDelimiter("n");
输入带有空格的字符串。
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while (t-- > 0) {
String str = cin.next();
long ans = 0;
long cnt = 0;
for (int i = 0; i < str.length(); i++) {
char st = str.charAt(i);
if (st == 'q') {
cnt++;
} else {
ans = ans + cnt * (cnt + 1) / 2;
cnt = 0;
}
}
ans = ans + cnt * (cnt + 1) / 2;
System.out.println(ans);
}
}
}







HDU 2544



//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int n, m;
int inf = 0x7f7f7f;
n = cin.nextInt();
m = cin.nextInt();
if (m == 0 && n == 0)
break;
int[][] mat = new int[100 + 10][100 + 10];
for (int i = 1; i <= 105; i++)
for (int j = 1; j <= 105; j++)
if (i == j)
mat[j][i] = mat[i][j] = 0;
else
mat[i][j] = mat[j][i] = inf;
for (int i = 1; i <= m; i++) {
int a, b, c;
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextInt();
if (mat[a][b] > c)
mat[a][b] = mat[b][a] = c;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
mat[i][j] = Math.min(mat[i][j], mat[i][k] + mat[k][j]);
System.out.println(mat[1][n]);
}
}
}





HDU 5912



连分数。

//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
// cin.useDelimiter("n");
输入带有空格的字符串。
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int Case = 0;
while (t-- > 0) {
int n = cin.nextInt();
long[] a = new long[n + 10];
long[] b = new long[n + 10];
for (int i = 1; i <= n; i++)
a[i] = cin.nextLong();
for (int i = 1; i <= n; i++)
b[i] = cin.nextLong();
// System.out.println(gcd(6,4));
long fz, fm, fz1, fm1;
fm = a[n];
fz = b[n];
for (int i = n - 1; i >= 1; i--) {
fz1 = a[i];
fm1 = 1;
long fenmu = fm1 * fm;
long fenzi = fz1 * fm + fz * fm1;
long Gcd = gcd(fenmu, fenzi);
fenmu /= Gcd;
fenzi /= Gcd;
// System.out.println(fenzi+" "+fenmu);
long x = b[i];
long fmm = fenzi;
long fzz = fenmu * x;
Gcd = gcd(fmm, fzz);
fmm /= Gcd;
fzz /= Gcd;
fm = fmm;
fz = fzz;
}
long q = gcd(fm, fz);
fm /= q;
fz /= q;
System.out.print("Case #");
System.out.print(++Case + ": ");
System.out.println(fz + " " + fm);
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}




HDU 5920



这题方法早就想到了,,,不过用网上c++大数模板搞不出来。。然而java大数类不熟悉,比赛的时候队友写的。。

今晚花了一个小时用java大数搞了搞,终于ac了。方法就是取一半减1,构造这个回文串。小于20的时候特判就行了。



//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
public class Main {
public static final double PI = 3.1415927;
private static final BigInteger ZERO = null;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int Case = 0;
while (t-- > 0) {
BigInteger num = cin.nextBigInteger();
BigInteger[] ans = new BigInteger[55];
System.out.println("Case #" + ++Case + ":");
int[] a = new int[1005];
int[] b = new int[1005];
int[] c = new int[1005];
int sb = 0;
while (num.compareTo(BigInteger.valueOf(20)) > 0) {
BigInteger s = num;
int cnt = 0;
while (s.compareTo(BigInteger.valueOf(0)) > 0) {
a[cnt++] = s.mod(BigInteger.valueOf(10)).intValue();
s = s.divide(BigInteger.valueOf(10));
}
for (int i = 0; i < cnt; i++) {
// System.out.print(a[i] + "
");
b[cnt - 1 - i] = a[i];
}
int len = cnt + 1;
len /= 2;
BigInteger sum = BigInteger.ZERO;
for (int i = 0; i < len; i++) {
sum = sum.multiply(BigInteger.valueOf(10));
sum = sum.add(BigInteger.valueOf(b[i]));
}
sum = sum.subtract(BigInteger.valueOf(1));
BigInteger op = sum;
int sz = 0;
while (op.compareTo(BigInteger.valueOf(0)) > 0) {
c[sz++] = op.mod(BigInteger.valueOf(10)).intValue();
op = op.divide(BigInteger.valueOf(10));
}
if (cnt % 2 == 0) {
for (int i = 0; i < sz; i++) {
sum = sum.multiply(BigInteger.valueOf(10));
sum = sum.add(BigInteger.valueOf(c[i]));
}
} else {
for (int i = 1; i < sz; i++) {
sum = sum.multiply(BigInteger.valueOf(10));
sum = sum.add(BigInteger.valueOf(c[i]));
}
}
//System.out.println(sum);
num = num.subtract(sum);
ans[sb++] = sum;
}
while (num.compareTo(BigInteger.valueOf(9)) > 0) {
num = num.subtract(BigInteger.valueOf(9));
ans[sb++] = BigInteger.valueOf(9);
}
if (num.compareTo(BigInteger.valueOf(0)) > 0) {
ans[sb++] = num;
}
System.out.println(sb);
for(int i=0;i<sb;i++)
System.out.println(ans[i]);
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}

POJ 2251



三维广搜,java代码并不能AC(照着AC代码c++改的),真是玄学了。

明白了几点,要想调用类要用static来修饰,然后变量先初始化下。方法内的数组下标默认从1开始,代入0会越界。队列推入的时候要将值一起推入。

先贴个代码,有时间改改AC掉。基本已经没错了。

//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
//Queue<node> queue = new LinkedList<node>();
声明队列
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
// int t = cin.nextInt();
// int Case = 0;
while (cin.hasNext()) {
int n, m, k;
k = cin.nextInt();
n = cin.nextInt();
m = cin.nextInt();
if (k == 0 && n == 0 && m == 0)
break;
int[][][] vis = new int[110][110][110];
char[][][] mat = new char[110][110][110];
int[][] dir = new int[6][3];
dir[0][0] = 0;
dir[0][1] = 1;
dir[0][2] = 0;
dir[1][0] = 0;
dir[1][1] = -1;
dir[1][2] = 0;
dir[2][0] = 1;
dir[2][1] = 0;
dir[2][2] = 0;
dir[3][0] = -1;
dir[3][1] = 0;
dir[3][2] = 0;
dir[4][0] = 0;
dir[4][1] = 0;
dir[4][2] = 1;
dir[5][0] = 0;
dir[5][1] = 0;
dir[5][2] = -1;
for (int i = 1; i <= 108; i++)
for (int j = 1; j <= 108; j++)
for (int z = 1; z <= 108; z++) {
vis[i][j][z] = 0;
mat[i][j][z]='#';
}
int x1, y1, z1;
x1 = y1 = z1 = 1;
int x2, y2, z2;
x2 = y2 = z2 = 1;
for (int i = 1; i <= k; i++)
for (int j = 1; j <= n; j++) {
String s = cin.next();
for (int z = 1; z <= m; z++) {
char str = s.charAt(z - 1); // 默认从0开始
mat[i][j][z] = str;
// System.out.println(mat[i][j][z]);
if (str == 'S') {
x1 = i;
y1 = j;
z1 = z;
} else if (str == 'E') {
x2 = i;
y2 = j;
z2 = z;
}
}
}
int ans = bfs(x1, y1, z1, vis, mat, x2, y2, z2, k, n, m, dir);
// System.out.println(ans);
if (ans == -1) {
System.out.println("Trapped!");
} else {
System.out.println("Escaped in " + ans + " minute(s).");
}
}
}
public static class node {
int x, y, z;
int time;
public node(int x1, int y1, int z1, int time1) {
this.x = x1;
this.y = y1;
this.z = z1;
this.time = time1;
}
}
public static int bfs(int x, int y, int z, int vis[][][], char mat[][][],
int xx, int yy, int zz, int k, int n, int m, int dir[][]) {
vis[x][y][z] = 1;
Queue<node> queue = new LinkedList<node>();
while (queue.size() > 0)
queue.poll();
node st = new node(0, 0, 0, 0);
node ed = new node(0, 0, 0, 0);
st.x = x;
st.y = y;
st.z = z;
st.time = 0;
// System.out.println(st.x+"
"+st.y+"
"+st.z);
queue.add(st);
while (queue.size() != 0) {
st = queue.peek();
queue.poll();
// System.out.println(queue.size());
// System.out.println(queue.size());
if (st.x == xx && st.y == yy && st.z == zz) {
return st.time;
}
for (int i = 0; i < 6; i++) {
// System.out.println(11111111);
ed.x = st.x + dir[i][0];
ed.y = st.y + dir[i][1];
ed.z = st.z + dir[i][2];
ed.time = st.time + 1;
// System.out.println(st.x + "
" + st.y + "
" + st.z);
// System.out.println(ed.x + "
" + ed.y + "
" + ed.z);
if (check(ed.x, ed.y, ed.z, k, n, m, mat, vis) == true) {
ed = new node(ed.x, ed.y, ed.z, ed.time);
vis[ed.x][ed.y][ed.z] = 1;
queue.add(ed);
// System.out.println(queue.size());
}
}
}
// System.out.println(queue.size());
return -1;
}
public static boolean check(int x, int y, int z, int k, int n, int m,
char mat[][][], int vis[][][]) {
if (mat[x][y][z] != '#' && x >= 1 && x <= k && y >= 1 && y <= n
&& z >= 1 && z <= m && vis[x][y][z] == 0)
return true;
else
return false;
}
}
/*
* 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E
* 1 3 3 S## #E# ###
*/






HDU 1172


猜数字,很水的题。瞎几把暴力。

//package com.code;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
//Queue<node> queue = new LinkedList<node>();
声明队列
public class Main {
public static final double PI = 3.1415927;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
// int t = cin.nextInt();
// int Case = 0;
while (cin.hasNext()) {
int n = cin.nextInt();
if (n == 0)
break;
int[] a = new int[105];
int[] b = new int[105];
int[] c = new int[105];
for (int i = 1; i <= n; i++) {
a[i] = cin.nextInt();
b[i] = cin.nextInt();
c[i] = cin.nextInt();
}
int number = 0;
int ans = 1000;
for (int i = 1000; i <= 9999; i++) {
if (check(i, n, a, b, c) == 1) {
number++;
ans = i;
if (number >= 2)
break;
}
}
if (number >= 2) {
System.out.println("Not sure");
} else {
System.out.println(ans);
}
}
}
public static int check(int number, int n, int a[], int b[], int c[]) {
int vis1[] = new int[10];
int vis2[] = new int[10];
int ok = 1;
for (int i = 1; i <= n; i++) {
int res = number;
int ans = a[i];
int ok1, ok2;
ok1 = ok2 = 0;
int cnt = 0;
while (res != 0) {
vis1[cnt] = res % 10;
vis2[cnt++] = ans % 10;
if (res % 10 == ans % 10)
ok2++;
res /= 10;
ans /= 10;
}
for (int k = 0; k < 4; k++) {
for (int z = 0; z < 4; z++) {
if (vis1[k] == vis2[z] && vis2[z] != -1) {
ok1++;
vis2[z] = -1;
break;
}
}
}
for (int k = 0; k < 4; k++) {
vis1[k] = vis2[k] = 0;
}
if (ok1 != b[i] || ok2 != c[i]) {
ok = 0;
break;
}
}
return ok;
}
}








java老师上课布置的,很水,全放一起。


package assignment.ass01;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
public class homework1 {
public static void main(String[] args) {
Random random = new Random();
int card = random.nextInt(52) + 1;
work(card);
}
public static void work(int a) {
int b = a % 13;
if (b == 0) {
b = 13;
}
System.out.print("The card you picked is ");
for (int i = 2; i <= 10; i++) {
if (b == i)
System.out.print(i + " " + "of ");
}
if (b == 1)
System.out.print("Ace" + " " + "of ");
if (b == 11)
System.out.print("Jack" + " " + "of ");
if (b == 12)
System.out.print("Queen" + " " + "of ");
if (b == 13)
System.out.print("King" + " " + "of ");
if (judge(a) == 0) {
System.out.println("Clubs");
} else if (judge(a) == 1) {
System.out.println("Diamonds");
} else if (judge(a) == 2) {
System.out.println("Hearts");
} else if (judge(a) == 3) {
System.out.println("Spades");
}
}
public static int judge(int a) {
return (a - 1) / 13;
}
}
package assignment.ass01;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
public class homework2 {
public static void main(String[] args) {
work();
}
public static void work() {
Random random = new Random();
int zm, fm;
zm = fm = 0;
for (int i = 1; i <= 1e6; i++) {
int number = random.nextInt();
if (number % 2 == 0)
zm++;
else
fm++;
}
System.out.println("正面出现的次数是:" + zm);
System.out.println("反面出现的次数是:" + fm);
}
}
package assignment.ass01;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
public class homework3 {
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
int[] a = new int[1000];
int sz = 0;
int max = -1;
System.out.print("Enter numbers: ");
while (true) {
a[sz++] = cin.nextInt();
max = Math.max(max, a[sz - 1]);
if (a[sz - 1] == 0)
break;
}
work(a, sz, max);
}
public static void work(int a[], int len, int max) {
int count = 0;
for (int i = 0; i < len; i++) {
if (a[i] == max)
count++;
}
System.out.println("The largest number is " + max);
System.out.println("The occurrence count of the largest number is "
+ count);
}
}
package assignment.ass01;
import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000");
import java.util.Scanner;
import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Arrays; // Arrays.sort(s);
import java.math.BigInteger;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
public class homework4 {
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
for (int i = 10, s = 0;; i++) {
if (judge(i) == true) {
s++;
System.out.print(i + " ");
if (s % 10 == 0)
System.out.println();
}
if (s == 100)
break;
}
}
public static boolean check(int a) {
if (a < 10)
return false;
else {
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
}
return true;
}
public static boolean judge(int a) {
int[] b = new int[10];
int sb = 0;
int num1 = a;
while (a != 0) {
b[sb++] = a % 10;
a /= 10;
}
int i, j;
for (i = 0; i < sb / 2; i++) {
if (b[i] != b[sb - 1 - i])
break;
}
int num2 = 0;
for (j = 0; j < sb; j++) {
num2 = num2 * 10 + b[j];
}
if (i == sb / 2 || check(num1) == false || check(num2) == false)
return false;
else
return true;
}
}



最后

以上就是拼搏老鼠为你收集整理的java 练手= = 做杭电的一些简单题学习java。的全部内容,希望文章能够帮你解决java 练手= = 做杭电的一些简单题学习java。所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部