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 ...
随机推荐
- SCOI2009windy数
数位DP,还不怎么会…… 其中calc函数的计算分为三部分: 第一部分:统计最高位为0的情况,或者说不足最高位位数的数的个数 第二部分:统计最高位为1到a[len]-1的情况,直接调用数组即可 第三部 ...
- python跟踪脚本进度(类似bash-x)
#详细追踪 python -m trace --trace jin.py #显示调用了哪些函数 python -m trace --trackcalls jin.py
- LeetCode Pascal's Triangle II (杨辉三角)
题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...
- 学习面试题Day06
1.字节流的处理方式 字节流处理的是计算机最基本的单位byte,它可以处理任何数据格式的数据.主要的操作对象就是byte数组,通过read()和write()方法把byte数组中的数据写入或读出. 2 ...
- jQuery树叶掉落特效代码
代码使用方法 1.将index.html源文件中的JS设置代码插入到你的网页中 2.将style.css文件内容合并到你的页面样式表中 3.将jquery文件加载到你的页面中,请注意,这不是通用的jq ...
- Android样式——Styles
说明 样式(style)是属性的集合,用来指定View或者Window的外观和格式. 这些属性可以是height(高度).padding(内边距).font size(字体颜色)等. 样式定义在另一个 ...
- HDU 5698 瞬间移动
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.7
Prove that for any vectors $$\bex u_1,\cdots,u_k,\quad v_1,\cdots,v_k, \eex$$ we have $$\bex |\det(\ ...
- 交易策略研究 R库
本文在Creative Commons许可证下发布 交易策略研究 R库,直接安装:xts, TTR,quantmod,RTAQ,PerformanceAnalytics,FactorAnalytics ...
- LeetCode题解——Reverse Integer
题目: 数字翻转,即输入123,返回321:输入-123,返回-321. 代码: class Solution { public: int reverse(int x) { , sign = ; ) ...