我如何从六位或更多位数找到最大数字?

时间:2010-03-18 04:58:16

标签: iphone

我有代码,我想从六个数字中找出最大的数字,代码如下,

                 if( (pixels->r == 244 || pixels->g == 242 || pixels->b == 245) || (pixels->r == 236 || pixels->g == 235 || pixels->b == 233) || (pixels->r == 250 || pixels->g == 249 || pixels->b == 247) || (pixels->r == 253 || pixels->g == 251 || pixels->b == 230) || (pixels->r == 253 || pixels->g == 246 || pixels->b == 230) ||  (pixels->r == 254 || pixels->g == 247 || pixels->b == 229))  
                {
                    numberOfPixels1++;
                    NSLog( @"Pixel data1 %d", numberOfPixels1);
                }
                if( (pixels->r == 250 || pixels->g == 240 || pixels->b == 239) ||(pixels->r == 243 || pixels->g == 234 || pixels->b == 229) || (pixels->r == 244 || pixels->g == 241 || pixels->b == 234) || (pixels->r == 251 || pixels->g == 252 || pixels->b == 244) || (pixels->r == 252 || pixels->g == 248 || pixels->b == 237) ||  (pixels->r == 254 || pixels->g == 246 || pixels->b == 225))  
                {
                    numberOfPixels2++;
                    NSLog( @"Pixel data2 %d", numberOfPixels2);                 }
                if( (pixels->r == 255 || pixels->g == 249 || pixels->b == 225) ||(pixels->r == 255 || pixels->g == 249 || pixels->b == 225) || (pixels->r == 241 || pixels->g == 231 || pixels->b == 195) || (pixels->r == 239 || pixels->g == 226 || pixels->b == 173) || (pixels->r == 224 || pixels->g == 210 || pixels->b == 147) ||  (pixels->r == 242 || pixels->g == 226 || pixels->b == 151))  
                {
                    numberOfPixels3++;
                    NSLog( @"Pixel data3 %d", numberOfPixels3);
                }

              if( (pixels->r == 235 || pixels->g == 214 || pixels->b == 159) ||(pixels->r == 235 || pixels->g == 217 || pixels->b == 133) || (pixels->r == 227 || pixels->g == 196 || pixels->b == 103) || (pixels->r == 225 || pixels->g == 193 || pixels->b == 106) || (pixels->r == 223 || pixels->g == 193 || pixels->b == 123) ||  (pixels->r == 222 || pixels->g == 184 || pixels->b == 119))  
                {
                    numberOfPixels4++;
                    NSLog( @"Pixel data4 %d", numberOfPixels4);
                }
                if( (pixels->r == 199 || pixels->g == 164 || pixels->b == 100) ||(pixels->r == 188 || pixels->g == 151 || pixels->b == 98) || (pixels->r == 156 || pixels->g == 107 || pixels->b == 67) || (pixels->r == 142 || pixels->g == 88 || pixels->b == 62) || (pixels->r == 121 || pixels->g == 77 || pixels->b == 48) ||  (pixels->r == 100 || pixels->g == 49 || pixels->b == 22))  
                {
                    numberOfPixels5++;
                    NSLog( @"Pixel data5 %d", numberOfPixels5);
                }
                if( (pixels->r == 101 || pixels->g == 48 || pixels->b == 32) ||(pixels->r == 96 || pixels->g == 49 || pixels->b == 33) || (pixels->r == 87 || pixels->g == 50 || pixels->b == 41) || (pixels->r == 64 || pixels->g == 32 || pixels->b == 21) || (pixels->r == 49 || pixels->g == 37 || pixels->b == 41) ||  (pixels->r == 27 || pixels->g == 28 || pixels->b == 46))  
                {
                    numberOfPixels6++;
                    NSLog( @"Pixel data6 %d", numberOfPixels6);
                }

我必须从上面的代码中找出numberOfPixels1 .... numberOfPixels6中的最大值。 找出最大的数字有什么最佳方法吗?

2 个答案:

答案 0 :(得分:2)

你只有六个值,所以在这种情况下,存储最大值的for循环应该可以很好地运行

伪代码::

for x in numberOfPixels1...numberOfPixels6 do
    if x > tmp then
        tmp = x
    end
end

答案 1 :(得分:1)

这里看不到任何图案,抱歉。也许,如果你将RGB转换为其他color space,这可以更简单吗?

相关问题