鱼C论坛

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

如何将不在数组中的无素插入数组中呢?为什么我这个不行啊?

[复制链接]
发表于 2014-3-27 05:10:11 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>

  2. void main()
  3. {
  4.         int a[10],l=0,m=5,h=9,i,j,t,n,f;
  5.          int c[11];
  6.         for(i=0;i<10;i++)
  7.         {
  8.                
  9.                 printf("Input a[%d]:",i);
  10.                 while(scanf("%d",&a[i])!=1)
  11.                 {
  12.                         printf("error! please Input a[%d] angin:",i);
  13.                         getchar();
  14.                 }                                                                                                                                                //whlie循环,用以抵抗非法输入;
  15.         }
  16.         printf("\nthe array a[10] is :\n");
  17.         for(i=0;i<10;i++)
  18.         {
  19.                 printf("%5d",a[i]);
  20.         }
  21.         for(i=0;i<10;i++)
  22.         {
  23.                 for(j=0;j<9-i;j++)
  24.                 {
  25.                         if(a[j]>a[j+1])
  26.                         {
  27.                                 t=a[j];
  28.                                 a[j]=a[j+1];
  29.                                 a[j+1]=t;
  30.                         }
  31.                 }
  32.         }                                                                                                                        //两个for循环,用来对输入的数据进行排序;
  33.         printf("\npai xu hou:\n");
  34.         for(i=0;i<10;i++)
  35.         {
  36.                 printf("%5d",a[i]);
  37.         }
  38.         putchar('\n');
  39.         printf("Input n:");
  40.         while(scanf("%d",&n)!=1)
  41.         {
  42.                 printf("error! please input angin:");
  43.                 getchar();
  44.                
  45.         }
  46.         if(n<a[0]||n>a[9])
  47.         {
  48.                 f=0;
  49.                 goto loop;
  50.         }
  51.         while(l<h)
  52.         {
  53.                 if(n==a[m])
  54.                 {
  55.                         f=1;
  56.                         break;
  57.                 }
  58.                 if(n>a[m])
  59.                 {
  60.                         l=m+1;
  61.                         m=(l+h)/2;
  62.                 }
  63.                 if(n<a[m])
  64.                 {
  65.                         h=m-1;
  66.                         m=(l+h)/2;
  67.                 }
  68.         }
  69. loop:if(0==f)
  70.          {
  71.                  printf("there is no %d\n",n);
  72.                
  73.          for(i=0;i<10;i++)                                     //从这里开始:
  74.                  {
  75.                          c[i]=a[i];
  76.                  }
  77.                  c[10]=n;
  78.                  for(i=0;i<11;i++)
  79.                  {
  80.                          for(j=0;j<10-i;j++)
  81.                          {
  82.                                  if(a[j]>a[j+1])
  83.                                  {
  84.                                          t=a[j];
  85.                                          a[j]=a[j+1];
  86.                                          a[j+1]=t;
  87.                                  }
  88.                          }
  89.                  }                                                                                                        //到这里为止。此段代码用于将不在数组中的数据插入数组,并重新排序。
  90.                  printf("数组插入%d之后,排列为:\n",n);                               
  91.                  
  92.                  for(i=0;i<10;i++);
  93.                  {
  94.                          printf("%5d",c[i]);
  95.                  }
  96.                  
  97.          }
  98.          if(1==f)
  99.          {
  100.                  printf("%d is a[%d]\n",n,m);
  101.          }
  102.          
  103. }
复制代码


以上的我的代码,不过运行的时候却得不到想要的结果……

像这样。.jpg

问题出在哪里呢?希望高手指点指点………………
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-3-27 08:30:31 | 显示全部楼层
我就看看,不说话
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-27 14:36:34 | 显示全部楼层
  1. #include<stdio.h>

  2. void main()
  3. {
  4.         int a[10]={1,2,3,4,5,6,7,8,9,10},l=0,m=5,h=9,i,j,t,n,f;
  5.          int c[11];
  6.         for(i=0;i<10;i++)
  7.         {
  8.                
  9.                 printf("Input a[%d]:",i);
  10.                 while(scanf("%d",&a[i])!=1)
  11.                 {
  12.                         printf("error! please Input a[%d] angin:",i);
  13.                         getchar();
  14.                 }                                                                                                                                                //whlie循环,用以抵抗非法输入;
  15.         }
  16.         printf("\nthe array a[10] is :\n");
  17.         for(i=0;i<10;i++)
  18.         {
  19.                 printf("%5d",a[i]);
  20.         }
  21.         for(i=0;i<10;i++)
  22.         {
  23.                 for(j=0;j<9-i;j++)
  24.                 {
  25.                         if(a[j]>a[j+1])
  26.                         {
  27.                                 t=a[j];
  28.                                 a[j]=a[j+1];
  29.                                 a[j+1]=t;
  30.                         }
  31.                 }
  32.         }                                                                                                                        //两个for循环,用来对输入的数据进行排序;
  33.         printf("\npai xu hou:\n");
  34.         for(i=0;i<10;i++)
  35.         {
  36.                 printf("%5d",a[i]);
  37.                 c[i] = a[i];
  38.         }
  39.         putchar('\n');
  40.         printf("Input n:");
  41.        
  42.         while(scanf("%d",&n)!=1)
  43.         {
  44.                 printf("error! please input angin:");
  45.                 getchar();
  46.                
  47.         }
  48.         c[10] = n;
  49.         if(n<a[0]||n>a[9])
  50.         {
  51.                 f=0;
  52.                 goto loop;
  53.         }
  54.         while(l<h)
  55.         {
  56.                 if(n==a[m])
  57.                 {
  58.                         f=1;
  59.                         break;
  60.                 }
  61.                 if(n>a[m])
  62.                 {
  63.                         l=m+1;
  64.                         m=(l+h)/2;
  65.                 }
  66.                 if(n<a[m])
  67.                 {
  68.                         h=m-1;
  69.                         m=(l+h)/2;
  70.                 }
  71.         }
  72. loop:if(0==f)
  73.          {
  74.                  printf("there is no %d\n",n);
  75.                
  76.                  for(i=0;i<11;i++)
  77.                  {
  78.                          for(j=0;j<10-i;j++)
  79.                          {
  80.                                  if(c[j]>c[j+1])
  81.                                  {
  82.                                          t=c[j];
  83.                                          c[j]=c[j+1];
  84.                                          c[j+1]=t;
  85.                                  }
  86.                          }
  87.                  }                                                                                                        //到这里为止。此段代码用于将不在数组中的数据插入数组,并重新排序。
  88.                  printf("数组插入%d之后,排列为:\n",n);                               
  89.                  
  90.                  for(i=0;i<11;i++)
  91.                  {
  92.                          printf("%5d",c[i]);
  93.                  }
  94.                  
  95.          }
  96.          if(1==f)
  97.          {
  98.                  printf("%d is a[%d]\n",n,m);
  99.          }
  100.          
  101. }
复制代码
我自己又查了一下,改了几个错误,但还是不行。。。
单步调试的结果,当输入值n在a[0]和a[9]之间的时候,程序直接跳到结尾处,而不执行后面的语句。
百思不得其解,等待解答中……谢谢。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 14:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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