|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- int main()
- {
- int max(int x,int y);
- int a[10],m,n,i;
- printf("enter 10 integer numbers:");
- for(i=0;i<10;i++)
- scanf("%d",&a[i]);
- printf("\n");
- for(i=1,m=a[0],n=0;i<10;i++);
- {
- if(max(m,a[i])>m)
- {
- m=max(m,a[i]);
- n=i;
- }
- }
- printf("the largest number is %d\nit is the %dth number.\n",m,n+1);
- return 0;
- }
- int max(int x,int y)
- {
- return(x>y?x:y);
- }
复制代码
代码如上,在小甲鱼的虚拟机中运行结果如下:
enter 10 integer numbers:1 2 3 4 5 6 7 8 9 10
the largest number is 1
it is the 1th number.
在window中的运行结果如下:
the largest number is 49
it is the 11th number.
这是为什么呀???谢谢!!!
本帖最后由 jackz007 于 2021-3-15 00:53 编辑
代码有错误
for(i=1,m=a[0],n=0;i<10;i++) ; // 得去掉最后这个 "尾巴"
- #include <stdio.h>
- int max(int x , int y)
- {
- return(x > y ? x : y) ;
- }
- int main(void)
- {
- int a[10] , i , n ;
- printf("enter 10 integer numbers : ") ;
- for(i = 0 ; i < 10 ; i ++) scanf("%d" , & a[i]) ;
- printf("\n") ;
- for(i = 1 , n = 0 ; i < 10 ; i ++) if(max(a[n] , a[i]) == a[i]) n = i ;
- printf("the largest number is %d\nit is the %dth number.\n" , a[n] , n + 1) ;
- }
复制代码
编译、运行实况
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- enter 10 integer numbers : 1 2 3 10 4 5 6 7 8 9
- the largest number is 10
- it is the 4th number.
- D:\00.Excise\C>
复制代码
再用这个代码测试,看看运行结果有没有差异?
|
|