求助。。。初学C。。

编译打包和其他
回复
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

求助。。。初学C。。

#1

帖子 yangsos123 » 2010-08-02 21:35

咱家以前学的是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
:em06

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#2

帖子 yangsos123 » 2010-08-02 21:43

没人? :em20

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
arkansas_ubuntu
帖子: 56
注册时间: 2010-05-18 10:00

Re: 求助。。。初学C。。

#3

帖子 arkansas_ubuntu » 2010-08-02 22:45

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.
Ubuntu论坛中的PLMM数目比Linux下的病毒数目还少,所以
就不用想了,如果你真的碰上了,恭喜,请执行以下命令:
sudo mount -t french /dev/mouth0 /mnt/kiss
french这个参数可以用man kiss查到。别忘了umount,否则
buffer内的数据会丢失。
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#4

帖子 yangsos123 » 2010-08-03 8:05

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.。。。

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 求助。。。初学C。。

#5

帖子 link_01 » 2010-08-03 9:13

代码: 全选

#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);
}
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#6

帖子 yangsos123 » 2010-08-03 9:29

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.我想知道到底我的程序错在哪里。。。
:em06
不过Thx了~

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#7

帖子 yangsos123 » 2010-08-03 10:43

来——人——哪—— :em20

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 求助。。。初学C。。

#8

帖子 link_01 » 2010-08-03 10:45

代码: 全选

#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(不是乘方?)
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#9

帖子 yangsos123 » 2010-08-03 10:49

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?

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

Re: 求助。。。初学C。。

#10

帖子 dbzhang800 » 2010-08-03 12:48

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 ,让编译器给出尽可能多的警告。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 求助。。。初学C。。

#11

帖子 BigSnake.NET » 2010-08-03 13:01

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
:em06
1. int main
2. unsigned 用 %u
3. 那个 &res 应该写 res
4. return 0
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
yangsos123
帖子: 331
注册时间: 2010-01-11 20:15
联系:

Re: 求助。。。初学C。。

#12

帖子 yangsos123 » 2010-08-03 15:51

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
:em06
1. int main
2. unsigned 用 %u
3. 那个 &res 应该写 res
4. return 0
3Q~4Q~5Q~6Q~~~~~~~~~~~~~
刚学C,确实。。。

代码: 全选

Art is always explosion~~
[/b][/u]
http://allenalan.wordpress.com/
====Allen Alan=====
头像
mopperwhite
帖子: 330
注册时间: 2010-05-02 20:25
来自: 柯伊伯带外奥尔特云内轨道与黄道错开的空间站里

Re: 求助。。。初学C。。

#13

帖子 mopperwhite » 2010-09-25 0:46

我也是pascal->C的!你也是电脑竞赛的??
愿他们残酷,但是永远正义
愿恐惧永远不会主宰他们的行动并愿民族的荣誉在他们心中高于一切
最后,愿他们最终可以理解,为完成我们建立民族社会主义国家的任务,需要数百年时间并需要每一个人永远将共同的利益置于个人利益之上。
----希特勒绝笔
讨厌我的人可以试着点一下
讨厌我的人可以试着点一下2.0bate
回复