挑战C编程

时间:2015-02-18 04:41:57

标签: c

我应该怎么做才能让程序运行没有任何错误..

#include <stdio.h>


    int i= 0;
    while (i< SIZE){
        j= i+1;
        while (j<SIZE){
            if (myList[i]+ myList [j] == target){
                printf("%d AND %d\n", i, j);

            }
            j= j+1;
        }
        i=i+1;
    }

为了编译和执行此代码,您必须将其输入编辑器。确保适当声明所有必需的变量。还要确保在执行这些if-else语句之前使用scanf()输入值。如果多次运行编译器,它将帮助您识别必须声明的变量。

问题:写一个C程序,它将读入N = 1000个整数,i1,i2,...,iN。这些数字未排序,您可以假设这些整数也没有任何常规模式。读完这些N个整数后,您的程序现在必须读入一个名为Target的新整数。从这N个整数中找出所有整数对ij和ik,使得ij + ik = Target。

1 个答案:

答案 0 :(得分:1)

好吧,我尝试了你的代码,并让它运行起来。我假设j可以等于k。如果它们不相等,那么我已经指出要在代码中更改哪个部分(其中ij而不是jk)。那么这里是固定代码

#include <stdio.h>
#define SIZE 10   // You can change SIZE here
int main()
 {
    int myList[SIZE],target;     
    printf("Enter 10 Numbers\n");   
    for(int k=0;k<SIZE;k++)
      {
       scanf("%d",&myList[k]);
      }
    printf("Enter the target\n");  
    scanf("%d",&target);   
    int i= 0,j;
    while (i< SIZE)
      {
        j=i;        // if i and j should not be equal just change it to j=i+1
        while (j<SIZE)
          {
            if (myList[i]+ myList [j] == target)
              {
                printf("%d AND %d\n", i, j);
              }
            j= j+1;
        }
        i=i+1;
      }
    return 0;
}

在此代码中,为方便起见,我使用了SIZE=10,因为我不想输入1000个数字。您可以随时将SIZE更改为1000。

干杯.......希望这是你想要的。