RSS
 

指针-数组 之 寻找最小数字

24 May

刚刚写成功了一个,受到了很多 CPL 那个书的启发。源码大家可以看下面:

#include"stdio.h"
#define N 5

main()
{
	int i,j=0,a[N];
	int *p=&a[0];

	printf("Input %d numbers:\n",N);

	for (i = 0; i < N; i++){
		scanf("%d",&a[i]);
	}
	for (i = 0; i < N; i++){
		if (*(p+i) < *p){
			*p = *(p+i);
			j = i+1;
		}
	}
	printf("the smallest number is: %d, No.%d\n",*p,j);
}

大家在这里查看 P.S:coolcode 插件不认 c 语言,所以缩进没有了。大家看刚才那个链接吧,那样会好点。

我的一个问题是,我想把中间那两个for给剩下一个,怎么作呢?貌似直接接上不成……迷惑中,觅达人~

update Jaykenduke 童鞋先抓了一只虫~
update @ May-26-2008 新版本的程序已经更新,大家看这里

 
5 Comments

Posted in web

 

Tags: , , ,

Leave a Reply

 
Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).
 
  1. echo

    May 24, 2008 at 10:38 pm

    反正我看不懂~~

    ReplyReply
     
  2. jaykenduke

    May 25, 2008 at 10:03 am

    现在没什么大问题了
    #include
    #define N 5

    main()
    {
    int i,j,m=0,a[N];
    int *p=&a[0];

    printf(“Input %d numbers:\n”,N);

    for (i = 0; i < N; i++){
    scanf(“%d”,&a[i]);
    for(j =0;j<i;j++)
    {

    if (*(p+i) < *p)
    {
    *p = *(p+i);
    m=i+1;
    }

    }

    }
    printf(“the smallest number is: %d, No.%d\n”,*p,m);
    }

    ReplyReply
     
  3. 1024

    May 25, 2008 at 12:41 pm

    /* 留言必须含有至少一个中文字 -_- */
    #include
    #define N 5

    int main()
    {
    int i,j,a[N];
    int *p=&a[0];

    printf(“Input %d numbers:\n”,N);

    for (i = 0; i < N; i++){
    scanf(“%d”,&a[i]);
    if (i != 0){
    if (*(p+i) < *p){
    *p = *(p+i);
    j = i;
    }
    }
    }
    printf(“the smallest number is: %d, No.%d\n”,*p,j+1);
    return 0;
    }

    ReplyReply
     
  4. 柒小诺

    May 25, 2008 at 1:16 pm

    这个还是有点点问题,就是那个 m 那里, 如果数字在第一个,就会显示 No.0 …. 如果市 m=i; 然后在后面成 m+1 就好了……诡异,也许是编译器的问题,我用的tc搞的。……很不爽。

    ReplyReply
     
  5. 柒小诺

    May 25, 2008 at 1:17 pm

    恩 和这个一样的我在tc里也写出来了,不知道回 code::block 会怎样…… 要这么多真讨厌tc。。。

    中文字这个问题我也不知道怎么回事儿,模板我就没动过,基本上。

    ReplyReply