OJ的文件流操作
2024-10-16 23:31:47
我们刷题的时候除了编码外,测试也是非常重要的,当测试样例比较小的时候,我们完全可以手打,但是当测试样例比较大时候,我们就抓狂了~
相信不少人都知道利用文件流,但是应该还有新手跟我一样,一遍又一遍地输入测试样例~
其实很简单,就两句代码。
#include<cstdio> //包含头文件,c语言的就是stdio.h freopen("in.txt","r",stdin);//在main 函数最开始加入,在当前工程下创建in.txt,之后加入数据 fclose(stdin);//在程序return 0前加入。
当然,如果纯粹是上面的代码,还是很琐碎,需要我们每次提交时候注释掉……
为了方便大家,很多OJ都使用ONLINE_JUDGE宏。
有就是我们这样就可以,在自己测试后,直接提交。
#include<iostream>
#include<cstdio>
using namespace std; int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif #ifndef ONLINE_JUDGE
fclose(stdin);
#endif return 0; }
当然,如果你跟我一样是使用linux+codeblock来写代码的话,我这里还有一个shell脚本,可以自动生成一些每次都必写的东西。
其实我觉得用vim是很好的,但是哎,真的没有IDE,写代码很懊恼……
当然,我写shell是新手,所以大神不要见怪。。
当然,注释断,如果觉得没用就直接删掉吧。
这个是cpp的。
#!/bin/sh
#vi /usr/share/codeblocks/templates/wizard/console/cpp
Curtime=`date "+%Y-%m-%d "`
echo '/*******************************************************************************/
/* OS : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
* Compiler : g++ (GCC) 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
* Encoding : UTF8
* Date :' > /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp $Curtime >> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp
echo ' * All Rights Reserved XXX.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/
' >> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp
echo '#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif #ifndef ONLINE_JUDGE
fclose(stdin);
#endif return 0; } '>> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp echo 'cpp.sh'
exit
这个是C的。
#!/bin/sh
#vi /usr/share/codeblocks/templates/wizard/console/cpp
Curtime=`date "+%Y-%m-%d "`
echo '/*******************************************************************************/
/* OS : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
* Compiler : GCC 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
* Encoding : UTF8
* Date :' > /usr/share/codeblocks/templates/wizard/console/c/main.c $Curtime >> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo ' * All Rights Reserved by yaolong.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/
' >> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo ' #include <stdio.h>
#include <string.h> int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif #ifndef ONLINE_JUDGE
fclose(stdin);
#endif return 0;
}
'>> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo 'c.sh' exit
最新文章
- ASP.NET上实现
- 使用 xlrd 模块实现对excel 的读取、excel转json 、excel 转 mysql insert 语句
- MapReduce几个简单的例子
- Cocos2d-x3.6 Android编译问题
- Linux 安装配置Subversion edge
- Source Insight 中使用 AStyle 代码格式工具
- HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception
- mysql oracle 删除外键约束
- flex渐变色制作圆角橙色按钮
- Android中Broadcast Receiver组件具体解释
- sping 对 hibernate进行事务管理--Annotation, xml, 大多数使用XML
- c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类
- LINQ to Entities 中的查询
- 如何滚动更新 Service?- 每天5分钟玩转 Docker 容器技术(102)
- 第四篇:Web框架 - Django
- java原码、补码、反码总结
- JAVA-类方法与实例方法
- opendaylight-O版本与openstack集成
- jquery $().each,$.each的区别
- 【由浅入深理解java集合】(一)——集合框架 Collction、Map
热门文章
- POJ 3186Treats for the Cows (区间DP)
- java解惑
- [LeetCode] 287. Find the Duplicate Number 解题思路
- 【ACM/ICPC2013】线段树题目集合(一)
- db2 identity列重置,reset/restart
- Oracle DB 自动管理共享内存
- C++的静态分发(CRTP)和动态分发(虚函数多态)的比较
- Quartz定时任务学习(七)Cron 触发器
- linux杂谈(十九):DNSserver的配置(二)
- FastDFS、nginx配置手记