這是斐波那契數(shù)列的問題
騰沖網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,騰沖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為騰沖上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的騰沖做網(wǎng)站的公司定做!
可以用遞歸,也可以用循環(huán)
遞歸:
public class Demo3 {
// 使用遞歸方法
private static int getFibo(int i) {
if (i == 1 || i == 2)
return 1;
else
return getFibo(i - 1) + getFibo(i - 2);
}
public static void main(String[] args) {
System.out.println("斐波那契數(shù)列的前20項(xiàng)為:");
for (int j = 1; j = 20; j++) {
System.out.print(getFibo(j) + "\t");
if (j % 5 == 0)
System.out.println();
}
}
}
循環(huán):
public class Demo2 {
// 定義數(shù)組方法
public static void main(String[] args) {
int arr[] = new int[20];
arr[0] = arr[1] = 1;
for (int i = 2; i arr.length; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
System.out.println("斐波那契數(shù)列的前20項(xiàng)如下所示:");
for (int i = 0; i arr.length; i++) {
if (i % 5 == 0)
System.out.println();
System.out.print(arr[i] + "\t");
}
}
}
一只雞是一個(gè)頭兩只腳
一只兔是一個(gè)頭四只腳
我的算法就是用for循環(huán)最多進(jìn)行35(頭的數(shù)量)次循環(huán),每循環(huán)一次減去一個(gè)頭只四腳,相當(dāng)于抓一只免子出來,當(dāng)籠子里頭的數(shù)量乘以2剛好等于腳的數(shù)量的時(shí)候,就說明籠子里全是雞了,這樣就可以知道各自的數(shù)量了,以下是核心代碼:
int?head?=?35,?foot?=?94;
int?countChicken,?int?countRabbit;
for(int?i?=?0;?i??35;?i++){
if(head?*?2?==?foot){
countChicken?=?head;
break;
}
head?-=?1;
foot?-=?4;
countRabbit++;
}
System.out.println("雞的數(shù)量:"?+?countChicken);
System.out.println("兔子的數(shù)量:"?+?countRabbit);
以“今有雉兔同籠,上有三十五頭,下有九十四足,問雉兔各幾何?”為例
public?class?test?{
public?static?void?main(String[]?args)?{
int?x,y;??//x:雞??y:兔
for(x=0;x=35;x++)?{???//遍歷雞的只數(shù)
y=35-x;????????//兔的只數(shù)等于35?-?雞
if(2*x+4*y==94)???//如果雞和兔的腳總數(shù)是94????????????
System.out.println("雞"+x+"只,兔"+y+"只");
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args){
while(true){
Scanner scanner = new Scanner(System.in);
System.out.println("請分別輸入頭的數(shù)量和腳的數(shù)量,用空格分開:");
System.out.println("輸入其它內(nèi)容結(jié)束程序運(yùn)行");
int heads,feet;
try{
heads = scanner.nextInt();
feet = scanner.nextInt();
}catch(Exception e){
break;
}
caculate(heads,feet);
}
}
public static void caculate(int head, int foot){
if((foot(int)1) != 0){
System.out.println("請輸入偶數(shù)只腳");
return;
}
if(foot 0){
System.out.println("腳數(shù)應(yīng)為非負(fù)整數(shù)");
return;
}
if(head 0){
System.out.println("頭數(shù)應(yīng)為非負(fù)整數(shù)");
return;
}
if(2*head foot){
System.out.println("頭的數(shù)量太多");
return;
}
if(4*head foot){
System.out.println("腳的數(shù)量太多");
return;
}
int che = (4*head-foot)/2;
int rub = (foot - 2*head)/2;
System.out.println("棚內(nèi)有" + che + "只雞和" + rub + "只兔子");
}
}
當(dāng)前標(biāo)題:JAVA中守株待兔源代碼 編寫守株待兔
網(wǎng)頁地址:http://muchs.cn/article16/doegegg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、全網(wǎng)營銷推廣、域名注冊、移動網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)