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++的更多相关文章

  1. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  2. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  3. [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 ...

  4. [CareerCup] 18.11 Maximum Subsquare 最大子方形

    18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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. ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. Bad Request (Invalid Hostname)解决方法

    当在Windows Server 2003+IIS6做Web服务器,出现打开如http://paullevi.oicp.net,出现,Bad Request (Invalid Hostname) 的提 ...

  2. 有关SQL

    1.SQL str函数是什么意思? 将数值型转换成指定长度的字符串.str()函数语法:str(数字类型的表达式[,表达式总长度][,小数点后面的位数]),表达式总长度和小数点后面的位数为可选择参数. ...

  3. python运算符的优先级

    运算符优先级 如果你有一个如2 + 3 * 4那样的表达式,是先做加法呢,还是先做乘法?我们的中学数学告诉我们应当先做乘法——这意味着乘法运算符的优先级高于加法运算符. 下面这个表给出Python的运 ...

  4. TCP 3次握手和四次挥手

    1.标示符说明    位码即tcp标志位: SYN(synchronous建立联机)   ACK(acknowledgement 确认)  PSH(push传送)    FIN(finish结束)   ...

  5. bzoj 3295 [Cqoi2011]动态逆序对(cdq分治,BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3295 [题意] n个元素依次删除m个元素,求删除元素之前序列有多少个逆序对. [思路] ...

  6. input file里的JQ change() 事件的只生效一次

    文件选择框的onchange事件只在第一次改变时生效,以后再选择文件不会触发onchange事件. 解决方法1:用jQuery的live代替直接使用change. 错误代码: $("#Upl ...

  7. 在Visual Studio中利用NTVS创建Pomelo项目

    刚看新闻,才知道微软发布了Node.js Tools for Visual Studio(NTVS),受够了WebStorm输入法Bug的困扰,这下终于可以解脱了.以Pomelo为例,运行命令:pom ...

  8. [Tool] 使用CodeMaid自動程式排版 - 摘自网络

    前言 「使用StyleCop驗證命名規則」這篇文章,指引開發人員透過StyleCop這個工具,來自動檢驗專案中產出的程式碼是否合乎命名規則. [Tool] 使用StyleCop驗證命名規則 但是在專案 ...

  9. adb不响应

    1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr "5037" .然后会弹出 ...

  10. 问题-在TreeView使用时,发现选中的树节点会闪烁或消失

    问题:在工程中选中一个树节点,鼠标焦点在树上,做某种操作时发现选中的点会消失?原因:如果只是BeginUpdate后,没有调用EndUpdate,树会全空.应该是BeginUpdate方法会刷新树,但 ...