深度优先

这个家伙好懒,除了文章什么都没留下

0%

过河问题

时间限制:1000 ms | 内存限制:65535 KB

难度:5

描述

在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。

输入

第一行是一个整数T(1<=T<=20)表示测试数据的组数
每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河
每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。(0<Si<=100)

输出

输出所有人都过河需要用的最少时间

样例输入

1
2
3
1
4
1 2 5 10

样例输出

1
17 

这个题还是蛮有意思的,上图说话:

ac代码:

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

public class T9过河问题 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int[] out=new int[n];

for (int i = 0; i < out.length; i++) {
int t=sc.nextInt();
int[] data=new int[t];
for (int j = 0; j < data.length; j++) {
data[j]=sc.nextInt();
}
out[i]=f(data);
}

for (int i = 0; i < out.length; i++) {
System.out.println(out[i]);
}
}
}

private static int f(int[] data) {
Arrays.sort(data);//排序

int index=data.length-1;//数组下标
int ans=0; //最短过桥的和时间
int counts=data.length; //有多少人需要过河

if(counts==1){
return data[0];
}

//当人数大于等于四人时,可以采用两种方案运送最大的两个为一次行动
while(counts>=4){

int Min=data[0]; //用时最短的
int eMin=data[1]; //用时第二短的
int Max=data[index]; //用时最长的
int eMax=data[index-1];//用时第二长的

int sum1=eMin+Min+Max+eMin;//方案一:两小送两大的,用时:第二小+第一小+最大的+第二
int sum2=Max+Min+eMax+Min; //方案二:最小的分别送两大的,用时:最大的+最小的+第二大的+最小的

ans+=Math.min(sum1,sum2);//选择其中用时较短的一种方案
index-=2;//运送过去了两个人
counts-=2;
}
//最后还有三个人时,剩下三个相加
if(counts==3){
ans+=data[index]+data[0]+data[1];
}
//两个人时,用时为大的那个
if(counts==2){
ans+=data[1];
}
return ans;
}
}

写了十个常用的验证
html部分:

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
<body>
1:用户姓名 *只能是中文字*<br>
<input type="text" name="" id="id1">
<input type="button" name="" id="btn1" value="验证">
<samp id="samp1">*</samp>
<br>

2:用户密码 *大于等于6 必须是数字*<br>
<input type="text" name="" id="id2">
<input type="button" name="" id="btn2" value="验证">
<samp id="samp2">*</samp>
<br>

3:联系方式 *必须是11位,电信或者联通的有效号码* 如:15102739524移动 17614575414联通<br>
<input type="text" name="" id="id3">
<input type="button" name="" id="btn3" value="验证">
<samp id="samp3">*</samp>
<br>

4:邮箱方式 *如:kelvin@yeah.com\290000738@qq.com*<br>
<input type="text" name="" id="id4">
<input type="button" name="" id="btn4" value="验证">
<samp id="samp4">*</samp>
<br>

5:博客地址 *Http:\\www.boke.com*<br>
<input type="text" name="" id="id5">
<input type="button" name="" id="btn5" value="验证">
<samp id="samp5">*</samp>
<br>

6:年龄 *必须是数字 小于等120*<br>
<input type="text" name="" id="id6">
<input type="button" name="" id="btn6" value="验证">
<samp id="samp6">*</samp>
<br>

7:邮政编码 *中国邮政编码为6位数字*<br>
<input type="text" name="" id="id7">
<input type="button" name="" id="btn7" value="验证">
<samp id="samp7">*</samp>
<br>

8:身份证 *中国的身份证为15位或18位*<br>
<input type="text" name="" id="id8">
<input type="button" name="" id="btn8" value="验证">
<samp id="samp8">*</samp>
<br>

9:电话号码 *匹配形式如 0511-4405222 或 021-87888822*<br>
<input type="text" name="" id="id9">
<input type="button" name="" id="btn9" value="验证">
<samp id="samp9">*</samp>
<br>

10: QQ号 *腾讯QQ号从10000开始*<br>
<input type="text" name="" id="id10">
<input type="button" name="" id="btn10" value="验证">
<samp id="samp10">*</samp>
<br>
</body>

css部分:

1
2
3
4
5
6
7
8
9
<style type="text/css">
body{
font-size: 12px;
line-height: 30px;
}
samp{
color: red;
}
</style>

js部分:

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<script src="Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
//1用户名验证
$("#btn1").click(function () {
var mytext = $("#id1").val();
var res = /[^\x00-\x80]/;

$("#samp1").empty();
$("#samp1").append("*");

if (mytext == "") {
$("#samp1").append("用户名不能为空!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp1").append("输入的用户名只能为中文!");
return false;
}
return true;
});

//2密码验证
$("#btn2").click(function () {
var mytext = $("#id2").val();
var res = /[^\x00-\x80]/;

$("#samp2").empty();
$("#samp2").append("*");

if (mytext == "") {
$("#samp2").append("密码不能为空!");
return false;
}
else if (mytext.length<6) {
$("#samp2").append("输入的密码长度应大于等于6位!");
return false;
}
else if (isNaN(mytext)) {
$("#samp2").append("输入的密码只能为数字!");
return false;
}
return true;
});

//3联系方式
$("#btn3").click(function () {
var mytext = $("#id3").val();
var res = /^0?(13[0-9]|15[012356789]|17[0236789]|14[57])[0-9]{8}$/;

$("#samp3").empty();
$("#samp3").append("*");

if (mytext == "") {
$("#samp3").append("电话号码不能为空!");
return false;
}
else if (mytext.length != 11) {
$("#samp3").append("输入的电话号码长度应等于11位!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp3").append("号码必须是电信或者联通的有效号码!");
return false;
}
return true;
});

//4邮箱方式
$("#btn4").click(function () {
var mytext = $("#id4").val();
var res = /^([0-9A-Za-z\-_\.]+)@([0-9A-Za-z]+\.[A-Za-z]{2,3}(\.[A-Za-z]{2})?)$/;

$("#samp4").empty();
$("#samp4").append("*");

if (mytext == "") {
$("#samp4").append("邮箱地址不能为空!");
return false;
}
else if (!res.test(mytext)) {
$("#samp4").append("邮箱格式不符合要求!");
return false;
}
return true;
});

//5博客地址
$("#btn5").click(function () {
var mytext = $("#id5").val();
var res = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;

$("#samp5").empty();
$("#samp5").append("*");

if (mytext == "") {
$("#samp5").append("博客地址不能为空!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp5").append("博客地址格式不符合要求!");
return false;
}
return true;
});

//6年龄
$("#btn6").click(function () {
var mytext = $("#id6").val();

$("#samp6").empty();
$("#samp6").append("*");

if (mytext == "") {
$("#samp6").append("请输入年龄!!");
return false;
}
else if (isNaN(mytext)) {
$("#samp6").append("年龄必须为数字!");
return false;
}
else if (Number(mytext) <= 0 || Number(mytext) >= 120) {
$("#samp6").append("年龄必须大于0,且小于120!");
return false;
}
return true;
});

//7邮政编码
$("#btn7").click(function () {
var mytext = $("#id7").val();
var res = /^[a-zA-Z0-9 ]{3,12}$/;

$("#samp7").empty();
$("#samp7").append("*");

if (mytext == "") {
$("#samp7").append("请输入邮政编码!!");
return false;
}
else if (isNaN(mytext)) {
$("#samp7").append("邮政编码必须为数字!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp7").append("邮政编码不符合要求!");
return false;
}
return true;
});

//8身份证
$("#btn8").click(function () {
var mytext = $("#id8").val();
var res = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

$("#samp8").empty();
$("#samp8").append("*");

if (mytext == "") {
$("#samp8").append("请输入身份证!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp8").append("身份证号码不符合要求!");
return false;
}
return true;
});

//9电话号码
$("#btn9").click(function () {
var mytext = $("#id9").val();
var res = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;

$("#samp9").empty();
$("#samp9").append("*");

if (mytext == "") {
$("#samp9").append("请输入电话号码!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp9").append("电话号码不符合要求!");
return false;
}
return true;
});

//10QQ号
$("#btn10").click(function () {
var mytext = $("#id10").val();
var res = /^[1-9][0-9]{4,}$/;

$("#samp10").empty();
$("#samp10").append("*");

if (mytext == "") {
$("#samp10").append("请输入QQ号!");
return false;
}
else if (!res.exec(mytext)) {
$("#samp10").append("QQ号码不符合要求!");
return false;
}
return true;
});
});
</script>

寻找最大数(三)

时间限制:1000 ms | 内存限制:65535 KB

难度:2

描述

给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数。

求这个新的整数的最大值是多少。

输入

多组测试数据。
每组测试数据占一行,每行有两个数N和K (1 ≤ N≤ 10^18; 0 ≤ K ≤ 100).

输出

每组测试数据的输出占一行,输出移动后得到的新的整数的最大值。

样例输入

1
2
3
1990 1
100 0
9090000078001234 6

样例输出

1
2
3
9190
100
9907000008001234
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
import java.util.Scanner;

public class T13寻找最大数 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String num=sc.next();
int k=sc.nextInt();
String s=f(num,k);
System.out.println(s);
}
}

private static String f(String num, int k) {

char[] ch=num.toCharArray();
while(k>0){
for (int i = 0; i < ch.length; i++) {
char c=ch[i];
int index=0;
for (int j = i; j <= k+i&&j<ch.length; j++) {
if(ch[j]>c){
c=ch[j];
index=j;
}
}
if(index>0){
k-=(index-i);
for (int j = index; j >i ; j--) {
char h=ch[j];
ch[j]=ch[j-1];
ch[j-1]=h;
}
}
}
break;
}
String str=new String(ch);
return str;
}
}