forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.1.cpp
More file actions
35 lines (34 loc) · 683 Bytes
/
13.1.cpp
File metadata and controls
35 lines (34 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
using namespace std;
void printLastKLines(ifstream &fin, int k){
string line[k];
int lines = 0;
string tmp;
while(getline(fin, tmp)){//&& !fin.eof()
line[lines%k] = tmp;
++lines;
}
// while(!fin.eof()){
// getline(fin, line[lines%k]);
// ++lines;
// }
int start, cnt;
if(lines < k){
start = 0;
cnt = lines;
}
else{
start = lines%k;
cnt = k;
}
for(int i=0; i<cnt; ++i)
cout<<line[(start+i)%k]<<endl;
}
int main(){
ifstream fin("13.1.in");
int k = 4;
printLastKLines(fin, k);
fin.close();
return 0;
}