I'm trying to read formatted inputs from stdin using Scala:
The equivalent C++ code is here:
int main() {
int t, n, m, p;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> n >> m >> p;
vector<Player> players;
for (int j = 0; j < n; ++j) {
Player player;
cin >> player.name >> player.pct >> player.height;
players.push_back(player);
}
vector<Player> ret = Solve(players, n, m, p);
cout << "Case #" << i + 1 << ": ";
for (auto &item : ret) cout << item.name << " ";
cout << endl;
}
return 0;
}
Where in the Scala code, I'd like to Use
players: List[Player], n: Int, m: Int, p: Int
to store these data.
Could someone provide a sample code?
Or, just let me know how to:
- how the "main()" function work in scala
- read formatted text from stdin
- efficiently constructing a list from inputs (as list are immutable, perhaps there's a more efficient way to construct it? rather than having a new list as each element comes in?)
- output formatted text to stdout
Thanks!!!
java.util.Scanner3. basically,ListBuffer, assuming you're using imperative style, which is probably best for this type of thing. Otherwise (since you tagged this "functional-programming"), you can use folds or recursion. HoweverVectormay be more suitable thanListif you're appending.