0

我正在尝试获取 3 个数组并添加每个元素组合以查看是否有任何等于 91,并且必须确保结果的红色值小于蓝色值小于绿色值。我一生都无法想到如何存储这么多实例,然后在最后引用它们。

如您所见,我缺少代码,因为我不知道如何继续。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(void) {

int i = 0;
int j = 0;
int k = 0;
int redSize = 6;
int blueSize = 9;
int greenSize = 9;
int red[] = (9, 22, 21, 18, 34, 13);
int blue[] = (20, 60, 14, 17, 39, 16, 6, 33, 18);
int green[] = (40, 7, 51, 26, 8, 24, 12, 11, 27);
for(i; i < redSize; i++){

    result = red[i] + blue[j] + green[k] 

    while (j < blueSize){

        // need to iterate over each item in array "blue"

        while(k < greenSize){
          //same for green

        } 

    }

    if (result == 91 && red[i] < blue[i] && blue[i] < green[i]){
    printf("The red value is %d, the blue value is %d, and the green value is %d", red[i], blue[i], green[i]);  


    }   

return 0;
}   

}
4

5 回答 5

3

你没那么远。一些小故障

  • 数组初始化是用 {} 而不是 ()
  • 您想要所有组合,因此需要 3 个嵌套循环:一个用于红色(i),一个用于蓝色(j),一个用于绿色(k)。如您所见,对于每个 red[i],您都执行 blue[j],对于每个 red[i],您都执行 green[k]。你计算i*j*k元素。
  • 测试将在嵌套循环的最内部中心执行,此时红色、蓝色和绿色已知(即 i、j 和 k 具有值)。

例如:(我只是修复了你的程序,没有优化任何东西)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(void){


int i = 0;
int j = 0;
int k = 0;
int redSize = 6;
int blueSize = 9;
int greenSize = 9;
int red[] = {9, 22, 21, 18, 34, 13};
int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};

for(i=0 ; i < redSize; i++){
  for(j=0 ; j < blueSize; j++){
    for(k=0 ; k < greenSize; k++){

      int result = red[i] + blue[j] + green[k];
      if (result == 91 && red[i] < blue[j] && blue[j] < green[k]){
        printf("The red value is %d, the blue value is %d, and the green value is %d\n", red[i], blue[j], green[k]);  
      }   

    }
  }
}

return 0;
}   
于 2013-01-16T06:29:39.993 回答
2

注意:您的数组声明错误,循环范围不正确,循环组织可以通过测试小于条件并消除整个子循环来允许大量跳过。对于已知不小于当前绿色的红色值,无需进行所有绿色与蓝色的比较。

算法:

for each red value
   for each blue value "greater" then the current red value
      for each green value "greater" than the current blue value
           if (red+blue+green)=91, the trio is a candidate.

扔掉很多杂物后,它应该看起来像这样:

#include<stdio.h>

int main(void)
{
    int red[] = {9, 22, 21, 18, 34, 13};
    int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
    int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
    int i,j,k;

    for(i=0;i<sizeof(red)/sizeof(red[0]); ++i)
    {
        for (j=0;j<sizeof(blue)/sizeof(blue[0]);++j)
        {
            if (red[i] < blue[j])
                for (k=0;k<sizeof(green)/sizeof(green[0]);++k)
                {
                    if (blue[j] < green[k] && red[i]+blue[j]+green[k]==91)
                        printf("red:%d blue:%d green:%d\n", red[i], blue[j], green[k]);
                }
        }
    }

    return 0;
}

输出

red:18 blue:33 green:40
于 2013-01-16T06:30:20.913 回答
1

或者我们可以通过在绿色循环中检查特定值来避免每次添加。

#include<stdio.h>

int main(void)
{
    int red[] = {9, 22, 21, 18, 34, 13};
    int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
    int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
    int i = 0, j = 0, k = 0;
while(i < redSize){
      while (j < blueSize){
             kValue = 91 - red[i] - blue[j];
             while(k < greenSize){
                  if(green[k] == kValue && red[i] < blue[j] && blue[j] < green[k])
                  {
                      printf("The red value is %d, the blue value is %d, and the green value is %d", red[i], blue[j], green[k]); 
                  }
                  k++;
                }
                j++;
            }
            i++;
        }
        return 0;
}
于 2013-01-16T06:36:23.797 回答
1

根据您对问题的简短介绍,我认为这可能会对您有所帮助,如果不是您真正需要的,请发表评论。

for(i=0 to redSize){
 for(j=0 to blueSize){
  for(k=0 to greenSize){
   if(red[i]<blue[j]<green[k]){
    if(91 == red[i]+blue[j]+green[k]){
       printf(red[i],blue[j],green[k]);
    }
   }
  }
 }
}
于 2013-01-16T06:38:02.320 回答
1
#include <stdio.h>

int main(void) {

    int red[] = {9, 22, 21, 18, 34, 13};
    int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
    int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};

    int r, g, b;

    for (r = 0; r < (sizeof(red) / sizeof(int)); ++r) {
        for (g = 0; g < (sizeof(green) / sizeof(int)); ++g) {
            for (b = 0; b < (sizeof(green) / sizeof(int)); ++b) {
                int s = red[r] + green[g] + blue[b];
                if (s == 91 && red[r] < blue[b] && blue[r] < green[g]) {
                    printf("%d %d %d", red[r], blue[b], green[g]);
                }
            }
        }
    }

    return 0;
}
于 2013-01-16T06:30:02.410 回答