您的位置:首页 > 编程语言 > Java开发

算法竞赛入门经典(第2版)第二章习题(Java)

2018-01-27 18:02 489 查看

算法竞赛入门经典(第2版)第二章习题(Java)

算法竞赛入门经典第2版第二章习题Java

习题 2-1 水仙花数daffodil

习题 2-2 韩信点兵hanxin

习题 2-3 倒三角形triangle

习题 2-4 子序列的和subsequence

习题 2-5 分数化小数decimal

习题 2-6 排列permutation

要注意题目中给出的变量值得范围,选择合适的变量类型。

要计算只包含加法减法的整数表达式除以正整数n的余数,可以在每步计算之后对n取余,结果不变。(防止程序进行中时数据过大溢出)

取整数的某个位置的数先除以该位再模于10即可。如:取1234的百位的数1234/100%10.

引用的包代码中并没有写,在eclipse下按Shift+Ctrl+o即可导入包

习题 2-1 水仙花数(daffodil)

public static void daffodil() {
for (int i = 100; i < 1000; i++) {
int b = i / 100 % 10;
int s = i / 10 % 10;
int g = i / 1 % 10;
if (i == (b * b * b + s * s * s + g * g * g)) {
System.out.println(i);
}
}
}


习题 2-2 韩信点兵(hanxin)

public static void hanxin() {
Scanner in = new Scanner(System.in);
//用来存放结果,当输入结束,输出结果
String str = "";
//记录是第几个输入的结果
int n = 1;
w: while (true) {
String p = in.nextLine();
//如果输入为空行,输入结束
if (p.equals(""))
break;
String[] ls = p.split(" ");
int a = Integer.parseInt(ls[0]);
int b = Integer.parseInt(ls[1]);
int c = Integer.parseInt(ls[2]);
for (int j = 10; j <= 100; j++) {
//判断符合条件将结果添加到结果字符串中,并跳转到外层循环,重新输入,否则会将“No answer”添加到结果字符串。
if (j % 3 == a && j % 5 == b && j % 7 == c) {
str = str + "Case " + (n++) + ": " + j + "\n";
continue w;
}
}
str = str + "Case " + (n++) + ": No answer\n";
}
System.out.println(str);
}


习题 2-3 倒三角形(triangle)

public static void triangle() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
//最外层循环负责换行
for (int i = 0; i < n; i++) {
//打印每行前面的空格
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
//打印“#”
for (int j = 0; j < (2 * (n - i) - 1); j++) {
System.out.print("#");
}
System.out.println();
}
}


习题 2-4 子序列的和(subsequence)

public static void subsequence() {
Scanner in = new Scanner(System.in);
//规定数据格式
DecimalFormat df = new DecimalFormat("0.#####");
//记录是第几次数据的结果
int ci = 1;
//存放结果
String jg = "";
while (true) {
String str = in.nextLine();
if (str.equals("0 0"))
break;
int n = Integer.parseInt(str.split(" ")[0]);
int m = Integer.parseInt(str.split(" ")[1]);
double sum = 0;
for (int i = n; i <= m; i++) {
sum = sum + (double) 1 / i / i;
}
jg = jg + "Case " + ci++ + ": " + df.format(sum) + "\n";
}
System.out.println(jg);
}


习题 2-5 分数化小数(decimal)

public static void decimal() {
Scanner in = new Scanner(System.in);
DecimalFormat df;
String jg = "";
//记录第几次输入的结果
int ci = 1;
while (true) {
String str = in.nextLine();
if (str.equals("0 0 0"))
break;
int a = Integer.parseInt(str.split(" ")[0]);
int b = Integer.parseInt(str.split(" ")[1]);
int c = Integer.parseInt(str.split(" ")[2]);
//根据c得到结果数据格式。
String wei = "0.";
for (int i = 0; i < c; i++) {
wei += "0";
}
df = new DecimalFormat(wei);
double chu = (double) a / b;
jg = jg + "Case " + ci++ + ": " + df.format(chu) + "\n";
}
System.out.println(jg);
}


习题 2-6 排列(permutation)

public static void permutation() {
//因为abc:def:ghi=1:2:3,所以只要暴力破解列出abc即可
//因为ghi<1000,所以a的范围为1-3
for (int i = 1; i <= 1000 / 3 / 100; i++) {
for (int j = 1; j <= 9; j++) {
if (j == i)
continue;
w: for (int k = 1; k <= 9; k++) {
if (j == k || j == i)
continue;
//如果i、j、k不同得到abc,def,ghi
int abc = i * 100 + j * 10 + k;
int def = abc * 2;
int ghi = abc * 3;
//将abc,def,ghi每位放入数组
int[] sz = { i, j, k, def / 100 % 10, def / 10 % 10, def % 10, ghi / 100 % 10, ghi / 10 % 10,
ghi % 10 };
//遍历数组中的数,是否与其他数不同。如果相同调到w:层循环,均不同则输出结果。
for (int n = 0; n < 9; n++) {
for (int m = n + 1; m < 9; m++) {
if (sz
== sz[m])
continue w;
}
}
System.out.println(abc + " " + def + " " + ghi);

}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 Java 习题 答案