鱼C论坛

 找回密码
 立即注册
查看: 1509|回复: 2

[已解决]一样的代码在dec 5.11和小甲鱼的虚拟机中运行结果不同?

[复制链接]
发表于 2021-3-14 23:16:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>

  2. int main()
  3. {
  4. int max(int x,int y);
  5. int a[10],m,n,i;
  6. printf("enter 10 integer numbers:");
  7. for(i=0;i<10;i++)
  8. scanf("%d",&a[i]);
  9. printf("\n");
  10. for(i=1,m=a[0],n=0;i<10;i++);
  11. {
  12.   if(max(m,a[i])>m)
  13.   {
  14.    m=max(m,a[i]);
  15.    n=i;
  16.   }
  17. }
  18. printf("the largest number is %d\nit is the %dth number.\n",m,n+1);
  19. return 0;
  20. }

  21. int max(int x,int y)
  22. {
  23. return(x>y?x:y);
  24. }
复制代码

代码如上,在小甲鱼的虚拟机中运行结果如下:
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.
这是为什么呀???谢谢!!!
最佳答案
2021-3-15 00:50:17
本帖最后由 jackz007 于 2021-3-15 00:53 编辑

        代码有错误
for(i=1,m=a[0],n=0;i<10;i++);   // 得去掉最后这个 "尾巴"
  1. #include <stdio.h>

  2. int max(int x , int y)
  3. {
  4.         return(x > y ? x : y)                                                       ;
  5. }

  6. int main(void)
  7. {
  8.         int a[10] , i , n                                                           ;
  9.         printf("enter 10 integer numbers : ")                                       ;
  10.         for(i = 0 ; i < 10 ; i ++) scanf("%d" , & a[i])                             ;
  11.         printf("\n")                                                                ;
  12.         for(i = 1 , n = 0 ; i < 10 ; i ++) if(max(a[n] , a[i]) == a[i]) n = i       ;
  13.         printf("the largest number is %d\nit is the %dth number.\n" , a[n] , n + 1) ;
  14. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. enter 10 integer numbers : 1 2 3 10 4 5 6 7 8 9

  4. the largest number is 10
  5. it is the 4th number.

  6. D:\00.Excise\C>
复制代码

        再用这个代码测试,看看运行结果有没有差异?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 00:50:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-3-15 00:53 编辑

        代码有错误
for(i=1,m=a[0],n=0;i<10;i++);   // 得去掉最后这个 "尾巴"
  1. #include <stdio.h>

  2. int max(int x , int y)
  3. {
  4.         return(x > y ? x : y)                                                       ;
  5. }

  6. int main(void)
  7. {
  8.         int a[10] , i , n                                                           ;
  9.         printf("enter 10 integer numbers : ")                                       ;
  10.         for(i = 0 ; i < 10 ; i ++) scanf("%d" , & a[i])                             ;
  11.         printf("\n")                                                                ;
  12.         for(i = 1 , n = 0 ; i < 10 ; i ++) if(max(a[n] , a[i]) == a[i]) n = i       ;
  13.         printf("the largest number is %d\nit is the %dth number.\n" , a[n] , n + 1) ;
  14. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. enter 10 integer numbers : 1 2 3 10 4 5 6 7 8 9

  4. the largest number is 10
  5. it is the 4th number.

  6. D:\00.Excise\C>
复制代码

        再用这个代码测试,看看运行结果有没有差异?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-3-15 17:23:31 | 显示全部楼层
jackz007 发表于 2021-3-15 00:50
代码有错误
for(i=1,m=a[0],n=0;i

谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢你!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-2 12:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表