圆整数数组中最接近的大量 [英] Round integer to nearest high number in array

查看:104
本文介绍了圆整数数组中最接近的大量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有整数像这样的数组:[32,128,1024,2048,4096]

I have an array of ints like this: [32,128,1024,2048,4096]

给定一个特定的值,我需要得到的最接近的值数组,它是等于或高于该值。

Given a specific value, I need to get the closest value in the array that is equal to, or higher than, the value.

我有以下代码

  private int GetNextValidSize(int size, int[] validSizes)
  {

      int returnValue = size;

      for (int i = 0; i < validSizes.Length; i++)
      {
          if (validSizes[i] >= size)
          {
              returnValue = validSizes[i];
              break;
          }
      }

      return returnValue;
  }



它的工作原理,但有没有更好的/更快的方式做到这一点?数组永远不会包含超过5-10元以上。

It works, but is there any better/faster way to do it? The array will never contain more than 5-10 elements.

澄清:其实我是想返回原来的值/大小,如果它是更大比任何有效的尺寸。该validSizes阵列可以认为总是进行排序,并且将始终包含的至少一个值

Clarification: I actually want to return the original value/size if it is bigger than any of the valid sizes. The validSizes array can be considered to always be sorted, and it will always contain at least one value.

推荐答案

鉴于有只有5-10元我会认为这是好的。

Given that you have only 5-10 elements I would consider this to be ok.

这篇关于圆整数数组中最接近的大量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆