读取输入错误

时间:2014-11-14 06:44:12

标签: c++ input scanf

我正在关于COdechef比赛问题的NOV14工作。我坚持这个问题。 http://www.codechef.com/NOV14/problems/RBTREE 我的算法运行良好,但我无法正确输入。问题是我不知道给出了多少输入。但我需要存储多个变量。 看看这里.. 5 Qb 4 5 Qr 4 5 Qi Qb 4 5 Qr 4 5

其中5是测试用例的数量, 我可以将每个测试用例读入变量吗? 如果我采取第一个测试用例,我可以将Qb带到一个变量,4个带到另一个变量,5个带到另一个变量。 但问题是如何读取以Qi开头的一行。

2 个答案:

答案 0 :(得分:0)

您需要检查您在每一步中阅读的内容,然后确定是否需要阅读这些数字。

所以阅读两个字符,如果你读过的字符是“Q”和“i”,你不需要阅读任何数字,你可以直接进入下一行。否则,您应该在转到下一行之前阅读这两个数字。

答案 1 :(得分:0)

首先,如果你编写C ++,你应该使用C ++流。这是输入代码(您可以根据自己的需要进行调整):

#include <iostream>
#include <fstream>

int main() {
  std::ifstream file;
  file.open("data.in");
  int lines = 0;
  file >> lines;
  std::string query_type;
  for (int i = 0; i < lines; i++) {
    file >> query_type;
    if (query_type == "Qi") {
      std::cout << query_type << std::endl;
    } else {
      int x = 0;
      int y = 0; 
      file >> x >> y;
      std::cout << query_type << " " << x << " " << y << std::endl;
    }
  }
  file.close();
  return 0;
}
相关问题