我正在上课。提示是:
编写程序以查找未出现的最小正整数 在数组中。该数字不能由数组中2个不同或相同数字的总和形成。 e1,2,3和4出现在此数组中。 4可以形成为 3 + 1、5作为3 + 2、6作为3 + 3、7作为3 + 4和8作为4 + 4(可以使用两次数字是可以的)。 9没有出现在数组中,并且不能形成为2个数字的和 数组。因此,9是该数组的解决方案。使用功能
int issumof2(int data[], int size, int number) int inarray(int data[], int size, int number)
issumof2
如果number是数据中2个元素的和,则返回1,否则返回0。inarray
如果数据中包含数字,则返回1,否则返回0。
#include <stdio.h>
//function declaration
int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size = 7, sum2, sum, number, data[7], i, j;
int main()
{
//get user input
printf("Enter 7 numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);
//function call
issumof2(data, size, number);
inarray(data, size, number);
number = 0;
//loop while condition is true and stop when condition becomes false
while (inarray(data, size, number) == 1 || issumof2(data, size, number) == 1)
{
number = number + 1;//increment number till loop stop
//print smallest number
printf("Smallest positive integer: %d\n", number);
}
return 0;
}
Expected output:
Enter 7 numbers
1 2 2 3 4 3 1
Smallest positive Integer = 9
//1,2,3,4 are in array. 3+1=4,3+2=5,3+3=6,3+4=7,4+4=8(number can be used twice).
//9 is not inarray or not a sum of 2 numbers in the array
int issumof2(int data[], int size, int number)
{
//add data numbers in array
for (i = 0; i < size; i++)
{
for (j = 0 + i; j < size; j++)
{
sum = data[i] + data[i];//add same numbers
sum2 = data[i] + data[j];//add all 2 combinations of different number
if (sum == data[i] || sum2 == data[i])//comparing with data value
return 1;//if sum or sum2 exists in data
}
}
return 0;//if sum or sum2 don't exist
}
//goes to infinite loop
int inarray(int data[], int size, int number)
{
for (number = 1; number <=size; number++)
{
printf("inarray number=%d\n", number);
//loop through data array
for (i = 0; i < size; i++)
{
printf("data[i]=%d\n", data[i]);
if (number == data[i])//if this is true it goes to infinite loop
//if false it just stops comparing
{
return 1;
printf("\n");
}
}
}
return 0;
}
答案 0 :(得分:2)
快速运行您所发布的内容实际上会导致无限循环,除非我错过了一些东西。我看到了几个错误:
size
设置为等于3,然后使用它来尝试扫描七个数字。inarray
和issumof2
中,有条件条件根据条件的结果返回一或零,因此它们只运行最内层循环的一次迭代。您需要将else语句移出循环。issumof2
中没有使用number
,但是如果我没有记错的话,那就是您必须将所有金额进行比较的地方。另外,我认为您可能无意中使用了i
来代替j
,反之亦然。以下是您的一些代码中带有注释的注释,它们有望导致您纠正错误:
int main() {
int data[7];
int i, number = 0;
// size is three so we won't scan 7 integers if we enter them line by line.
printf("Enter 7 numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);
// the problem states that both of these functions must return zero for the
// expected answer, yet this loop breaks whenever number does not meet both
// criteria, which isn't what we want.
while(inarray(data, size, number) == 0 && issumof2(data, size, number) == 0) {
number = number + 1;
// if the conditional matched the problem statement, then we would print
// this line for every integer that fails the criteria, but that isn't
// what we want.
printf("Smallest positive integer: %d\n", number);
}
return 0;
}
int issumof2(int data[], int size, int number) {
int i, j, sum = 0;
for (i = 0; i < size; i++) {
// here we've doubled the value at i and saved within a temporary
// variable -- but why?
sum2 =data[i]+data[i];
for (j = 0; j < size; j++) {
// here we're combining different values to test their sum against
// number -- good job
sum = data[i] + data[j];
// why are we comparing the data at i with twice itself (sum2), and
// its addition with the data at j (sum)? we must compare sum2
// against number.
if (sum==data[i] || sum2==data[i])
// here we return 1 if the condition is met. this would be good
// if our conditional was representative of the problem.
return 1;
else
// otherwise we return 0 -- why? i guess we don't need to check
// any other sums...
return 0;
}
}
// alright we're done here. or are we? the prototype says we'll return an int
}
int inarray(int data[], int size, int number)
{
int i;
// so we're looping over all input -- good job
for (i = 0; i < size; i++) {
// here we test for equality of index i with the data at index i -- why?
// we must compare the data at index i with number.
if (i == data[i])
return 1;
else // again we can't just call it a day after checking just one number
return 0;
}
// and as before we need to return some value if we complete the loop
}
这是一个更正的解决方案:
#include<stdio.h>
int issumof2(int[], int, int);
int inarray(int[], int, int);
int size = 7;
int issumof2(int data[], int size, int number) {
int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (i == j)
continue;
if (data[i] + data[j] == number)
return 1;
}
}
return 0;
}
int inarray(int data[], int size, int number) {
int i;
for (i = 0; i < size; i++) {
if (data[i] == number)
return 1;
}
return 0;
}
int main() {
int data[7];
int i, number = 1;
printf("Enter 7 numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);
while(inarray(data, size, number) == 1 || issumof2(data, size, number) == 1)
number++;
printf("Smallest positive integer: %d\n", number);
return 0;
}
答案 1 :(得分:1)
首先,这是您的逻辑正确的答案
#include<stdio.h>
int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size=7,sum2;
int main()
{
int data[7];
int i, number=1;
printf("Enter 7 numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);
//I am a little confused about this part. I wasn't sure how to write it.
while(inarray(data, size, number)==1 || issumof2(data, size, number)==1)//till functions are false
{
number=number+1;
}
printf("Smallest positive integer: %d\n", number);
return 0;
}
int issumof2(int data[], int size, int number)
{
int i, j, sum = 0;
for (i = 0; i < size; i++)
{
sum2 =data[i]+data[i];
for (j = 0; j < size; j++)
{
sum = data[i] + data[j];
if (sum==number || sum2==number) {
return 1;
}
}
}
return 0;
}
int inarray(int data[], int size, int number)
{
int i;
for (i = 0; i < size; i++)
{
if (number == data[i]) {
return 1;
}
}
return 0;
}
我还准备了另一组代码来解释您的错误。希望对您学习有帮助
#include<stdio.h>
int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size=7,sum2; // Size has be changed from 3 to 7 , since you want to check the for 7 numbers
int main()
{
int data[7];
int i, number=0;
printf("Enter 7 numbers: ");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);
//I am a little confused about this part. I wasn't sure how to write it.
// while(inarray(data, size, number)==0 && issumof2(data, size, number)==0)//till functions are false
// I feel its best to check the followin way
// If any one of the condition is true, then that is not the required number, so increment it
// else leave the loop which is the required number
while(inarray(data, size, number)==1 || issumof2(data, size, number)==1)//till functions are false
{
number=number+1;
//printf("Smallest positive integer: %d\n", number);
// You will print the required number after the loop, not inside the loop
}
printf("Smallest positive integer: %d\n", number);
return 0;
}
int issumof2(int data[], int size, int number)
{
int i, j, sum = 0;
for (i = 0; i < size; i++)
{
sum2 =data[i]+data[i];
for (j = 0; j < size; j++)
{
sum = data[i] + data[j];
// if (sum==data[i] || sum2==data[i])
if (sum==number || sum2==number) // You are supposed to check the Sum with Number. Not with data irself
return 1;
// else
// return 0; // Returning 0 here is not correct, It should be returned if all the condition are failed.
}
}
return 0; // Correct place to return 0;
}
int inarray(int data[], int size, int number)
{
int i;
for (i = 0; i < size; i++)
{
// if (i == data[i])
if (i == number) // You are supposed to check with numbner not with data itself
return 1;
// else
// return 0; // You are not supposed to return 0 here. Return is after all the condition is failed
}
return 0; // Corect place to return 0
}
答案 2 :(得分:0)
我编译并运行了您的代码。它会打印,但会成千上万。
$ gcc code.c
$ ./a.out
>Enter 7 numbers: 1 2 3 4 5 6 7 --press enter here--
...
如果我在输入单个数字后按Enter,则可以稍微重现您的问题。确保您输入的内容是一行。