Last active
August 29, 2015 14:16
-
-
Save balamark/27cba9d64bf796f6d350 to your computer and use it in GitHub Desktop.
[note] for_each() and getline()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cstdio> //sscanf | |
#include <string> //getline, c_str() | |
#include <iostream> //cin | |
using namespace std; | |
int t, n, l1, l2; | |
int main(){ | |
int i=1; | |
// fill the array with consecutive numbers and print it out | |
for_each(coin_type, coin_type+300, [&i](int &n){n=i++;}); | |
for_each(coin_type, coin_type+300, [](int &n){printf("%d ", n);}); | |
// input could be 1,2 or 3 elements so don't know how to achieve this with scanf("%d") or %s | |
// better to use getline to the the whole line end with \n then split with sscanf | |
string str; | |
while (getline(cin, str)) { | |
n=l1=l2=0; | |
t=sscanf(str.c_str(), "%d %d %d", &n, &l1, &l2); | |
printf("%d %d %d %d\n", t, n, l1, l2); | |
} | |
return 0; | |
} | |
//input output | |
6 2 | |
2 6 2 0 | |
6 2 5 | |
3 6 2 5 | |
6 | |
1 6 0 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment