首页 > 解决方案 > a.exe 不会在 2 行后接受输入

问题描述

我对 c++ 有点陌生,想用它来解决问题,但我发现输入不会超过 2 行。有人可以帮我吗?输入格式在这里http://www.usaco.org/index.php?page=viewproblem2&cpid=568。它应该读取多行输入来解决问题。

例如,请参阅下面的输入

输入示例:

3 3
40 75
50 35
10 45
40 76
20 30
40 40

当我将其粘贴到控制台中,期望代码给出答案时,控制台在第二行的 75 之后切断。我希望将整个内容输入到代码中,但只输入前两行。

#include <bits/stdc++.h>
using namespace std;

int cow[100];
int limit[100];
int N, M;
int currMile = 0;

int main() {
    cin >> N >> M;
    for(int x = 0; x < N; x++) {
        int len, speed;
        cin >> len >> speed;
        for(int x = currMile; x < len + currMile; x++) {
            limit[x] = speed;
            currMile += 1;
        }
    }
    currMile = 0;
    for(int j = 0; j < M; j++) {
        int len, speed;
        cin >> len >> speed;
        for(int i = currMile; i < len + currMile; i++) {
            cow[i] = speed;
            currMile += 1;
        }
    }
    int max = 0;
    for(int h = 0; h < 100; h++) {
        if(cow[h] - limit[h] > max) {
            max = cow[h] - limit[h];
        }
    }
    cout << max;
    return 0;
}

标签: c++

解决方案


不知道你在问什么。你想让你的程序循环吗?

while (cin >> N >> M)
{
  // rest of your code
}

推荐阅读