当我为100的输入运行这个程序时,它将输入行修剪到88,并使数组超出绑定异常.i.e。最多需要5个这些。 (50 33 11 27 19 40 13 27 37 45 40)未显示在控制台窗口
using System;
using System.Numerics;
class MyClass {
static void Main(string[] args) {
int T = Convert.ToInt32(Console.ReadLine());
string worth =Console.ReadLine();
string[] worthSplit = new string[T];
int[] worthTransactions = new int[T];
int sum=0;
int Q = Convert.ToInt32(Console.ReadLine());
int[] Target = new int[Q];
for (int i = 0; i < Q; i++)
{
Target[i]=Convert.ToInt32(Console.ReadLine());
}
if (T >= 1 && T <= 100000 && Q >= 1 && Q <= 1000000)
{
worthSplit = worth.Split(' ');
for (int i = 0; i < T; i++)
{
worthTransactions[i] = Convert.ToInt32(worthSplit[i]);
}
for (int j = 0; j < Q; j++)
{
sum = 0;
int k = 0;
while (Target[j] > sum && k<T)
{
sum+=worthTransactions[k];
k++;
}
if (k >= T)
{
Console.WriteLine("-1");
}
else
{
Console.WriteLine(k);
}
}
}
}
}
Input:
100
34 37 28 16 44 36 37 43 50 22 13 28 41 10 14 27 41 27 23 37 12 19 18 30 33 31 13 24 18 36 30 3 23 9 20 18 44 7 12 43 30 24 22 20 35 38 49 25 16 21 14 27 42 31 7 24 13 21 47 32 6 26 35 28 37 6 47 30 14 8 25 46 33 46 15 18 35 15 44 1 38 9 27 29 39 35 4 2 5 50 33 11 27 19 40 13 27 37 45 40
90
2634
2633
2632
2631
2630
2629
2628
2627
2626
2625
2624
2623
2622
2621
2620
2619
2618
2617
2616
2615
2614
2613
2612
2611
2610
2609
2608
2607
2606
2605
2604
2603
2602
2601
2600
2599
2598
2597
2596
2595
2594
2593
2592
2591
2590
2589
2588
2587
2586
2585
2584
2583
2582
2581
2580
2579
2578
2577
2576
2575
2574
2573
2572
2571
2570
2569
2568
2567
2566
2565
2564
2563