抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

1.

查找数组a中除0外的最小值 int a[] = {0,3,9,10,1,0,5}

1
2
3
4
5
6
7
8
9
10
11
12
public class Solution{
public static void main(String[] args) {
int a[] = {0,3,9,10,1,0,5};
int min = Integer.MAX_VALUE;
for(int i=0; i<a.length; i++){
if(min>a[i]&&a[i]!=0){
min = a[i];
}
}
System.out.println(min);
}
}

2.

将一个字符串反转输出的代码

1
2
3
4
5
6
7
8
9
public class Solution {
public static String reverseString(String s){
return new StringBuffer(s).reverse().toString();
}
public static void main(String[] args) {
String s = "Reverse this string";
System.out.println(reverseString(s));
}
}

3.

题目描述:写出一个数的质因数表示例如:236 = 2*2*59

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Solution {
/*
Pollard Rho因数分解
1) 对一个正整数 num 进行指数分解,应先找到一个最小的质数 i(也就是 2)
2) if(i == num),说明分解过程结束,打印并推成程序即可
3) if(i!=num) 但是 num%i==0,则打印出 i 的值,并将 num 除以 i 的商赋值给 num
4) 若 i!=num,而且 num%i!=0,则 i++

get:
若 num 能被 6 整除,那么 num 一定就能被 2,3整除,从而避免了被合数整除
*/
public static void decomposePrimeFactor(int num){
System.out.print(num+" = ");
int i = 2;
while(i<=num){
if(i == num){
System.out.print(i);
return;
}else if(num%i == 0){
System.err.print(i+"*");
num /= i;
}else
i++;
}
}
public static void main(String[] args) {
int num = 236
decomposePrimeFactor(num);
scanner.close();
}
}

4.

输出九九乘法表到文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.File;
import java.io.FileOutputStream;

public class Solution {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream(new File("九九乘法表.txt"));
for(int row=1; row<=9; row++){
for(int col=1; col<=row; col++){
String s = col+" * "+row+" = "+col*row+"\t";
byte[] buffer = s.getBytes();
fos.write(buffer);
}
fos.write("\n".getBytes());
}
fos.close();
}
}

5.

将一个int型数组写到一个temp.dat文件中,再从temp.dat中读出,并在控制台上逆序打印数组元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Solution {
public static void main(String[] args) throws IOException {
int[] arr = {1, 2, 3, 4, 5, 6};
//DataOutputStream允许应用程序以与机器无关方式(int, long, boolean等)从底层输入流中读写基本 Java 数据类型
DataOutputStream dos = new DataOutputStream(new FileOutputStream("temp.dat"));
for(int i:arr) dos.writeInt(i);
RandomAccessFile raf = new RandomAccessFile("temp.dat", "r");//RandomAccessFile支持“随机访问”的方式,程序快可以直接跳转到文件的任意地方来读写数据
int len = (int)raf.length()/4; //一个 int 占 4 个字节
for(int i=len-1; i>=0; i--){ //逆序输出
raf.seek(i*4);
System.out.println(raf.readInt());
}
raf.close();
dos.close();
}
}

6.

有一个数组pm25,里面存放了若干天pm2.5的值,要求统计pm2.5连续3天(含)以上超过75(含)的次数,int[] pm25 = {80,75,55,77,82,77,75,60,78,77,75};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
//双指针问题
public static void main(String[] args) {
int[] pm25 = {80,75,55,77,82,77,75,60,78,77,75};
int i = 0;
int sum = 0;
while(i<pm25.length){
if(pm25[i]>=75){
int j = i+1;
while(j<pm25.length && pm25[j]>=75) j++;
if(j-i>=3) sum++;
i = j;
}
i++;
}
System.out.println(sum);
}
}

7.

有一个数组p,里面存放了某支股票每天的价格。如果你最多只允许完成一次交易(某天买入,另外一天卖出),设计一个算法来找出最大利润。(保留两位小数)double p[] ={5.40,5.65,5.69,6.10,6.02,6.10,5.85,5.71,5.28,5.51};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {

public static double maxProfit(double[] prices){
double max = 0;
double min = prices[0];
//状态转移:第i天的最大收益 = maxProfit = max(第i-1天的最大收益, 第i天的股票价格-前i-1天中最低的股票价)
for(int i=1; i<prices.length; i++){
max = Math.max(max, prices[i]-min);
min = Math.min(min, prices[i]);
}
return max;
}

public static void main(String[] args) {
double p[] = {5.40,5.65,5.69,6.10,6.02,6.10,5.85,5.71,5.28,5.51};

System.out.printf("%.2f\n", maxProfit(p));
}
}

8.

列出某个文件夹中的所有文件和文件夹(指定文件类型和不指定类型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File;

public class Solution {
public static void main(String[] args) throws InterruptedException {
File file = new File("test");
if(file==null){
System.err.println("指定的文件不存在");
return;
}
if(file.isDirectory()){
File[] fileArr = file.listFiles();
for(File f : fileArr)
if(f.isDirectory())
System.out.println(f.getName()+"(Directory)");
else
System.err.println(f.getName());
}else
System.out.println(file.getName());
}
}

9.

算出2008-08-08距离今天的天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Date;
import java.util.GregorianCalendar;

public class Solution {

public static void main(String[] args) {
//为什么不用 Date past = new Date(2008, 8, 8)?
//因为 new Date(year, month, day)这个构造方法还要将year减去1900
// Date past = new Date(2008-1900, 8, 8);
Date past = new GregorianCalendar(2008, 8, 8, 0, 0, 0).getTime();
Date today = new Date();
System.out.println((today.getTime()-past.getTime())/1000/60/60/24);

}
}

10.

每个数均可表示成若干完全平方数(1,4,9,16,25…)之和,完全平方数是可重复,求出n的一种组合要求完全平方数的个数最少。如:12 = 4 + 4 + 4; 13 = 4 + 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.HashMap;
import java.util.LinkedList;

public class Solution {
static LinkedList<Integer> track = new LinkedList<>();
static HashMap<Integer, LinkedList<Integer>> res = new HashMap<>();
static int min = Integer.MAX_VALUE;

//用回溯法找到一个数的完全平方表示
public static void trackBack(int n, int[] arr){
if(n==0 && track.size()<min){
res.remove(min);
min = track.size();
res.put(track.size(), new LinkedList<Integer>(track));
return;
}
for(int i=arr.length-1; i>=0; i--){
if(n<0) continue;
track.add(arr[i]);
trackBack(n-arr[i], arr);
track.removeLast();
}
}

public static void perfectSquare(int num){
int n = (int)Math.sqrt(num);
int[] arr = new int[n];
for(int i=0; i<n; i++)
arr[i] = (int)Math.pow((i+1), 2);

trackBack(num, arr);

LinkedList<Integer> list = res.get(min);
System.out.print(num+" = ");
for(int i=0; i<list.size()-1; i++){
System.out.print(list.get(i)+" + ");
}
System.out.println(list.get(list.size()-1));
}

public static void main(String[] args) {
perfectSquare(12);
}
}

11.

编写一个单例模式的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public static void main(String[] args) {
/*
单例模式的三个关键点:
1、构造方法私有化
2、对象为static
3、getInstance()方法为public
*/
}
}

//饿汉式,考试的时候用这个吧,好记一点
class Single_1{
private Single_1(){}
private static Single_1 s = new Single_1();
public Single_1 getInstance(){
return s;
}
}

//懒汉式
class Single_2{
private static Single_2 s = null;
private Single_2(){}
public Single_2 getInstance(){
if(s==null)
s = new Single_2();
return s;
}
}

12.

编写程序模拟4个窗口卖100张票

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
public static void main(String[] args) {
Window w1 = new Window();
Window w2 = new Window();
Window w3 = new Window();
Window w4 = new Window();
w1.start();
w2.start();
w3.start();
w4.start();

Shop s = new Shop();
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
Thread t3 = new Thread(s);
Thread t4 = new Thread(s);
t1.start();
t2.start();
t3.start();
t4.start();

}
}

//方法一
class Window extends Thread{
private static int n = 1;
public void run(){
while(n<=100)
System.out.println(getName()+" 卖第 "+n+++" 张票");
}
}

//方法二
class Shop implements Runnable{
private int n = 1;
public void run() {
while(n<=100){
System.out.println(Thread.currentThread().getName()+ " 卖第 " +n+++" 张票");
}
}
}

13.

编写一个控制台程序显示当月的日历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Calendar;

public class Solution {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int curWeekday = c.get(Calendar.DAY_OF_WEEK);
int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int week = 1;

System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=0; i<curWeekday; i++){
System.out.print("\t");
week++;
}
for(int curDay=1; curDay<=maxDay; curDay++){
if(week%7==0){
System.out.println(curDay);
week++;
}else{
System.out.print(curDay+"\t");
week++;
}
}
}
}

14.

编写一个程序复制一个文件,如果文件很大(50M)怎么处理?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Solution {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("origin.txt");
FileOutputStream fos = new FileOutputStream("copy.txt");

byte[] buffer = new byte[1024];
int len = 0;
while((len=fis.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}

问:文件很大(50M)怎么处理?

答:创建一个字节数组作为缓冲区,每次先将读取到的内容存放在数组中,然后再一次性将数组中的内容写入到文件中,这样就提高了文件的读写效率。

评论