#include<limit.h>
#define min(x,y) ((x)<(y)?(x):(y))
int mindistance(int a[],int b[],int m,int n)
{
int minimum=INT_MAX;
int x=0,y=0;
while(x<m&&y<n){
if(a[x]>=b[y]){
minimum=min(minimum,a[x]-b[y]);
y++;}
else{
minimum=min(minimum,b[y]-a[x]);
x++;}
}
return minimum;
}
void main(){
int a[6]={8,18,28,38,48,49};
int b[6]={7,15,16,17,18,56};
int m=n=6;
printf(“%d”,mindistance(a,b,m,n));
}
这段代码为什么报错,还有limit头文件中的极限值,什么时候用,这里设个变量赋个初值不可以吗?
37
#include <stdio.h> #include <limits.h> #define min(x,y) ((x)<(y)?(x):(y)) int mindistance(int a[], int b[],int m,int n) { int minimum = INT_MAX; int x = 0, y = 0; while (x < m && y < n) { if (a[x] >= b[y]) { minimum=min(minimum, a[x]-b[y]); y++; } else { minimum=min(minimum,b[y]-a[x]); x++; } } return minimum; } int main() { int a[6] = {8,18,28,38,48,49}; int b[6] = {7,15,16,17,18,56}; int m, n; m = n =6; printf("%d\n", mindistance(a, b, m, n)); return 0; }
头文件名写错了,include应该是limits.h;
int m = n =6;
这样定义,n是未定义的,改一下;如上的代码,注意下代码风格~
3
一般是用到某个类型的最大值或最小值时会用到吧,尤其是在嵌入式平台,由于其会设计不同的硬件平台,不同的编译器会对不同类型的数据大小做一些限制;
建议看一下limits.h里的内容,根据内容来区分在什么场合下使用。
10
5
C:\Program Files\Microsoft Visual Studio 8\VC\crt\src>grep -0is limits.h *.c *.cpp
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\assert.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\dbgheap.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\dbghook.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\dospawn.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\fread.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\gets.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\initctyp.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\initmon.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\lcnvinit.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\lconv.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\localtim.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\loctim64.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\makepath.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\mbrtowc.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\mbsnbcol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\mbsncoll.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\mbsnicol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\mlock.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\output.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\putch.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\putwch.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\rotl.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\rotr.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\sbheap.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\setenv.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\setvbuf.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\sprintf.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\stdargv.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strlwr.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strncoll.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strnicol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strtol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strtoq.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strupr.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\strxfrm.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\swprintf.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\vsprintf.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\vswprint.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcrtomb.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcsncoll.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcsnicol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcstol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcstombs.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcstoq.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wcsxfrm.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wctomb.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wild.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\wperror.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xmbtowc.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xstol.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xstoll.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xstoul.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xstoull.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xstrxfrm.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xtoa.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xwcsxfrm.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\xwctomb.c
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\swprintn.cpp
File: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\vswprinn.cpp