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

刚刚写成功了一个,受到了很多 CPL 那个书的启发。源码大家可以看下面:
[c]
#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);
}
[/c]
大家在这里查看 P.S:coolcode 插件不认 c 语言,所以缩进没有了。大家看刚才那个链接吧,那样会好点。

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

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

收藏/分享
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • MySpace
  • Pownce
  • StumbleUpon
  • Haohao
  • E-mail this story to a friend!
  • SalesMarks
  • Technorati
  • TwitThis

Comments (5)

echoMay 24th, 2008 at 10:38 pm

反正我看不懂~~

[Reply]

jaykendukeMay 25th, 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);
}

[Reply]

1024May 25th, 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;
}

[Reply]

柒小诺May 25th, 2008 at 1:16 pm

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

[Reply]

柒小诺May 25th, 2008 at 1:17 pm

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

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

[Reply]

Leave a comment

Your comment