当前位置: 代码迷 >> C语言 >> 请教一个小问题!
  详细解决方案

请教一个小问题!

热度:96   发布时间:2007-09-14 11:41:53.0
请教一个小问题!

S=20+21+22+23+......+2n,直到S<=100.

帮我看一下,我那里编错了,谢谢!

#include <math.h>
#include <stdio.h>
int main(void)
{
int x = 2, y = 0,s=0;
return 0;
while(s<=100)
{
s=s+pow(x,y);
++y;
}
printf("%d", s);
}

搜索更多相关的解决方案: include  return  

----------------解决方案--------------------------------------------------------
#include <math.h>
#include <stdio.h>
int main(void)
{
int x = 2, y = 0,s=0;
return 0; -----------------------------------直接返回了```程序结束```
while(s<=100)
{
s=s+pow(x,y);
++y;
}
printf("%d", s);
return 0; ---------------------------------其实应该放在这里```
}


顺便问一下````你的指数是什么输出来的```我想那样写都不回``



----------------解决方案--------------------------------------------------------
从return 0;程序就运行结束了,怎么能放在这呢!而且你的程序是s>100后才退出循环!
#include <math.h>
#include <stdio.h>
int main(void)
{
int x=2,y=0,s=0;
while(s+pow(x,y)<=100)
{
s=s+pow(x,y);
++y;
}
printf("%d\n",s);
return 0;
}
----------------解决方案--------------------------------------------------------
LS```LZ的意思```不就是要让S<=100``也就是说S>100就完了吗?```
所以应该没什么吧```


----------------解决方案--------------------------------------------------------
DEVCPP出现一个警告```

#include <math.h>
#include <stdio.h>
int main(void)
{
int x=2,y=0,s=0;

while(s+pow(x,y)<=100)
{
s=s + (int)pow(x,y); ------------------------这里要强制转换一下类型`
++y;
}

printf("%d\n",s);
getchar();
return 0;
}


----------------解决方案--------------------------------------------------------
将表达式 S=20+21+22+23+......+2n,直到S<=100
变一下形,,,S=1+2+4+8+16+......,直到s<=100,可以用下面这种方法吗?看看那里错了哟!怎么结果是 127,而正确的是 63 啊????

#include <math.h>
#include <stdio.h>
main()
{
int n=1,m,s=1;
while(s+n<=100)
{
n=2*n;
s=s+n;
}
printf("%d",s);
}

[此贴子已经被作者于2007-9-14 12:26:16编辑过]


----------------解决方案--------------------------------------------------------

#include <stdio.h>
#include <math.h>

int main(void)
{
int s=0,x=2,y=0;
while (y<=100)
{
s=s+(int) pow(x,y); /*如果这里不转换的话。。会变成负数。。*/
y++;
}
printf ("%d",s);
getch();
}


----------------解决方案--------------------------------------------------------
整型(int)的数据范围是-32768______32767
而你这么大的数据不是溢出了吗?
----------------解决方案--------------------------------------------------------
回复:(skydun)将表达式 S=20+21+22+23+.........
改成s+2*n跳出循环,这样结果就是63了,我在三楼已经回答了的!
----------------解决方案--------------------------------------------------------
  相关解决方案