1.打印1-1000的素数
题目很简单:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,
问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。
public class HomeWork {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
int c = 1;
for (int a = 2; a < i; a++) {
if (i % a == 0) {
c++;
break;
}
}
if (c == 1) {
System.out.println(i);
}
}
}
}
2.打印水仙花数
public class HomeWork2 {
public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100 % 10;
if (a * a * a + b * b * b + c * c * c == i) {
System.out.println(i);
}
}
}
}
3.打印99乘法表
public class HomeWork {
public static void main(String[] args) {
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();
}
}
}
4. 百钱买百鸡的问题算是一套非常经典的不定方程的问题,
题目很简单:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,
问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。
public class HomeWork { public static void main(String[] args) { //设公鸡只数为a,100最多能买20只 for (int a = 1; a <= 20; a++) { //设母鸡只数为b,100最多买33只 for (int b = 1; b <= 33; b++) { //设小鸡只数为c,100最多买300只,3只一文钱,基数3增量3 for (int c = 3; c <= 300; c+=3) { if (a + b + c == 100 && a * 5 + b * 3 + c / 3 == 100) { System.out.println("公鸡:"+a+"只,母鸡:"+b+"只,小鸡:"+c+"只"); } } } } } }