Homework 1: I/O for Q5
Hi all,
I see that some of you have been having trouble with the I/O routines for question 5. I’m attaching here two routines that I know to work (for C and C++). In the comments to the homework 1 post, some people have also posted other examples that work.
I don’t see an example for Java, so if you have one and don’t mind sharing, email it to me and I’ll add it to this post (or post it in the comments and I’ll lift it to the post). Only post things that you know work.
C/C++ Code using scanf (two flavors):
int main ()
{
int left = 0;
int right = 0;
while (scanf("%d%d", &left, &right) != EOF)
{
cout << left << " " << right << " " << findMaxCycleLength(left, right)<< endl;
}
}
or…
int main(int argc, char **argv)
{
int lo, hi;
while( scanf(”%d%d”, &lo,&hi) != EOF ) {
printf(”%d %d %d\n”, lo, hi, FindMax(lo,hi));
}
}
Code using cin (from Mike Clark):
int one, two;
while(cin>>one>>two) {
// logic goes here
}
Java code (from John Meier):
Input…
Scanner inputScanner = new Scanner(System.in);
while (inputScanner.hasNext()) {
run(inputScanner.nextLong(), inputScanner.nextLong());
}
and output…
//Output the result
System.out.format("%d %d %d\n", i_orig, j_orig, max_cycle_length);
.