c语言程序猴子偷桃问题

c语言程序猴子偷桃问题,第1张

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

#include "stdio.h"

main()

{

int sum=1,i

for(i=0i<9i++)

sum=(sum+1)*2

printf("猴子共摘了%d个桃子。\n",sum)

}

#include<iostream>

using namespace std

void main()

{

int a=1,n,i=1

cout<<"第几天后"<<endl

cin>>n

while(i<n)

{

a=(a+1)*2

i++

}

cout<<"第一天的桃子数"<<a<<endl

}

public class Monkey {

private String color

private String sex

public Monkey(String color,String sex) {

this.color = color

this.sex = sex

}

public Monkey(String color) {

this.color = color

this.sex = "公"

}

public Monkey(){}

public String getColor() {

return color

}

public void setColor(String color) {

this.color = color

}

public String getSex() {

return sex

}

public void setSex(String sex) {

this.sex = sex

}

public int add(int p1,int p2,int p3) {

return p1+p2+p3

}

public int add(int p1,int p2) {

return p1+p2

}

}

public class Zoo {

public static void main(String[] args) {

Monkey g = new Monkey("黑")

Monkey l = new Monkey("红","母")

int p1 = g.add(1, 2)

int p2 = l.add(1, 2, 3)

System.out.println(g.getSex()+":"+p1)

System.out.println(l.getSex()+":"+p2)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11171411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存