package demo;
public class Demo01 {
/**
* 1.有一對兔子,從出生後第3個月起每個月生一對兔子,小兔子長到第三個月後每個月又生一對兔子
,假如兔子都不死,問每個月的兔子對數爲多少? (輸入月數,輸出兔子的對數)
提示:兔子的規律1,1,2,3,5,8,13,21,34...
斐波拉契數列
f(n) = f(n-1) + f(n-2)
*/
public static void main(String[] args) {
int num = fn(8+1);
System.out.println(num);
}
public static int fn(int n){
if(n==1){
return 1;
}
if(n==2){
return 1;
}
return fn(n-1) + fn(n-2);
}
}
package task;
//有一對兔子,從出生後第3個月起每個月生一對兔子,小兔子長到第三個月後每個月又生一對兔子
//,假如兔子都不死,問每個月的兔子對數爲多少? (輸入月數,輸出兔子的對數)
//提示:兔子的規律1,1,2,3,5,8,13,21,...
import java.util.Scanner;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("請輸入你想要查看的月數:");
int month = scan.nextInt(); //輸入的月份month
for(int i = 1;i <= month;i++){
System.out.println("第"+i+"個月的兔子數量:"+number(i));
}
}
public static int number(int x){ //返回兔子的數目
if(x == 1||x == 2)
return 1;
else
return(number(x - 1) + number(x - 2)); //兔子的規律:1,1,2,3,5,8,13,21.....
}
}
package demo;
public class Demo02 {
/**
* 2.求1~1000所有素數之和
* 什麼是素數
* 能被1和自身整除的數,但1不是素數
*/
public static void main(String[] args) {
//測試
//System.out.println(isSu(2));
//1,2,3,4,5,6,7,8,9,10
//2+3+5+7 = 17
int sum = 0;//總和
for (int i = 1; i <=1000; i++) {
if(isSu(i)){
sum += i;
}
}
System.out.println(sum);
}
//是否是素數
public static boolean isSu(int num){
if(num<=1){
return false;
}
for (int i = 2; i < num; i++) {
if(num%i==0){
//不是素數
return false;
}
}
System.out.println(num);
return true;
}
}
package task;
//求1~1000所有素數之和
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0, sum = 0; //計數器count ,和sum
for(int i = 2;i <= 1000;i++)
{
boolean flag = false; //建立一個標籤
for (int j = 2;j <= i;j++)
{
if(i % j == 0 && j != i){ //判斷是否爲素數
flag = true; //更改標籤(非素數)
break;
}
}
if(!flag){ //對標籤爲true的素數求和
System.out.println(i);
sum += i;
count ++;
}
}
System.out.println("1-1000之間的素數一共有"+count+"個,其和爲"+sum);
}
}
package demo;
public class Demo03 {
/**
* 3.打印所有"水仙花數",所謂“水仙花數”是指一個三位數,其各位數字的立方等於該數本身。例如
153是一個“水仙花數”,因爲153 = 1^3 +5^3+3^3
*/
public static void main(String[] args) {
//System.out.println(isOK(153));
for (int i = 100; i <1000; i++) {
if(isOK(i)){
System.out.println(i);
}
}
}
//判斷一個數是否是水仙花數
public static boolean isOK(int num){
int ge = num%10;
int shi = num/10%10;
int bai = num/100;
return num == ge*ge*ge + shi*shi*shi + bai*bai*bai;
}
}
package task;
//打印所有"水仙花數",所謂“水仙花數”是指一個三位數,其各位數字的立方等於該數本身。例如
//153是一個“水仙花數”,因爲153 = 1^3 +5^3+3^3
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("所有的水仙花數:");
for(int i = 100; i <= 999; i++)
{
int single = i % 10; //個位數
int tens = i / 10 % 10; //十位數
int hundreds = i / 100 % 10; //百位數
int sum = (int)(Math.pow(single,3) + Math.pow(tens,3) + Math.pow(hundreds,3)); //立方和
if(sum == i){
System.out.println(i);
}
}
}
}
package demo;
public class Demo04 {
/**
* 4.一球從100米高度自由落下,每次落地後反彈到原來高度的一半,
* 再落下,求它在第20次落地時,共經過多少米
*
* 1 2 3 4 ..
* 100 200 250 275
* 100 50 25 12.5
*/
public static void main(String[] args) {
System.out.println(getNum(10));
}
public static float getNum(int count){
if(count==1){
return 100f;
}
if(count==2){
return 200f;
}
return getNum(count-1) +(getNum(count-1)-getNum(count-2))/2;
}
}
package task;
//一球從100米高度自由落下,每次落地後反彈到原來高度的一半,再落下,求它在第20次落地時,
//共經過多少米
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double height=100; //設立初始高度height
double sum=100; //小球第1次落地時已經過的高度
for(int i=2;i<=20;i++)
{
height=height/2; //計算高度
sum=sum+height*2; //小球第i次落地時共經過的高度
}
System.out.println("第二十次小球共經過:"+sum+"米");
}
}
package demo;
import java.util.ArrayList;
import java.util.List;
public class Demo05 {
/**
* 5.輸出1,2,3,4四個數字所產生的不重複的所有3位數
* 123 132 213 231 312 321
* 134
* 234
* 124
*/
public static void main(String[] args) {
int[] arr = {1,2,3,4};
List<Integer> list = new ArrayList<Integer>();
int i,j,k;
for ( i = 0; i < arr.length; i++) {//i選第一個數
for (j = 0; j < arr.length; j++) {//j選第二個數
if(arr[i]==arr[j]){
continue;
}
for (k = 0; k < arr.length; k++) {//k選第三個數
if(arr[i]==arr[k] || arr[j]==arr[k]){
continue;
}
int num = arr[i]*100 + arr[j]*10 + arr[k];
list.add(num);
}
}
}
System.out.println(list.size());//24
//打印所有生成的數
for (int n = 0; n < list.size(); n++) {
System.out.println(list.get(n));
}
}
}
package demo;
import java.util.ArrayList;
import java.util.List;
public class Demo06 {
/**
* 5.輸出1,2,3,4四個數字所產生的不重複的所有3位數
* 123 132 213 231 312 321
* 134
* 234
* 124
*/
public static void main(String[] args) {
int[] arr = {1,2,3,4};
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {//i第一個數
for (int j = 0; j < arr.length; j++) {//第二個數
if(arr[j]==arr[i]){//第二個數與第一個數重複
//忽略這輪循環,再次選第二個數
continue;
}
//第二個數OK,選第三個數
for (int k= 0; k < arr.length;k++) {
//第三個數不能與第一個和第二個數重複
if(arr[k]==arr[i] || arr[k] ==arr[j]){
continue;//忽略這輪,重新選
}
//三個數都選好了
int num = arr[i]*100 + arr[j]*10 +arr[k];
list.add(num);
}
}
}
System.out.println("一共生成"+list.size()+"個數");
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
}
}
package task;
//輸出1,2,3,4四個數字所產生的不重複的所有3位數
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,j,k;
for(i = 1; i <= 4; i++)
{
for(j = 1; j<=4; j++)
{
for(k = 1; k <= 4; k++)
{
if(i != j && j != k && k != i) //3個數互不相同
System.out.printf("%d%d%d\n",i,j,k);
}
}
}
}
}
package demo;
import java.util.Scanner;
public class Demo07 {
/**
*
6.輸入某年某月某日,判斷這一天是這一年的第幾天
*/
public static void main(String[] args) {
//System.out.println(isRunYear(2018));
Scanner sc = new Scanner(System.in);
System.out.print("請輸入年份:");
int year = sc.nextInt();
if(year<0){
System.out.println("請輸入正確得年份");
return;
}
System.out.print("請輸入月份:");
int mon = sc.nextInt();
if(mon<=0 || mon>12){
System.out.println("請輸入正確得月份");
return;
}
System.out.print("請輸入日期:");
int date = sc.nextInt();
//大月
if(date<=0 || date>31){
System.out.println("請輸入正確得日期");
return;
}
//小月 4 6 9 11
if(mon==4 || mon==6 || mon==9 || mon==11){
if(date>30){
System.out.println("請輸入正確得日期");
return;
}
}
if(mon==2 && isRunYear(year)){
//2月份並且是閏年
if(date>29){
System.out.println("請輸入正確得月份");
}
return;
}
if(mon==2 && !isRunYear(year)){
//2月份並且是平年
if(date>28){
System.out.println("請輸入正確得月份");
}
return;
}
//判斷是否是閏年從而確定二月得天數
int days;
if(isRunYear(year)){
days = 29;
}else{
days = 28;
}
int totalDays = 0;
switch(mon){
case 12: totalDays += 30;//11月天數
case 11:totalDays += 31;//10月天數
case 10:totalDays += 30;//9月天數
case 9:totalDays += 31;//8月天數
case 8:totalDays += 31;//7月天數
case 7:totalDays +=30;//6月天數
case 6:totalDays +=31;//5月天數
case 5:totalDays += 30;//4月天數
case 4:totalDays += 31;//3月天數
case 3:totalDays += days;//2月天數
case 2:totalDays +=31;//1月天數
case 1:totalDays += date;//日期數
}
System.out.println(totalDays);
}
public static boolean isRunYear(int year){
if((year%4==0 && year%100!=0)|| year%400==0){
return true;
}
return false;
}
}
package task;
import java.util.Scanner;
//輸入某年某月某日,判斷這一天是這一年的第幾天
public class Test6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("請輸入某年某月某日:");
Scanner scan = new Scanner(System.in);
System.out.println("年:");
int year = scan.nextInt();
System.out.println("月:");
int month = scan.nextInt();
System.out.println("日:");
int day = scan.nextInt();
int nonleapyear[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //非閏年
int leapyear[] = {31,29,31,30,31,30,31,31,30,31,30,31}; //閏年
int mon = 0;
int tian = 0;
//判斷輸入是否可取
if(month > 12 || month < 1 || day > 31 || day <1 ){
System.out.println("輸入有誤,無法查詢!");
}
//閏年(不能整除100,能整4或400)
else if(year % 100 != 0 && year %4 == 0 || year %400 == 0){
if(month < 2){
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+day+"天。");
}
else{
for(int i = 2;i <= month;i++){
mon += leapyear[i-2]; //前面的(month-1)個月的天數之和
}
tian = mon + day; //計算天數
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
}
//非閏年
else{
if(month < 2){
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
else{
for(int i = 2;i <= month;i++){
mon += nonleapyear[i-2]; //前面的(month-1)個月的天數之和
}
tian = mon + day; //計算天數
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+tian+"天。");
}
}
}
}