解题思路:通过对比三种循环语句,发现while()和do……while()这两种循环时间复杂度要比for要少许多。设最后一天的桃子个数sum=1,则倒数第二天的桃子数量x,由此x-x/2-1=sum,整理得x=(sum+1)*2;而循环条件内的n一定要大于1,其本质是去求第二天的桃子个数,而不是第一天!!!
注意事项:
而循环条件内的n一定要大于1,其本质是去求第二天的桃子个数,而不是第一天!!!
参考代码:
#include<stdio.h>
int main()
{
int n,sum=1;
scanf("%d", &n);
do
{
sum=(sum+1)*2;
n--;
}while(n>1);
printf("%d\n",sum);
return 0;
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:443 |
最长单词 (C语言代码)浏览:1461 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:991 |
WU-字符串比较 (C++代码)浏览:818 |
WU-拆分位数 (C++代码)浏览:817 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:748 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:593 |
1013题解浏览:595 |
整除问题 (C语言代码)浏览:592 |
C语言训练-自守数问题 (C语言代码)浏览:791 |