Time-Traveler

时光旅人,向往美好

0%

190709 贪婪算法练习

变形虫碰撞(点击可以跳转)

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

public class Main {
public static void main(String[] args) {
//读取键盘输入
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
double[] weight = new double[num];
for(int i=0;i<num;i++){
weight[i] = scan.nextDouble();
}

//小到大排序
Arrays.sort(weight);
double SumWeight=weight[weight.length-1];

//计算几何平均值
for(int i = num-1;i>0;i--){
double sqrt = Math.sqrt(SumWeight*weight[i-1]);
SumWeight=2 * sqrt;
}

//格式化输出
String parten = "#0.000";
DecimalFormat decimal = new DecimalFormat(parten);
String reweight = decimal.format(SumWeight);
System.out.println(reweight);
}
}

电话号码减法游戏

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

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int length = scan.nextInt();
int num8 = 0;
int numOther = 0;

String TelPhoNum = scan.next().substring(0,length-10);
char[] charTPN = TelPhoNum.toCharArray();
for (char a: charTPN) {
String n = a+"";
int b = Integer.parseInt(n);
if(b==8){
num8++;
}else{
numOther++;
}
}

if(num8>numOther){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}

农场主安排奶牛工作

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

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

ArrayList<Cow> cow = new ArrayList<Cow>(cowNumber);

for(int i=0;i<cowNumber;i++){
int a = scan.nextInt();
int b = scan.nextInt();
Cow newcow = new Cow(a,b);
cow.add(newcow);
}


int workCowNum=0;
int worktime=0;
int workstart = cow.get(0).start;
int workend = cow.get(0).end;
boolean iswork=false;
boolean isend=false;
for (int i=0;i<cow.size();i++){
for(int j=i;j<cow.size();j++){
if(cow.get(j).end>=end){
i=j;
iswork = true;
isend = true;
break;
}
if(workstart<=cow.get(j).start&&workend>=cow.get(j).end){
if(worktime<=cow.get(j).end-workend) {
iswork=true;
i=j;
}
}
}

if(iswork){
workstart = workend;
workend = cow.get(i).end;
worktime = cow.get(i).end - workend;
workCowNum++;
iswork = false;
}

if(isend){
break;
}
}

System.out.println(workCowNum+1);
}
}

class Cow{
int start;
int end;
public Cow(int a,int b){
this.start = a;
this.end = b;
}
}