我是一名初学程序员,对Common Lisp非常感兴趣,但根本不了解它.我非常感谢能在Common Lisp中实现以下问题的人,所以我可以看到Common Lisp如何处理基本的东西,比如字符串处理,文件I/O等.
这是问题所在:
输入示例,从"problem.in"文件中读取:
3 5
XHXSS
XSHSX
XXHSS
Run Code Online (Sandbox Code Playgroud)
您将得到^ h通过W¯¯字符表.第一个数字是行数,第二个数字是列数.
对于每列字符,您应该执行以下操作:
从头到尾开始查看字符.
如果找到"X",则输出列的成本(默认情况下,成本为零),后跟空格字符,然后转到下一列(跳过当前列中的所有其他字符).
如果找到"S",则将成本增加1.
如果找到"H",则将成本增加3.
如果列中没有"X",则输出"N"后跟空格字符.
输出示例,写入"problem.out"文件:
0 4 0 N 1
Run Code Online (Sandbox Code Playgroud)
这是我在C++中的实现:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
ifstream input;
input.open("problem.in");
ofstream output("problem.out");
int h, w;
input >> h >> w;
string * str = new string[h];
for(int i = 0; i < h; i++) input >> str[i];
for(int i = 0; i < w; i++)
{
int cost = 0;
bool found = false;
for(int j = 0; j < h; j++)
{
char ch = str[j][i];
if(ch == 'X') { found = true; break; }
else if(ch == 'S') cost += 1;
else if(ch == 'H') cost += 3;
}
if(found) output << cost;
else output << 'N';
output << ' ';
}
input.close();
output.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我更希望看到这个问题作为一个函数实现,具体如下:
(defun main()
...
(with-open-file (input "problem.in" :direction :input)
(...))
...
(with-open-file (output "problem.out" :direction :output :if-exists :supersede)
(...))
...
)
Run Code Online (Sandbox Code Playgroud)
(defun solve-problem (in-file out-file)
(labels ((solve (in out)
(let ((h (read in))
(w (read in)))
(compute-output out h w
(read-array in h w (make-array (list h w))))))
(read-array (stream h w array)
(loop for i below h do (read-line stream)
(loop for j below w
do (setf (aref array i j)
(read-char stream))))
array)
(compute-output (stream h w array)
(loop with x-p and cost for j below w do
(setf x-p nil cost 0)
(loop for i below h do
(case (aref array i j)
(#\X (setf x-p t) (return))
(#\S (incf cost))
(#\H (incf cost 3))))
(format stream "~a " (if x-p cost #\N)))))
(with-open-file (in in-file)
(with-open-file (out out-file :direction :output :if-exists :supersede)
(solve in out)))))
CL-USER 17 > (solve-problem "/tmp/test.data" "/tmp/result.text")
NIL
CL-USER 18 > (with-open-file (stream "/tmp/result.text") (read-line stream))
"0 4 0 N 1 "
Run Code Online (Sandbox Code Playgroud)