careercup-C和C++
13.1 用C++写个方法,打印输出文件的最后K行。
解答:
一种方法是打开文件两次,第一次计算文件的行数N,第二次打开文件,跳过N-K行, 然后开始输出。如果文件很大,这种方法的时间开销会非常大。
我们希望可以只打开文件一次,就可以输出文件中的最后k行。 我们可以开一个大小为k的字符串数组,然后将文件中的每一行循环读入。 怎么样循环读入呢?就是将k行字符串保存到字符串数组后, 在读第k+1时,将它保存到数组的第1个元素,依次类推。这样一来, 实际上文件中第i行的字符串会被保存到数组的第i%k的位置。最后当文件读完时, 数组保存的就是最后k行的字符串。当然了,如果读入的行数大于K行,它的开始位置不是0,而是i%k,否则开始位置位0.
C++实现代码:
#include<fstream>
#include<iostream>
#include<string>
using namespace std; void printLast10Lines(char *filename)
{
ifstream in(filename);
const int K=;
string L[K];
string tmp;
int line=;
int count;
while(getline(in,tmp))
{
L[line%K]=tmp;
line++;
}
if(line<K)
{
count=line;
line=;
}
else
{
count=K;
line=line%K;
}
for(int i=;i<count;i++)
cout<<L[(line+i)%K]<<endl;
} int main()
{
printLast10Lines("13.1.cpp");
}
careercup-C和C++的更多相关文章
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...
- [CareerCup] 18.11 Maximum Subsquare 最大子方形
18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an ...
- [CareerCup] 18.10 Word Transform 单词转换
18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...
- [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数
18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...
- [CareerCup] 18.8 Search String 搜索字符串
18.8 Given a string s and an array of smaller strings T, design a method to search s for each small ...
- [CareerCup] 18.7 Longest Word 最长的单词
5.7 Given a list of words, write a program to find the longest word made of other words in the list. ...
- [CareerCup] 18.6 Smallest One Million Numbers 最小的一百万个数字
18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume t ...
- [CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离
18.5 You have a large text file containing words. Given any two words, find the shortest distance (i ...
随机推荐
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- 如何在CentOS 5.x 中安装Windows Azure Linux Agent (WALA)
Qing Liu Tue, Mar 10 2015 3:06 AM 在今天的这一个章节中,我们主要讨论在CentOs 5.x 中如何安装Windows Azure Linux Agent 2.11 ...
- PostgreSql与sqlserver对比杂记
PostgreSql与MSSqlServer区别 增删查改没有语法一样. 排序Group Having 聚集函数使用一样 联结查询 ON 子句是最常见的连接条件的类型:它接收一个和 WHERE 子句相 ...
- NGINX(七)分段下载
前言 nginx分段下载通过ngx_http_range_filter_module模块进行处理,关于HTTP分段下载过程,可以参考HTTP分段下载一文,主要分为一次请求一段和一次请求多段 涉及数据结 ...
- Python脚本控制的WebDriver 常用操作 <二十二> 处理alert / confirm / prompt
测试用例场景 webdriver中处理原生的js alert confirm 以及prompt是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confirm/prom ...
- oracle修改process和session数
第一步:连接服务器,输入sqlplus 第二步:以sysdba身份登陆 第三步:查看和修改processes和sessions参数 1. 查看processes和sessions参数 select * ...
- POJ 1113&&HDU 1348
题意:凸包周长+一个完整的圆周长.因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 模板题,之前写的Graham模板不对,WR了很多发....POJ上的AC代码 #includ ...
- (Trie) uvalive 3942 Remember the word
题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串. 思路:字典树(显然)+DP DP: dp[i]+=dp[j+1] i<=j<=m-1,且i到j的字符串能在字典树中找到.也就 ...
- DateTime和DateTime2
1.与ANSI和ISO8601标准的一致性不同 datetime不符合该标准,datetime2符合该标准.对于新的应用,尽量使用符合标准的类型. 2.表示范围的不同 datetime:1753-01 ...
- Solution for Latex error: "Cannot determine size of graphic"
I'm trying to include graphics in my Latex-file, which I compiled with latex+dvipdf on OS X. Latex h ...