算法笔记_108:第四届蓝桥杯软件类省赛真题(JAVA软件开发本科A组)试题解答
目录
前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~
1 世纪末的星期
标题: 世纪末的星期 曾有邪教称1999年12月31日是世界末日。当然该谣言已经不攻自破。 还有人称今后的某个世纪末的12月31日,如果是星期一则会.... 有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!! 于是,“谣言制造商”又修改为星期日...... 1999年的12月31日是星期五,请问:未来哪一个离我们最近的一个世纪末年(即xx99年)的12月31日正好是星期天(即星期日)? 请回答该年份(只写这个4位整数,不要写12月31等多余信息) 2299(使用Excel表格解答,选择格式中时间输出为星期几,然后在表格式输入XX99-12-31,输出星晴天则为所求)
引用网友代码:
public class Test
{
public static void main(String[] args)
{
int year = 2000;
int total = 0;
for( ; ; year++)
{
if(year%400==0 || (year%4==0 && year%100!=0))
{
total += 366;
}
else
{
total += 365;
}
if((total+5)%7 == 0 && (year+"").endsWith("99"))
{
System.out.println(year);
break;
}
}
}
}
2 振兴中华
标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格子。 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振
我做起振兴
做起振兴中
起振兴中华 比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。 要求跳过的路线刚好构成“从我做起振兴中华”这句话。 请你帮助小明算一算他一共有多少种可能的跳跃路线呢? 答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。
35 数学方法秒杀:
一共要向下走3步,向右走4步,这7步的顺序是不确定的,
就相当于从7步当中选出3步向下走,剩余4步向右走
那么可能的路线数就等于上述选择的方法数就是C(7,3) 代码求解:使用BFS遍历
import java.util.ArrayList; public class Main { public static int[][] move = {{1,0},{0,1}}; //表示向下和向右移动一步 static class point {
public int x;
public int y; point(int x, int y) {
this.x = x;
this.y = y;
}
} public boolean check(int x, int y) {
if(x > 4 || y > 5)
return false;
return true;
} public void printResult() {
point start = new point(1, 1);
ArrayList<point> list = new ArrayList<point>();
list.add(start);
int result = 0;
while(list.size() != 0) {
point a = list.get(0);
list.remove(0); if(a.x == 4 && a.y == 5) {
result++;
continue;
} for(int i = 0;i < 2;i++) {
int x = a.x + move[i][0];
int y = a.y + move[i][1];
if(check(x, y)) {
list.add(new point(x, y));
}
}
}
System.out.println(result);
} public static void main(String[] args) {
Main test = new Main();
test.printResult();
}
}
3 梅森素数
标题: 梅森素数 如果一个数字的所有真因子之和等于自身,则称它为“完全数”或“完美数” 例如:6 = 1 + 2 + 3 28 = 1 + 2 + 4 + 7 + 14 早在公元前300多年,欧几里得就给出了判定完全数的定理: 若 2^n - 1 是素数,则 2^(n-1) * (2^n - 1) 是完全数。 其中 ^ 表示“乘方”运算,乘方的优先级比四则运算高,例如:2^3 = 8, 2 * 2^3 = 16, 2^3-1 = 7 但人们很快发现,当n很大时,判定一个大数是否为素数到今天也依然是个难题。 因为法国数学家梅森的猜想,我们习惯上把形如:2^n - 1 的素数称为:梅森素数。 截止2013年2月,一共只找到了48个梅森素数。 新近找到的梅森素数太大,以至于难于用一般的编程思路窥其全貌,所以我们把任务的难度降低一点: 1963年,美国伊利诺伊大学为了纪念他们找到的第23个梅森素数 n=11213,在每个寄出的信封上都印上了“2^11213-1 是素数”的字样。 2^11213 - 1 这个数字已经很大(有3000多位),请你编程求出这个素数的十进制表示的最后100位。 答案是一个长度为100的数字串,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。 8586718527586602439602335283513944980064327030278104224144971883680541689784796267391476087696392191 使用Java中大整数类来求解:
import java.math.BigInteger; public class Main { public static void main(String[] args) {
BigInteger a = new BigInteger("1");
BigInteger b = new BigInteger("2");
for(int i = 0;i < 11213;i++)
a = a.multiply(b);
a = a.subtract(new BigInteger("1"));
String A = a.toString();
int len = A.length();
A = A.substring(len - 100);
System.out.println(A);
} }
4 颠倒的价牌
标题: 颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店。 其标价都是4位数字(即千元不等)。 小李为了标价清晰、方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参见p1.jpg)。 这种价牌有个特点,对一些数字,倒过来看也是合理的数字。如:1 2 5 6 8 9 0 都可以。这样一来,如果牌子挂倒了,有可能完全变成了另一个价格,比如:1958 倒着挂就是:8561,差了几千元啊!! 当然,多数情况不能倒读,比如,1110 就不能倒过来,因为0不能作为开始数字。 有一天,悲剧终于发生了。某个店员不小心把店里的某两个价格牌给挂倒了。并且这两个价格牌的电视机都卖出去了! 庆幸的是价格出入不大,其中一个价牌赔了2百多,另一个价牌却赚了8百多,综合起来,反而多赚了558元。 请根据这些信息计算:赔钱的那个价牌正确的价格应该是多少? 答案是一个4位的整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。 9088
import java.util.ArrayList; public class Main { public ArrayList<number> list2 = new ArrayList<number>();
public ArrayList<number> list8 = new ArrayList<number>(); static class number {
public int num; //原金额
public int money; //反转后,差额 number(int num, int money) {
this.num = num;
this.money = money;
}
} public boolean judge(int n) {
int a1 = n % 10;
int a2 = n / 10 % 10;
int a3 = n / 100 % 10;
int a4 = n / 1000;
if(a1 == 0 || a1 == 3 || a1 == 4 || a1 == 7)
return false;
if(a2 == 3 || a2 == 4 || a2 == 7)
return false;
if(a3 == 3 || a3 == 4 || a3 == 7)
return false;
if(a4 == 3 || a4 == 4 || a4 == 7)
return false;
return true;
} public boolean judge1(int n) {
int a1 = n % 10;
int a2 = n / 10 % 10;
int a3 = n / 100 % 10;
int a4 = n / 1000; if(a1 == 6)
a1 = 9;
else if(a1 == 9)
a1 = 6; if(a2 == 6)
a2 = 9;
else if(a2 == 9)
a2 =6; if(a3 == 6)
a3 = 9;
else if(a3 == 9)
a3 = 6; if(a4 == 6)
a4 = 9;
else if(a4 == 9)
a4 = 6;
int temp = n - (a1 * 1000 + a2 * 100 + a3 * 10 + a4);
if(temp > 200 && temp < 300) {
list2.add(new number(n, temp));
return true;
}
return false;
} public boolean judge2(int n) {
int a1 = n % 10;
int a2 = n / 10 % 10;
int a3 = n / 100 % 10;
int a4 = n / 1000; if(a1 == 6)
a1 = 9;
else if(a1 == 9)
a1 = 6; if(a2 == 6)
a2 = 9;
else if(a2 == 9)
a2 =6; if(a3 == 6)
a3 = 9;
else if(a3 == 9)
a3 = 6; if(a4 == 6)
a4 = 9;
else if(a4 == 9)
a4 = 6;
int temp = (a1 * 1000 + a2 * 100 + a3 * 10 + a4) - n;
if(temp > 800 && temp < 900) {
list8.add(new number(n, temp));
return true;
}
return false;
} public void printResult() {
for(int i = 1000;i <= 9999;i++) {
if(judge(i)) {
judge1(i);
judge2(i);
}
}
for(int i = 0;i < list8.size();i++) {
for(int j = 0;j < list2.size();j++) {
if(list8.get(i).money - list2.get(j).money == 558) {
System.out.println(list8.get(i).num+", "+list2.get(j).num);
break;
}
}
} } public static void main(String[] args) {
Main test = new Main();
test.printResult();
}
}
5 三部排序
标题:三部排序 一般的排序有许多经典算法,如快速排序、希尔排序等。 但实际应用时,经常会或多或少有一些特殊的要求。我们没必要套用那些经典算法,可以根据实际情况建立更好的解法。 比如,对一个整型数组中的数字进行分类排序: 使得负数都靠左端,正数都靠右端,0在中部。注意问题的特点是:负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!! 以下的程序实现了该目标。 static void sort(int[] x)
{
int p = 0;
int left = 0;
int right = x.length-1; while(p<=right){
if(x[p]<0){
int t = x[left];
x[left] = x[p];
x[p] = t;
left++;
p++;
}
else if(x[p]>0){
int t = x[right];
x[right] = x[p];
x[p] = t;
right--;
}
else{
_________________________; //代码填空位置
}
}
} 如果给定数组:
25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0
则排序后为:
-3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25 请分析代码逻辑,并推测划线处的代码,通过网页提交
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!! p++
6 逆波兰表达式
标题:逆波兰表达式 正常的表达式称为中缀表达式,运算符在中间,主要是给人阅读的,机器求解并不方便。 例如:3 + 5 * (2 + 6) - 1 而且,常常需要用括号来改变运算次序。 相反,如果使用逆波兰表达式(前缀表达式)表示,上面的算式则表示为: - + 3 * 5 + 2 6 1 不再需要括号,机器可以用递归的方法很方便地求解。 为了简便,我们假设: 1. 只有 + - * 三种运算符
2. 每个运算数都是一个小于10的非负整数 下面的程序对一个逆波兰表示串进行求值。
其返回值为一个数组:其中第一元素表示求值结果,第二个元素表示它已解析的字符数。 static int[] evaluate(String x)
{
if(x.length()==0) return new int[] {0,0}; char c = x.charAt(0);
if(c>='0' && c<='9') return new int[] {c-'0',1}; int[] v1 = evaluate(x.substring(1));
int[] v2 = __________________________________________; //填空位置 int v = Integer.MAX_VALUE;
if(c=='+') v = v1[0] + v2[0];
if(c=='*') v = v1[0] * v2[0];
if(c=='-') v = v1[0] - v2[0]; return new int[] {v,1+v1[1]+v2[1]};
} 请分析代码逻辑,并推测划线处的代码,通过网页提交。
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!! evaluate(x.substring(v1[1] + 1))
7 错误票据
标题:错误票据 某涉密单位下发了某种票据,并要在年终全部收回。 每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。 因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。 你的任务是通过编程,找出断号的ID和重号的ID。 假设断号不可能发生在最大和最小号。 要求程序首先输入一个整数N(N<100)表示后面数据行数。
接着读入N行数据。
每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000)
每个整数代表一个ID号。 要求程序输出1行,含两个整数m n,用空格分隔。
其中,m表示断号ID,n表示重号ID 例如:
用户输入:
2
5 6 8 11 9
10 12 9 则程序输出:
7 9 再例如:
用户输入:
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119 则程序输出:
105 120 资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 2000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; public class Main { public void printResult(String[] A) {
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0, len = A.length;i < len;i++) {
// char[] arrayA = A[i].toCharArray(); //把每一行字符串处理转换为相应输入数
// for(int j = 0, len1 = arrayA.length;j < len1;j++) {
// int a = 0;
// while(j < len1 && arrayA[j] != ' ') {
// a = a * 10 + arrayA[j] - '0';
// j++;
// }
// if(a != 0)
// list.add(a);
// }
//split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String[] temp = A[i].split(" ");
for(int j = 0;j < temp.length;j++) {
int a = Integer.valueOf(temp[j]);
list.add(a);
}
}
Collections.sort(list); //对list中元素进行从小到大排序
int m = 0, n = 0;
for(int i = 1, len2 = list.size();i < len2;i++) {
if(list.get(i) - list.get(i - 1) == 2) {
m = (list.get(i) + list.get(i - 1)) / 2;
} else if(list.get(i) - list.get(i - 1) == 0) {
n = list.get(i);
}
}
System.out.println(m+" "+n);
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String[] A = new String[n];
for(int i = 0;i < n;i++)
A[i] = in.nextLine();
test.printResult(A);
}
}
8 带分数
标题:带分数 100 可以表示为带分数的形式:100 = 3 + 69258 / 714 还可以表示为:100 = 82 + 3546 / 197 注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。 类似这样的带分数,100 有 11 种表示法。 题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法! 例如:
用户输入:
100
程序输出:
11 再例如:
用户输入:
105
程序输出:
6 资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 3000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
方法1(蛮力枚举):
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; public class Main {
//判断数a,b,c所有位置数是否刚好是1~9组成,如是返回true,否则返回false
public boolean judge(int a, int b, int c) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(a > 0) {
int temp = a % 10;
a = a / 10;
list.add(temp);
}
while(b > 0) {
int temp = b % 10;
b = b / 10;
list.add(temp);
}
while(c > 0) {
int temp = c % 10;
c = c / 10;
list.add(temp);
}
Collections.sort(list);
if(list.size() == 9) {
int i = 0;
for(;i < 9;i++) {
if(list.get(i) == i + 1)
continue;
else
return false;
}
if(i == 9)
return true;
}
return false;
}
//判断数x中是否有重复数字,若有返回false,否则返回false
public boolean judge1(int x) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(x > 0) {
int temp = x % 10;
x = x / 10;
list.add(temp);
}
Collections.sort(list);
for(int i = 1;i < list.size();i++) {
if(list.get(i - 1) == list.get(i))
return false;
}
return true;
} public void printResult(int n) {
int result = 0;
int a, b, c; //n = a + b / c(带分数)
for(a = 1; a < n;a++) {
if(!judge1(a))
continue;
for(c = 1;c < 10000;c++) {
b = (n - a) * c;
if(!judge1(b) || !judge1(c))
continue;
if(judge(a, b, c))
result++;
}
}
System.out.println(result);
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int N = in.nextInt();
test.printResult(N);
}
}
方法2(DFS遍历枚举):
import java.util.Scanner; public class Main { public static int count = 0; public void swap(int[] A, int a, int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
} //全排列
public void dfs(int[] A, int step, int n) {
if(step == A.length) {
check(A, n);
return;
} else {
for(int i = step;i < A.length;i++) {
swap(A, i, step);
dfs(A, step + 1, n);
swap(A, i, step);
}
}
return;
} public void check(int[] A, int n) {
int len = String.valueOf(n).length();
String arrayA = "";
for(int i = 0;i < A.length;i++) {
arrayA += A[i];
}
for(int i = 1;i <= len;i++) {
int a = Integer.valueOf(arrayA.substring(0, i));
if(a > n)
continue;
for(int j = i + (9 - i) / 2;j < 9;j++) {
int b = Integer.valueOf(arrayA.substring(i, j));
int c = Integer.valueOf(arrayA.substring(j, 9));
if(b < c || b % c != 0)
continue;
if(a + b / c == n)
count++;
}
}
return;
} public static void main(String[] args) {
Main test = new Main();
int[] A = {1,2,3,4,5,6,7,8,9};
Scanner in = new Scanner(System.in);
int n = in.nextInt();
test.dfs(A, 0, n);
System.out.println(count);
}
}
9 剪格子
标题:剪格子 如图p1.jpg所示,3 x 3 的格子中填写了一些整数。 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60。 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以分割为两个部分,使得这两个区域的数字和相等。
如果存在多种解答,请输出包含左上角格子的那个区域包含的格子的最小数目。
如果无法分割,则输出 0 程序输入输出格式要求:
程序先读入两个整数 m n 用空格分割 (m,n<10)
表示表格的宽度和高度
接下来是n行,每行m个正整数,用空格分开。每个整数不大于10000
程序输出:在所有解中,包含左上角的分割区可能包含的最小的格子数目。 例如:
用户输入:
3 3
10 1 52
20 30 1
1 2 3 则程序输出:
3 再例如:
用户输入:
4 3
1 1 1 1
1 30 80 2
1 1 1 100 则程序输出:
10 (参见p2.jpg) 资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 5000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
方法1:求全组合,枚举,这样思考简单,不过时间复杂度较高
import java.util.ArrayList;
import java.util.Scanner; public class Main {
public static int min = Integer.MAX_VALUE;
public ArrayList<Integer> list = new ArrayList<Integer>();
//dfs求取数组A的全部组合数
public void dfs(int[] A, int[][] value, int step) {
while(step < A.length) {
list.add(A[step]);
step++;
check(value);
dfs(A, value, step);
list.remove(list.size() - 1);
}
} public void check(int[][] value) {
boolean judge1 = true; //用于判断list中格子是否满足减格子的减法要求
boolean judge2 = false; //用于判断list中是否包含第1个格子
for(int i = 0;i < list.size();i++) { //检查list获取的一组格子是否符合减格子减法要求
if(list.get(i) == 0)
judge2 = true;
boolean judge4 = false; //用于判定格子i是否与list中其它某个格子相邻
for(int j = 0;j < list.size();j++) {
if(j == i)
continue;
int temp = Math.abs(list.get(i) - list.get(j));
if(temp == 1 || temp == value[0].length) {
judge4 = true;
break;
}
}
if(judge4 == false) {
judge1 = false;
break;
}
}
if(judge1 == false) //当list中包含格子不符合减法要求时,直接结束函数
return;
int sum = 0;
for(int i = 0;i < value.length;i++) { //value数组元素总和
for(int j = 0;j < value[0].length;j++)
sum += value[i][j];
}
int listSum = 0;
for(int i = 0;i < list.size();i++) { //list链表中包含value中元素和
int a = list.get(i) / value[0].length;
int b = list.get(i) - a * value[0].length;
listSum += value[a][b];
}
if(listSum * 2 != sum) //当减成的两部分格子数字和不相等时,结束函数
return;
if(judge2 == true) { //当第一个格子包含在list链表中
if(min > list.size())
min = list.size();
} else {
int L = value.length * value[0].length - list.size();
if(min > L)
min = L;
}
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);int m = in.nextInt();
int n = in.nextInt();
int len = m * n;
int[] A = new int[len];
for(int i = 0;i < len;i++)
A[i] = i;
int[][] value = new int[n][m];
while(len > 0) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) {
value[i][j] = in.nextInt();
len--;
}
}
}
test.dfs(A, value, 0);
if(min != Integer.MAX_VALUE)
System.out.println(min);
else {
System.out.println(0);
}
}
}
方法2:使用DFS从(,)顶点开始遍历 import java.util.Scanner; public class Main1 {
public static int[][] move = {{-,},{,},{,-},{,}};//表示分别向上、下、左、右移动一步
public static int min = Integer.MAX_VALUE; //用于存放最终输出结果,初始化为最大
public static int sum = ; //用于存放输入矩阵的所有元素和
//用于判断当前x,y是否在属于矩阵A中元素点
public boolean check(int[][] A, int x, int y) {
if(x < || x > A.length - || y < || y > A[].length - )
return false;
return true;
}
/*
* 参数A:输入矩阵元素
* 参数used:判定当前DFS遍历的点,false代表未被遍历,true代表已被遍历
* 参数x、y:当前遍历到的坐标位置
* 参数tempSum:当前已被遍历的元素和
* 参数size:当前已被遍历的元素个数
*/
public void dfs(int[][] A, boolean[][] used, int x, int y, int tempSum, int size) {
if(used[x][y] == true) {
return;
} else {
used[x][y] = true; //代表已被遍历
tempSum += A[x][y];
size++;
} if(tempSum * == sum) { //判断是否符合减格子要求
if(min > size)
min = size;
return;
} else {
for(int i = ;i < ;i++) {
int tempX = x + move[i][];
int tempY = y + move[i][];
if(check(A, tempX, tempY)) { //当tempX,tempY均在矩阵A中时
dfs(A, used, tempX, tempY, tempSum, size); //进行递归操作
}
}
}
used[x][y] = false; //进行回溯处理
tempSum -= A[x][y];
size--;
return;
} public static void main(String[] args) {
Main1 test = new Main1();
Scanner in = new Scanner(System.in);
int m = in.nextInt(); //矩阵的列
int n = in.nextInt(); //矩阵的行
int[][] A = new int[n][m];
boolean[][] used = new boolean[n][m];
for(int i = ;i < n;i++) {
for(int j = ;j < m;j++) {
int temp = in.nextInt();
sum += temp;
A[i][j] = temp;
used[i][j] = false;
}
}
if(sum % != ) { //当输入矩阵所有元素和为奇数时,直接输出0,退出程序
System.out.println("");
return;
}
test.dfs(A, used, , , , );
if(min != Integer.MAX_VALUE) {
System.out.println(min);
} else {
System.out.println("");
}
}
}
10 大臣的旅费
标题:大臣的旅费 很久以前,T王国空前繁荣。为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市。 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者通过其他大城市间接到达。同时,如果不重复经过大城市,从首都到达每个大城市的方案都是唯一的。 J是T国重要大臣,他巡查于各大城市之间,体察民情。所以,从一个城市马不停蹄地到另一个城市成了J最常做的事情。他有一个钱袋,用于存放往来城市间的路费。 聪明的J发现,如果不在某个城市停下来修整,在连续行进过程中,他所花的路费与他已走过的距离有关,在走第x千米到第x+1千米这一千米中(x是整数),他花费的路费是x+10这么多。也就是说走1千米花费11,走2千米要花费23。 J大臣想知道:他从某一个城市出发,中间不休息,到达另一个城市,所有可能花费的路费中最多是多少呢? 输入格式:
输入的第一行包含一个整数n,表示包括首都在内的T王国的城市数
城市从1开始依次编号,1号城市为首都。
接下来n-1行,描述T国的高速路(T国的高速路一定是n-1条)
每行三个整数Pi, Qi, Di,表示城市Pi和城市Qi之间有一条高速路,长度为Di千米。 输出格式:
输出一个整数,表示大臣J最多花费的路费是多少。 样例输入:
5
1 2 2
1 3 1
2 4 5
2 5 4 样例输出:
135 样例说明:
大臣J从城市4到城市5要花费135的路费。 根据资源限制尽可能考虑支持更大的数据规模。 资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 5000ms 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.6及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
方法1(floyd算法):
import java.util.Scanner; public class Main { public void floyd(int[][] matrix) {
int len = matrix.length;
for(int k = 1;k < len;k++) {
for(int i = 1;i < len;i++) {
for(int j = 1;j < len;j++) {
if(i == j)
continue;
if(matrix[i][k] != -1 && matrix[k][j] != -1) {
if(matrix[i][j] == -1 || matrix[i][j] > matrix[i][k] + matrix[k][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
}
} public void printResult(int[][] matrix) {
floyd(matrix);
int max = 0;
int len = matrix.length;
for(int i = 1;i < len;i++) {
for(int j = 1;j < len;j++) {
if(max < matrix[i][j])
max = matrix[i][j];
}
}
int result = max * 10 + (1 + max) * max / 2;
System.out.println(result);
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] matrix = new int[n + 1][n + 1];
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= n;j++)
matrix[i][j] = -1; //表示不可到达
}
for(int i = 1;i < n;i++) {
int a = in.nextInt();
int b = in.nextInt();
int value = in.nextInt();
matrix[a][b] = value;
matrix[b][a] = value;
}
test.printResult(matrix);
}
}
方法2(spfa算法):
import java.util.ArrayList;
import java.util.Scanner; public class Main { static class point {
public int a; //起点
public int b; //终点
public int value; //距离 point(int a, int b, int value) {
this.a = a;
this.b = b;
this.value = value;
}
} public int spfa(ArrayList<point>[] list, int a) {
int max = 0;
int len = list.length;
int[] result = new int[len];
int[] count = new int[len];
boolean[] used = new boolean[len];
for(int i = 1;i < len;i++) {
result[i] = -1; //表示a城市到不能到i城市
used[i] = false;
}
result[a] = 0;
used[a] = true;
count[a]++;
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(a);
while(list1.size() != 0) {
int start = list1.get(0);
for(int i = 0;i < list[start].size();i++) {
int b = list[start].get(i).b;
if(result[start] != -1) {
if(result[b] == -1 || result[b] > result[start] + list[start].get(i).value) {
result[b] = result[start] + list[start].get(i).value;
if(!used[b]) {
used[b] = true;
list1.add(b);
count[b]++;
if(count[b] > len) //检测回环负路
return 0;
}
}
}
}
used[start] = false;
list1.remove(0);
}
for(int i = 1;i < len;i++) {
if(max < result[i])
max = result[i];
}
return max;
} public void printResult(ArrayList<point>[] list) {
int max = 0;
int len = list.length;
for(int i = 1;i < len;i++) {
int tempMax = spfa(list, i);
if(max < tempMax)
max = tempMax;
}
int result = max * 10 + (1 + max) * max / 2;
System.out.println(result);
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList<point>[] list = new ArrayList[n + 1];
for(int i = 1;i <= n;i++)
list[i] = new ArrayList<point>();
for(int i = 1;i < n;i++) {
int a = in.nextInt();
int b = in.nextInt();
int value = in.nextInt();
list[a].add(new point(a, b, value));
list[b].add(new point(b, a, value)); //输入数据为无向连通图
}
test.printResult(list);
}
}
算法笔记_108:第四届蓝桥杯软件类省赛真题(JAVA软件开发本科A组)试题解答的更多相关文章
- 算法笔记_214:第六届蓝桥杯软件类校赛真题(Java语言A组)
目录 1 题目一 2 题目二 3 题目三 4 题目四 5 题目五 6 题目六 7 题目七 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 题目一 一个串的子串是指该串的一个连续的局部.如果不要求连续 ...
- 算法笔记_110:第四届蓝桥杯软件类省赛真题(JAVA软件开发高职高专组部分习题)试题解答
目录 1 猜年龄 2 组素数 3 第39级台阶 4 核桃的数量 5 打印十字图 6 买不到的数目 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 猜年龄 标题: ...
- 算法笔记_109:第四届蓝桥杯软件类省赛真题(JAVA软件开发本科B组部分习题)试题解答
目录 1 马虎的算式 2 黄金连分数 3 有理数类 4 幸运数 5 连号区间数 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 马虎的算式 标题: 马虎的算式 小明 ...
- 算法笔记_111:第五届蓝桥杯软件类省赛真题(Java本科A组)试题解答
目录 1 猜年龄 2 李白打酒 3 神奇算式 4 写日志 5 锦标赛 6 六角填数 7 绳圈 8 兰顿蚂蚁 9 斐波那契 10 波动数列 前言:以下试题解答代码部分仅供参考,若有不当之处,还请路 ...
- 算法笔记_112:第五届蓝桥杯软件类省赛真题(Java本科B组)试题解答
目录 1 武功秘籍 2 切面条 3 猜字母 4 大衍数列 5 圆周率 6 奇怪的分式 7 扑克序列 8 分糖果 9 地宫取宝 10 矩阵翻硬币 前言:以下试题解答代码部分仅供参考,若有不当之处, ...
- 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)
目录 1 好好学习 2 埃及分数 3 金蝉素数 4 横向打印二叉树 5 危险系数 6 公式求值 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的 ...
- 算法笔记_203:第四届蓝桥杯软件类决赛真题(C语言B组)
目录 1 猜灯谜 2 连续奇数和 3 空白格式化 4 高僧斗法 5 格子刷油漆 6 农场阳光 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 猜灯谜 标题:猜灯谜 A 村的元宵节灯会上有一迷题: ...
- 算法笔记_207:第五届蓝桥杯软件类决赛部分真题(Java语言C组)
目录 1 数字拆分 2 稍大的串 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 数字拆分 正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 6 5+1 4+2 4+1+ ...
- 算法笔记_211:第七届蓝桥杯软件类决赛部分真题(Java语言A组)
目录 1 阶乘位数 2 凑平方数 3 棋子换位 4 机器人塔 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 阶乘位数 阶乘位数 9的阶乘等于:362880 它的二进制表示为:10110001001 ...
随机推荐
- [转载] 使用Kettle进行数据迁移(ETL)
由于开发新的系统,需要将之前一个老的C/S应用的数据按照新的数据设计导入到新库中.此过程可能涉及到表结构不一致.大数据量(千万级,甚至上亿)等情况,包括异构数据的抽取.清洗等等工作.部分复杂的工作需要 ...
- mysql-connector-java升级到6.0以后启动tomcat报错
mysql-connector-java升级到6.0以后启动tomcat报错 java.sql.SQLException: The server time zone value '�й���ʱ��' ...
- Oracle学习中,重点是sql语句的学习,而所有的sql语句都要在scott用户下完成。
熟悉这个用户下的四张表,是必要的. 查看所有表名: SELECT * FROM tab; 查看每张表的结构: DESC 表名称: 1.部门表:dept no 名称 类型 描述 1 DEPTNO NU ...
- oracle直方图
直方图 当某列数据分布不均衡.为了让CBO能生成最佳的运行计划,我们可能须要对表收集直方图,直方图最大的桶数(Bucket)是254. 收集直方图是一个很耗时的过程,如无必要.千万别去收集直方图. O ...
- Mongodb后台daemon方式启动
Mongodb可以通过命令行方式和配置文件的方式来启动,具体命令如下: 命令行: [root@localhost mongodb]# ./bin/mongod --dbpath=/data/db 配置 ...
- JDBC进阶之PreparedStatement执行SQL语句(MySQL)
一.什么是PreparedStatement 参阅Java API文档,我们可以知道,PreparedStatement是Statement的子接口(如图所示),表示预编译的 SQ ...
- tornado基础入门(一)——简单了解tornado
参考:http://demo.pythoner.com/itt2zh/ch1.html tornado是一个轻量级的web框架,是一个用python写的web服务器,它有三个最大的特点(优势)速度.简 ...
- spring事务管理器的源码和理解
原文出处: xieyu_zy 以前说了大多的原理,今天来说下spring的事务管理器的实现过程,顺带源码干货带上. 其实这个文章唯一的就是带着看看代码,但是前提你要懂得动态代理以及字节码增强方面的知识 ...
- 【BZOJ】【3239】Discrete Logging
BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...
- ubuntu/wireshark: There are no interfaces on which a capture can be done.故障解决
[转载]http://blog.csdn.net/ccwwff/article/details/6697258 在ubuntu安装wireshark, 在启动程序启动wireshark. 点captr ...