1.
/*
* 题目:输入某年某月某日,判断这一天是这一年的第几天?
*/
public static void print14(int year, int month, int day) {
long start = System.currentTimeMillis();
int[][] monthDays = { { 1, 31 }, { 2, 59 }, { 3, 90 }, { 4, 120 }, { 5, 151 }, { 6, 181 }, { 7, 212 }, { 8, 243 }, { 9, 273 }, { 10, 304 },
{ 11, 334 } };
int daysOfYear = day;
if (month > 1) {
daysOfYear += monthDays[month - 2][1];
}
if (month > 2 && isLeapYear(year)) {
daysOfYear++;
}
long end = System.currentTimeMillis();
// 比较
long start2 = System.currentTimeMillis();
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day);
int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
long end2 = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(format.format(c.getTime()) + ":" + daysOfYear + ",用时" + (end - start) + "," + dayOfYear + ",用时" + (end2 - start2));
}
/*
* 是否是闰年
*/
private static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
} else {
return true;
}
}
return false;
}
public static void main(String[] args) {
T2.print14(1900, 1, 13);
T2.print14(1900, 2, 13);
T2.print14(1900, 3, 13);
T2.print14(2000, 2, 13);
T2.print14(2000, 12, 13);
T2.print14(2012, 2, 13);
T2.print14(2012, 3, 13);
T2.print14(2013, 2, 13);
T2.print14(2013, 3, 13);
}
输出:
1900年01月13日:13,用时0,13,用时24
1900年02月13日:44,用时0,44,用时0
1900年03月13日:72,用时0,72,用时0
2000年02月13日:44,用时0,44,用时0
2000年12月13日:348,用时0,348,用时0
2012年02月13日:44,用时0,44,用时0
2012年03月13日:73,用时0,73,用时0
2013年02月13日:44,用时0,44,用时0
2013年03月13日:72,用时0,72,用时0
2.
/*
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
*/
public static void print15(int x, int y, int z) {
int flag = 0;
long start = System.currentTimeMillis();
if (x > y) {
if (x > z) {
if (y > z) {
int temp = x;
x = z;
z = temp;
flag = 1;
} else {
int temp = x;
x = y;
y = z;
z = temp;
flag = 2;
}
} else {
int temp = x;
x = y;
y = temp;
flag = 3;
}
} else {
if (x > z) {
int temp = x;
x = z;
z = y;
y = temp;
flag = 4;
} else {
if (y > z) {
int temp = y;
y = z;
z = temp;
flag = 5;
} else {
flag = 6;
}
}
}
long end = System.currentTimeMillis();
System.out.println("[" + x + "," + y + "," + z + "]标志:" + flag + ",用时:" + (end - start));
}
/*
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
*/
public static void print16(int x, int y, int z) {
int flag = 0;
long start = System.currentTimeMillis();
if (x > y) {
int temp = x;
x = y;
y = temp;
flag++;
}
if (x > z) {
int temp = x;
x = z;
z = temp;
flag++;
}
if (y > z) {
int temp = y;
y = z;
z = temp;
flag++;
}
long end = System.currentTimeMillis();
System.out.println("[" + x + "," + y + "," + z + "]标志:" + flag + ",用时:" + (end - start));
}
public static void main(String[] args) {
T2.print15(1, 2, 3);
T2.print15(1, 3, 2);
T2.print15(2, 1, 3);
T2.print15(2, 3, 1);
T2.print15(3, 1, 2);
T2.print15(3, 2, 1);
System.out.println();
T2.print16(1, 2, 3);
T2.print16(1, 3, 2);
T2.print16(2, 1, 3);
T2.print16(2, 3, 1);
T2.print16(3, 1, 2);
T2.print16(3, 2, 1);
}
输出:
[1,2,3]标志:6,用时:0
[1,2,3]标志:5,用时:0
[1,2,3]标志:3,用时:0
[1,2,3]标志:4,用时:0
[1,2,3]标志:2,用时:0
[1,2,3]标志:1,用时:0
[1,2,3]标志:0,用时:0
[1,2,3]标志:1,用时:0
[1,2,3]标志:1,用时:0
[1,2,3]标志:2,用时:0
[1,2,3]标志:2,用时:0
[1,2,3]标志:3,用时:0
3.
/*
* 题目:输出9*9口诀。
*/
public static void print17() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "×" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
T2.print17();
}
输出:
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
4.
/*
* 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
* 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
* 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
*/
public static void print18() {
int num = 10;
System.out.println(getNumber(num));
}
private static int getNumber(int n) {
if (n == 1) {
return 1;
} else {
return (getNumber(n - 1) + 1) * 2;
}
}
/*
* 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
* 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
* 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
*/
public static void print19() {
int num = 10;
int count = 1;
num--;
while (num > 0) {
count = (count + 1) * 2;
num--;
}
System.out.println(count);
}
public static void main(String[] args) {
T2.print18();
T2.print19();
}
输出:
1534
1534
86万+

被折叠的 条评论
为什么被折叠?



