我有两个字符串数组
string[] input; //user input could be any size
string[] output; //a copy of user input but, should not be larger than 50
如果输入长度 <= 50,则输出是输入的精确副本。
如果输入数组长度 > 50 那么它只会从输入中复制 50 个元素
它必须来自input
的first 和last 元素并选择其余部分均匀;它不仅仅是取前 50 个元素或后 50 个元素。
执行此操作最有效的方法是什么?
更新 假设 input[] 有 98 个元素。然后你将取第一个和最后一个元素,然后将其余元素除以 2 得到 50 个元素
98-2=96
96/2=48
2+48=50
最佳答案
for (float i = 0, int count = 0; count < 50; i+= arraySize / 50.0f, count++)
{
output[count] = input[(int)i];
}
关于c# - 如何制作数组大小上限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/854371/