分页: 1 / 1
求助。。。初学C。。
发表于 : 2010-08-02 21:35
由 yangsos123
咱家以前学的是Pascal。。。转投C了。。。。
卸了个简单的程序,求的是1+2+2^2+2^3+2^4+……+2^n,,,
编译无问题,结果是负的。。绝对值还很大。。。
都已经unsigned了!!!
代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
yangsos123@AlanAllen:~/C/Part-入门$ gcc ex6.c -o ex6.out
ex6.c: In function ‘main’:
ex6.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘unsigned int *’
yangsos123@AlanAllen:~/C/Part-入门$ ./ex6.out
2
-1074561196

Re: 求助。。。初学C。。
发表于 : 2010-08-02 21:43
由 yangsos123
没人?

Re: 求助。。。初学C。。
发表于 : 2010-08-02 22:45
由 arkansas_ubuntu
The range of unsigned int is 0 to 65535.
If n is greater than 65535 and the result will be a negative value.
Just change
unsigned int i, n, res=1;
to
unsigned long i, n, res=1;
The range of unsigned long is 0 to 4294967295. Make sure your value is not overflow.
Re: 求助。。。初学C。。
发表于 : 2010-08-03 8:05
由 yangsos123
arkansas_ubuntu 写了:The range of unsigned int is 0 to 65535.
If n is greater than 65535 and the result will be a negative value.
Just change
unsigned int i, n, res=1;
to
unsigned long i, n, res=1;
The range of unsigned long is 0 to 4294967295. Make sure your value is not overflow.
问题不在这个吧。。。。。
我输的是2.。。。
Re: 求助。。。初学C。。
发表于 : 2010-08-03 9:13
由 link_01
代码: 全选
#include <stdio.h>
#include <math.h>
void main() {
printf("Please enter a int number: ");
long i,m,n,r=1;
scanf("%ld",&n);
for (i = 1; i <= n; ++i) {
m = pow(2,i);
r += m;
}
printf("result is: %ld\n",r);
}
Re: 求助。。。初学C。。
发表于 : 2010-08-03 9:29
由 yangsos123
link_01 写了:代码: 全选
#include <stdio.h>
#include <math.h>
void main() {
printf("Please enter a int number: ");
long i,m,n,r=1;
scanf("%ld",&n);
for (i = 1; i <= n; ++i) {
m = pow(2,i);
r += m;
}
printf("result is: %ld\n",r);
}
1.你的时间复杂度比我高。
2.pow出来的值是Double型。
3.我想知道到底我的程序错在哪里。。。
不过Thx了~
Re: 求助。。。初学C。。
发表于 : 2010-08-03 10:43
由 yangsos123
来——人——哪——

Re: 求助。。。初学C。。
发表于 : 2010-08-03 10:45
由 link_01
代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
我也初学,可不懂什么时间复杂度,所以你的这段不理解很多:
for前, n++是为什么?
res = 2 * res是做什么?
你的题目是1+2+2^2+2^3+2^4+……+2^n(不是乘方?)
Re: 求助。。。初学C。。
发表于 : 2010-08-03 10:49
由 yangsos123
link_01 写了:代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
我也初学,可不懂什么时间复杂度,所以你的这段不理解很多:
for前, n++是为什么?
res = 2 * res是做什么?
你的题目是1+2+2^2+2^3+2^4+……+2^n(不是乘方?)
可以做数学。。。
原式
=2+2+2^2+2^3+2^4+……+2^n-1
=2^2+2^2+2^3+2^4+……+2^n-1
=2^2*2+2^3+2^4+……+2^n-1
=2^3*2+2^4+……+2^n-1
=2^(n+1)-1
这样就好理解了。。。
以前用Pascal写都没什么问题。。。
God!难道我不适合C?
Re: 求助。。。初学C。。
发表于 : 2010-08-03 12:48
由 dbzhang800
yangsos123 写了:link_01 写了:代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
以前用Pascal写都没什么问题。。。
God!难道我不适合C?
建议你找本好点的C语言的入门书先看一下(最好不是国产的)。你这一个例子暴露了不少问题。
首先,首帖中已经给出了很好的警告了,如果你接受这个警告,并改正错误,你可以得到正常的结果
* 无符号数 用 %u 而不是 %d, 后面是值 res ,而不是地址 &res
其次,按C语言要求,main前面你应该添加 int,函数结束处添加 return 0;
再次,编译时最好添加 -Wall ,让编译器给出尽可能多的警告。
Re: 求助。。。初学C。。
发表于 : 2010-08-03 13:01
由 BigSnake.NET
yangsos123 写了:咱家以前学的是Pascal。。。转投C了。。。。
卸了个简单的程序,求的是1+2+2^2+2^3+2^4+……+2^n,,,
编译无问题,结果是负的。。绝对值还很大。。。
都已经unsigned了!!!
代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
yangsos123@AlanAllen:~/C/Part-入门$ gcc ex6.c -o ex6.out
ex6.c: In function ‘main’:
ex6.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘unsigned int *’
yangsos123@AlanAllen:~/C/Part-入门$ ./ex6.out
2
-1074561196

1. int main
2. unsigned 用 %u
3. 那个 &res 应该写 res
4. return 0
Re: 求助。。。初学C。。
发表于 : 2010-08-03 15:51
由 yangsos123
BigSnake.NET 写了:yangsos123 写了:咱家以前学的是Pascal。。。转投C了。。。。
卸了个简单的程序,求的是1+2+2^2+2^3+2^4+……+2^n,,,
编译无问题,结果是负的。。绝对值还很大。。。
都已经unsigned了!!!
代码: 全选
#include <stdio.h>
main()
{
unsigned int i,n,res=1;
scanf("%d",&n);
n++;
for (i=1;i<=n;i++)
res*=2;
res-=1;
printf("%d\n",&res);
}
yangsos123@AlanAllen:~/C/Part-入门$ gcc ex6.c -o ex6.out
ex6.c: In function ‘main’:
ex6.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘unsigned int *’
yangsos123@AlanAllen:~/C/Part-入门$ ./ex6.out
2
-1074561196

1. int main
2. unsigned 用 %u
3. 那个 &res 应该写 res
4. return 0
3Q~4Q~5Q~6Q~~~~~~~~~~~~~
刚学C,确实。。。
Re: 求助。。。初学C。。
发表于 : 2010-09-25 0:46
由 mopperwhite
我也是pascal->C的!你也是电脑竞赛的??