package exec;
public class Age ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int father = 30;
int son = 6;
for(int i=1;i<100;i++)
...{
if((father+i)==((son+i)*2))...{
System.out.println(i + " 年");
}
}
}
}
package exec;
import java.util.Arrays;
public class Arrary ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int a[] = ...{ 1, 3, 5, 8, 9, 4 };
int b[] = ...{ 2, 4, 7, 6, 5, 9 };
int c[] = new int[12];
for (int i = 0; i < 6; i++) ...{
c[i] = a[i];
c[i + 6] = b[i];
}
Arrays.sort(c);
for (int i = 0; i < 12; i++) ...{
System.out.print(c[i] + " ");
}
}
}
* 然後將反彈起多高? */
package exec;
public class BallFall ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
float heigher = 100;
float sum = 0;
for (int i = 1; i < 10; i++) ...{
heigher /= 2;
sum += heigher;
System.out.println("第" + i + "次" + "總行程" + sum + "米" + "反彈"
+ heigher + "米");
}
}
}
package exec;
public class ChangeCoin ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int num = 0;
for (int i = 1; i <= 20; i++) ...{
for (int j = 1; j <= 50; j++) ...{
for (int k = 1; k <= 100; k++) ...{
if (5 * i + 2 * j + 1 * k == 100) ...{
num += 1;
}
}
}
}
System.out.print(num + "種換法");
}
}
* 每頭小母牛從第四個年頭起,每年年初也要生一頭小母牛。按此規律,若無牛死亡,第20年頭上共有多少頭母牛?*/
package exec;
public class CountCow ...{
int[] cowNumber = new int[21];
public void count(int increaseCycle, int years) ...{
cowNumber[0] = 1;
for (int currentYear = 1; currentYear <= years; currentYear++) ...{
if (currentYear < increaseCycle) ...{
cowNumber[currentYear] = cowNumber[currentYear - 1] + 1;
} else ...{
cowNumber[currentYear] = cowNumber[currentYear - 1]
+ cowNumber[currentYear - increaseCycle + 1];
}
System.out
.println("第" + currentYear + "年" + cowNumber[currentYear]);
}
}
public static void main(String[] args) ...{
int increaseCycle = 4;
int years = 20;
CountCow cow = new CountCow();
cow.count(increaseCycle, years);
}
}
package exec;
public class Divide ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
for (int i = 0; i <= 500; i++) ...{
if ((i % 7 == 0) | (i % 9 == 0)) ...{
System.out.print(i + " ");
}
}
}
}
* 編寫一個Java程序 使它隨即產生1800到2000之間的年份,打印它是否是一個閏年,閏年是1584年以後的年份,它要能被400整除,
* 要能被4整除但是不能被100整除,已知使用Math.random()方法可以產生0到1之間的隨即小數. )
*/
package exec;
public class EmergerYear ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) ...{
long year = (Math.round(Math.random() * 200) + 1800);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ...{
System.out.println(year + "是閏年!");
} else
System.out.println(year + "不是閏年!");
}
}
}
package exec;
public class Number ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
System.out.print(numbers(200));
}
public static int numbers(int n)...{
return n<=1?1:n+numbers(n-1);
}
}
* 並指出它是由鍵盤輸入的第幾個數。
*/
package exec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NumberCompare ...{
public static final int NUMBER = 80; // 給定一個要比較的數
public static void main(String args[]) ...{
System.out.print("Please input numbers: ");
BufferedReader io = new BufferedReader(new InputStreamReader(System.in));
try ...{
// 從鍵盤輸入若干數
String line = io.readLine();
// 放入數組中
String[] items = line.split(" ");
int temp = 5000, x = 0, place = 0;
System.out.println("target number: 80");
// 和給定的數比較大小
for (int i = 0; i < items.length; i++) ...{
if (Integer.parseInt(items[i]) > NUMBER) ...{
x = Integer.parseInt(items[i]) - NUMBER;
} else ...{
x = NUMBER - Integer.parseInt(items[i]);
}
if (x < temp) ...{
temp = x;
place = Integer.parseInt(items[i]);
}
}
System.out.println("place: " + place);
io.close();
System.exit(0);
} catch (IOException e) ...{
e.printStackTrace();
}
}
}
package exec;
import java.util.Iterator;
import java.util.TreeSet;
public class RandomNumber ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
/** *//**方法1
long max = 50;
long min = 50;
for(int i=0; i<10; i++){
long number = Math.round(Math.random()*99 + 1);
System.out.print(number + " ");
max = Math.max(max, number);
min = Math.min(min, number);
if(i == 9){
System.out.print(" Math MaxNumber=" + max + " MinNumber=" + min);
}
}
*/
//方法2
TreeSet ts = new TreeSet();
for(int i=0; i<10; i++)...{
long number = Math.round(Math.random()*99 + 1);
System.out.print(number + " ");
ts.add(number);
}
System.out.println();
Iterator it = ts.iterator();
while(it.hasNext())...{
System.out.print(it.next() + " ");
}
System.out.print(" Math MaxNumber=" + ts.last() + " MinNumber=" + ts.first());
}
}
package exec;
public class SteelSection ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int i = 0;
float steel = 2000f;
while (steel >= 5) ...{
i++;
steel = steel / 2;
System.out.println(i + "天后鋼管長" + steel + "米");
}
}
}
* 編寫一個程序求出2到10000之間的所有完全數.*/
package exec;
public class WanNumber ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int m = 0;
for (int n = 2; n <= 10000; n++) ...{
m = 0;
for (int i = 1; i < n; i++) ...{
if (n % i == 0) ...{
m += i;
if (m == n) ...{
System.out.println(n + "個完全數");
}
}
}
}
}
}
* 如果是返回true 否則返回false 已知Babbage函數發f(x)=x*x+x+41,其中x爲自然數,
* 當x小於特定的n之間所有的函數均爲素數,編寫一個測試類,其中的main方法找出這個特定n值.
*/
package exec;
public class Test ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int num = 200000; // 表示1-num之間的自然數
for (int i = 1; i <= num; i++) ...{
if (Prime.me(i)) ...{
System.out.println(i + "是素數");
} else ...{
System.out.println(i + "不是素數");
}
}
}
}
package exec;
public class Prime ...{
public static boolean me(int n) ...{
for (int x = 1; x < n; x++) ...{
if (n == x * x + x + 41) ...{
return true;
}
}
return false;
}
}