1.
/*
* 题目:打印出如下图案(菱形)
*/
// *
// ***
// *****
// *******
// *****
// ***
// *
/*
* 序列为 1,3,5,7,5,3,1
*/
public static void print22() {
int start = 1;
int end = 7;
int count = 2;
while (start > 0) {
for (int i = 0; i < (end - start) / 2; i++) {
System.out.print(" ");
}
for (int i = 0; i < start; i++) {
System.out.print("*");
}
start += count;
if (start >= end) {
count = -count;
}
System.out.println();
}
}
输出:
*
***
*****
*******
*****
***
*
2.
/*
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 (这里不用递归求)
*/
public static void print23() {
int count = 20;
double total = 0;
int pre = 1;
int now = 2;
for (int i = 0; i < count; i++) {
System.out.print(now + "/" + pre);
if (i < (count - 1)) {
System.out.print(" + ");
}
total += now * 1.0 / pre;
int temp = pre + now;
pre = now;
now = temp;
}
System.out.println("=" + total);
}
public static void main(String[] args) {
T2.print23();
}
输出:
2/1 + 3/2 + 5/3 + 8/5 + 13/8 + 21/13 + 34/21 + 55/34 + 89/55 + 144/89 + 233/144 + 377/233 + 610/377 + 987/610 + 1597/987 + 2584/1597 + 4181/2584 + 6765/4181 + 10946/6765 + 17711/10946=32.66026079864164
3.
/*
* 题目:求1+2!+3!+...+20!的和
*/
public static void print24() {
int count = 20;
long total = 0;
String str = "";
for (int i = 1; i <= count; i++) {
long temp = 1;
for (int j = 1; j <= i; j++) {
temp *= j;
}
System.out.println(i + "!=" + temp);
str += i + "!";
if (i < count) {
str += " + ";
} else {
str += " = ";
}
total += temp;
}
System.out.println(str + total);
}
/*
* 题目:求1+2!+3!+...+20!的和
*/
public static void print25() {
int count = 20;
long total = 0;
long temp = 1;
String str = "";
for (int i = 1; i <= count; i++) {
temp *= i;
total += temp;
System.out.println(i + "!=" + temp);
str += i + "!";
if (i < count) {
str += " + ";
} else {
str += " = ";
}
}
System.out.println(str + total);
}
public static void main(String[] args) {
T2.print24();
T2.print25();
}
输出:
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
17!=355687428096000
18!=6402373705728000
19!=121645100408832000
20!=2432902008176640000
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! + 20! = 2561327494111820313
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
17!=355687428096000
18!=6402373705728000
19!=121645100408832000
20!=2432902008176640000
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! + 20! = 2561327494111820313
4.
/*
* 题目:利用递归方法求5!。
*/
public static int print26(int n) {
if (n == 1) {
return 1;
} else {
return print26(n - 1) * n;
}
}
public static void main(String[] args) {
System.out.println(T2.print26(5));
}
输出:
120
5.
/*
* 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,
* 说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?(也可以用递归实现)
*/
public static void print27() {
int count = 5;
int start = 10;
for (int i = 1; i < count; i++) {
start += 2;
}
System.out.println(start);
}
public static void main(String[] args) {
T2.print27();
}
输出:
18
6.
/*
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
*/
public static void print28(int n) {
int length = 0;
if (n >= 10000) {
length = 5;
} else if (n >= 1000) {
length = 4;
} else if (n >= 100) {
length = 3;
} else if (n >= 10) {
length = 2;
} else {
length = 1;
}
System.out.print(n + "是" + length + "位数,逆序输出为:");
String temp = "";
for (int i = 0; i < length; i++) {
temp += n % 10;
n = n / 10;
}
System.out.println(Integer.valueOf(temp));
}
/*
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
*/
public static void print29(int n) {
String str = String.valueOf(n);
System.out.print(n + "是" + str.length() + "位数,逆序输出为:");
String temp = "";
for (int i = str.length() - 1; i >= 0; i--) {
temp+=str.charAt(i);
}
System.out.println(Integer.valueOf(temp));
}
public static void main(String[] args) {
T2.print28(10100);
T2.print29(12345);
}
输出:
10100是5位数,逆序输出为:101
12345是5位数,逆序输出为:54321
7.
/*
* 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
*/
public static void print30(int num) {
int n = num;
if (n >= 10000 && n < 100000) {
if (n / 10000 == n % 10) {
n = n / 10 % 1000;
if (n / 100 == n % 10) {
System.out.println(num + "是回文数");
return;
}
}
System.out.println(num + "不是回文数");
}
}
/*
* 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 (任意一个数都可以判断)
*/
public static void print31(int n) {
String str = String.valueOf(n);
if (new StringBuffer(str).reverse().toString().equals(str)) {
System.out.println(n + "是回文数");
return;
}
System.out.println(n + "不是回文数");
}
public static void main(String[] args) {
T2.print30(10100);
T2.print30(12345);
T2.print30(12321);
T2.print30(15851);
T2.print30(15861);
System.out.println("--------------------");
T2.print31(10100);
T2.print31(12345);
T2.print31(12321);
T2.print31(15851);
T2.print31(15861);
}
输出:
10100不是回文数
12345不是回文数
12321是回文数
15851是回文数
15861不是回文数
--------------------
10100不是回文数
12345不是回文数
12321是回文数
15851是回文数
15861不是回文数
8.
/*
* 使用Java语言编写代码,将一个正整数分解质因数,例如:输入90,打印输出90=2*3*3*5
*/
public static void print43(int n) {
System.out.print(n);
if (n > 0) {
String str = "";
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
str += i;
n = n / i;
i--;
if (n != 1) {
str += "*";
}
}
}
System.out.println("=" + str);
}
}
public static void main(String[] args) {
T2.print43(90);
T2.print43(9);
T2.print43(7);
T2.print43(100);
}
输出:
90=2*3*3*5
9=3*3
7=7
100=2*2*5*5
9.
/*
* 求出所有“水仙花数”,水仙花数是指一个 3位数,它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
*/
public static void print44() {
for (int i = 100; i < 1000; i++) {
int n = i;
int total = 0;
while (n > 0) {
int k = n % 10;
total += k * k * k;
n = n / 10;
}
if (total == i) {
System.out.println(i);
}
}
}
public static void main(String[] args) {
T2.print44();
}
输出:
153
370
371
407