开始学Java之19-High-Tide,-Low-Tide-Time

别想歪。。

乱七八糟的代码:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
High Tide, Low Tide Time

Problem Description
Joe Coder is camping near the Bay of Fundy between Nova Scotia and New Brunswick.
When he arrived at the bay, he was told that the difference in height between high
tide and low tide at the Bay of Fundy was the largest tidal difference in the world.
Ever the skeptic, Joe decided to verify this. He chose a reference point and, after
learning from the radio when the tides were highest and lowest, he went with a boat
to his reference point and measured the depth of the water.
Unfortunately, on the last day of his trip, a strong wind scattered his measurements.
Joe has recovered all of his measurements, but they may not be in their original order.
Luckily, he remembers some things about his measurements:
• He started measuring water levels at a low tide, his second measurement was of the water
level at high tide, and after that the measurements continued to alternate between low and
high tides.
• All high tide measurements were higher than all low tide measurements.
• Joe noticed that as time passed, the high tides only became higher and the low tides
only became lower.
Given Joe’s measurements in no particular order, you must reconstruct the correct order
in which the measurements were taken.
Input Specification
The first line contains the integer N (1 ≤ N ≤ 100).
The next line contains N distinct space separated positive integers, where each integer
is at most 1 000 000.

Output Specification
Output the N integers in the unique order that Joe originally took the measurements.
Sample Input
8
10 50 40 7 3 110 90 2
Output for Sample Input
10 40 7 50 3 90 2 110
Explanation of Output for Sample Input
The low tide measurements (in order) were 10, 7, 3, and 2. The high tide measurements (in order) were
40, 50, 90, and 110.
*/

import java.util.Scanner;

public class Main {
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {

System.out.println("Input N:");
int n = scan.nextInt();

//int[] low = new int[n/2], high = new int[n/2];

System.out.println("Input integers:");
int[] ints = new int[n];
for (int i=0; i<n; ++i)
ints[i] = scan.nextInt();
scan.close();

int temp;
for (int i=0; i<n; ++i){
for (int j=i+1; j<n; ++j){
if (ints[j] < ints[j-1]){
temp = ints[j];
ints[j] = ints[j-1];
ints[j-1] = temp;
}
}
//System.out.println(ints[i]);
}

for (int i=0; i<4; ++i)
System.out.print(ints[i] + " " + ints[i+4] + " ");

//for (int low=0, high=4; low<4 && high<8; ++low, ++high)
// System.out.print(ints[low] + " " + ints[high] + " ");

//for (int low=3, high=4; low>=0 && high<8; --low, ++high)
// System.out.print(ints[low] + " " + ints[high] + " ");




/*
String intsString = scan.nextLine();
int countInt, countStart;
for (int i=1, t=intsString.length(); i<t; ++i){
if (Character.isSpaceChar(intsString.charAt(i))){
countInt++; countStart=i;

intsInt[countInt] = intsString.substring(countStart, i);
System.out.println(intsInt[countInt]);
}
}*/
}
}

下断点看了好几次,发现这个仅适用于例子:10 50 40 7 3 110 90 2,把10放到最后面就不行了。改了一下。。。

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
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.println("Input N:");
int n = scan.nextInt();

System.out.println("Input integers:");
int[] ints = new int[n];
for (int i=0; i<n; ++i)
ints[i] = scan.nextInt();
scan.close();

int temp;
for (int i=0; i<n; ++i){
for (int j=i+1; j<n; ++j){
if (ints[j] < ints[i]){
temp = ints[j];
ints[j] = ints[i];
ints[i] = temp;
}
}
}

for (int low=(n/2)-1, high=n/2; low>=0 && high<=n; --low, ++high)
System.out.print(ints[low] + " " + ints[high] + " ");
}
}

Input N:
12
Input integers:
50 40 7 3 110 90 2 10 1 4 11 46 85 69 54 498 8 9 45 64
10 11 7 40 4 46 3 50 2 90 1 110

-

Input N:
8
Input integers:
2 40 3 50 7 90 10 110
10 40 7 50 3 90 2 110

应该是没bug了…

最后就是这块代码

1
2
3
4
5
6
7
8
9
10
int temp;
for (int i=0; i<n; ++i){
for (int j=i+1; j<n; ++j){
if (ints[j] < ints[i]){
temp = ints[j];
ints[j] = ints[i];
ints[i] = temp;
}
}
}

想不到有啥优化方法…

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...