根据文本文件中最后一行的数字将前面的内容重复多遍
例题描述和简单分析
有 txt 文件 txt.txt,如下:
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
3
某文本文件共 N+1 行,其中第 N+1 行是个数字 M,比如 3。现在要据此生成新文件,将 N 行重复 M 遍,即共 N*M 行,结果如下:
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
a brown frog leaps over a lazy dog
the dog then chases it's own tail
the tail is long and brown
the brown frog goes for a swim
解法及简要说明
在集算器中编写脚本 p1.dfx,如下所示:
A |
|
1 |
=file("txt.txt").import@i() |
2 |
=A1.m(:-2)*int(A1.m(-1)) |
3 |
=file("result.txt").export(A2) |
简要说明:
A1 读取 txt 数据,@i 表示结果集只有 1 列时返回成序列
A2 把除去最后一个成员的序列复制最后一个成员的整数份
A3 复制后的结果导出至 result.txt
JAVA 集成这段代码的方法可参考:《Java 如何调用 SPL 脚本》。
应该写回文件
英文版