public class mission1 {
// 偉大的馬克思的手稿中有這樣一道有趣的數學題:
// 有30個人,其中可能有男人,女人,小孩。他們在一家飯館中喫飯,共花費50先令。
// 如果每個男人喫飯要花3先令,每個女人要花2先令,每個小孩要花1先令,
// 問男人,女人,小孩各多少人?(有幾種可能性)
public static void main(String[] args) {
int nman = 0;
int nwoman = 0;
int nboy = 0;
int count = 0;
System.out.println("男人\t" + "女人\t" + "小孩");
// for (nman = 0; nman<17; nman++) {
//
// for(nwoman = 0; nwoman<=25; nwoman++){
//
// for(nboy = 0; nboy<=50; nboy++){
//
// if((50 == (nman*3 + nwoman*2 + nboy*1))&&(30 == nman + nwoman + nboy)){
// System.out.println(nman + "\t" + nwoman + "\t" + nboy);
// count++;
// }
// }
// }
// }
for(; nman <= 10; nman++){
//nman + nwoman + nboy = 30
//nman*3 + nwoman*2 + nboy*1 = 50
nwoman = 20 - 2 * nman;
nboy = 30 - nman - nwoman;
if(50 == (nman*3 + nwoman*2 + nboy*1)){
System.out.println(nman + "\t" + nwoman + "\t" + nboy);
count++;
}
}
System.out.println("總共有" + count + "種。");
}
}
================================================================================================
// 1. 兩數交換程序 (兩種方法)
//
// 輸出互換前手中的紙牌:
// 左手中的紙牌: 10
// 右手中的紙牌: 8
//
// 輸出互換後手中的紙牌:
// 左手中的紙牌: 8
// 右手中的紙牌: 10
// 提示: 可以設置一箇中間數
public class mission1 {
public static void main(String[] args) {
int a = 10;
int b = 8;
System.out.println("\t交換前:a = " + a + "\tb = " + b);
method1 m1 = new method1(a, b);
method2 m2 = new method2(a, b);
method3 m3 = new method3(a, b);
}
}
class method1 {
int temp;
public method1(int a, int b) {
temp = a;
a = b;
b = temp;
System.out.println("method1 交換後:a = " + a + "\tb = " + b);
}
}
class method2 {
public method2(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("method2 交換後:a = " + a + "\tb = " + b);
}
}
class method3 {
public method3(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
System.out.println("method3 交換後:a = " + a + "\tb = " + b);
}
}
================================================================================================
import java.util.Scanner;
public class mission2 {
// 求2+22+222+2222+22222+.....n次 的值
// 提示:
// System.out.println("請輸入統計的次數:");
// int n = input.nextInt();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer num = sc.nextInt();
int factor = 0;
int sum = 0;
int count = 0;
// for (int j = num; j >=1; j--) {
//
// for(int i = j - 1; i >= 0; i--){
//
// sum += factor * Math.pow(10, i);
//
// }
// count++;
// }
for (int i = 0; i < num; i++) {
factor = factor * 10 + 2;
sum += factor;
count++;
}
// System.out.println(factor);
System.out.println("統計" + count + "次。");
System.out.println("值爲: " + sum);
}
}
================================================================================================
public class Daffodil {
// 打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,
// 其各位數字立方和等於該數本身。例如:153是一個 "水仙花數 ",
// 因爲153=1的三次方+5的三次方+3的三次方
public static void main(String[] args) {
Daffodil da = new Daffodil();
for(int i = 100; i<=999; i++){
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if(da.isDaffodil(i)){
System.out.println(i + "=" + a + "的三次方 + " +
b + "的三次方 + " + c + "的三次方");
}
}
}
public boolean isDaffodil(int i){
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (i == (a*a*a + b*b*b + c*c*c)) {
return true;
} else{
return false;
}
}
}
================================================================================================
//求1+2!+3!+...+20!的和
public class mission2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += multi(i);
}
System.out.println(sum);
}
public static int multi(int num){ //計算階乘
if(num == 1 || num == 0){
return 1;
} else{
return (num * multi(--num));
}
}
}