文章目录
- (1) 定义 day、x1、x2 为基本整型,并为 day 和 x2 赋初值 9 和 1。 (2) 使用 while 语句由后向前推出第一天摘的桃子数。 (3) 输出结果。 (4) 程序代码如下: #include <stdio.h> int main() { intday,x1,x2; /*定义 day、x1、x2 3 个变董为基本整型*/ day=9; x2=1; while(day>0) { x1=(x2+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/ x2=x1; day--; /*因为从后向前推所以天数递减*/ } printf("the total is %d\n",x1); /* 输出桃子的总数*/ return 0; }
- the total is 1534
- 本实例的思路基本上是先找出变量间的关系,也就是要明确第一天桃数和第二天桃子数之间的关系,即第二天桃子数加 1 的 2 倍等于第一天的桃子数。 if(typeof window.xh_social_share!='function'){ window.xh_social_share=function(settings){ if(!settings||typeof settings!='object'){return;} var url = encodeURIComponent('https://www.jiuanw.com/2022/02/05/41286/'); var title = encodeURIComponent(document.title); var summary = document.querySelector('.entry-content') || document.querySelector('article') || document.querySelector('main') || document.querySelector('body') || ''; var pic = ''; if(summary){ var index = 0; [].forEach.call(summary.querySelectorAll('img'), function(a){ if(index++>=3){ return false; } pic += (pic?'||':'') + encodeURIComponent(a.src); }); summary = encodeURIComponent(summary.innerText.replace(/\r|\n|\t/g,'').replace(/ +/g,' ').replace(//g,'').substr(0,80)); } var single_img ='https://www.jiuanw.com/wp-content/uploads/2020/05/1590931267-1-1590931267-300x300.jpeg'; pic = pic.replace(single_img+'||',''); pic = pic.replace(single_img,''); pic = single_img+(pic?'||':'')+pic; var link =typeof settings.link!='undefined'?settings.link:''; if(!link||link.length
微信公众号功能:表情包查询,每日表情包推送,京东捡漏优惠群
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将第一天剩下的桃子吃掉一半,有多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,发现只剩下一个桃子了。编写程序求猴子第一天摘了多少个桃子。
(1) 定义 day、x1、x2 为基本整型,并为 day 和 x2 赋初值 9 和 1。
(2) 使用 while 语句由后向前推出第一天摘的桃子数。
(3) 输出结果。
(4) 程序代码如下:
#include <stdio.h>
int main()
{
intday,x1,x2; /*定义 day、x1、x2 3 个变董为基本整型*/
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2; /*第一天的桃子数是第二天桃子数加1后的2倍*/
x2=x1;
day--; /*因为从后向前推所以天数递减*/
}
printf("the total is %d\n",x1); /* 输出桃子的总数*/
return 0;
}
the total is 1534
本实例的思路基本上是先找出变量间的关系,也就是要明确第一天桃数和第二天桃子数之间的关系,即第二天桃子数加 1 的 2 倍等于第一天的桃子数。
未经允许不得转载:斗图吧 » 【C语言】猴子吃桃问题

斗图吧



评论前必须登录!
登陆 注册