先复习一下C的一些基本概念

1、C标准化输出:scanf

int m,n;
scanf("%d%d",&n,&m);

实际上scanf是有返回值的,且返回值的类型为int,为输入的个数。如:

int m,n;
printf("%d", scanf("%d%d",&n,&m) ); //输入 12 56
//输出 2 //输入 2 a a输入失败
//输出 1 //输入 a 5 a输入失败,则后面的也失败,故输出为0
//输出 0

scanf还有一个返回值EOF(即-1,符号常量),代表输入数据已经结束,如:

#include <iostream>
using namespace std; int main()
{
int a,b;
while(scanf("%d%d",&a,&b) != EOF){
printf("%d\n",a+b);
}
return ;
}

在Windows下,按Ctrl+z,再按回车即可结束输入。

2、C++的标准化输出:cin

cin表达式的值,只能为true(成功读入所有变量) 和false

对应的一直输入为:

#include <iostream>

using namespace std;

int main()
{
int a,b;
while(cin>>a>>b) // 注意这里不能加;否则不执行后面的
{
cout << a+b << endl;
}
return ;
}

Windows停止同按Ctrl+z  回车

例如:输入若干个(不知道多少个)正整数,输出其中的最大值

#include <iostream>
using namespace std; int main()
{
int a,mx=;
while(scanf("%d",&a) != EOF){
if (a>mx){
mx = a;
}
printf("%d\n",mx);
}
return ;
}

3、用freopen重定向输入

调试程序时,每次运行程序都要输入测试数据,太麻烦

可以将测试数据存入文件,然后用freopen将输入由键盘重定向为文件,则运行程序时,就不需要输入数据了。:

#include <iostream>
using namespace std; int main()
{
freopen("E:\\CodeBlocks\\Project_\\POJ\\test1\\input.txt","r",stdin); // \\为转义\ 且注意交到oj上的时候注意把它注释掉
// 此后所有输入都来自文件 input.txt
int a,mx=;
while(scanf("%d",&a) != EOF){
if (a>mx){
mx = a;
}
}
printf("%d\n",mx);
return ;
}

input文件为:

运行结果为:

POJ 入门的更多相关文章

  1. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  2. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  3. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  4. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  5. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  6. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  7. POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道

    rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  8. POJ 1579 Function Run Fun 【记忆化搜索入门】

    题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  9. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. Html中编码格式的设置,及引入javascript、css的方法

    1.html页面中设置字符编码的格式: <meta http-equiv="Content-Type" content="text/html; charset=ut ...

  2. delphi -----TListView的用法

    层次关系: TListView:           ->Columns:           ->Items  : -->TListItems:                   ...

  3. Phonetic Symbols:2个半元音:[w] ,[j]

    2个半元音音标发音技巧与单词举例 原文地址:http://www.hlyy.in/1243.html 2个半元音音标发音技巧与半元音单词举例 [w]  发音技巧: 嘴唇张开到刚好可以含住一根吸管的程度 ...

  4. junit test 报错,java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=esopCreateTest],

    java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=esopCreateTest], {ExactMatc ...

  5. SQL Server 2005 临时表

    with t as ( select * from t_pub_param ) select * from t SQL Server 2005 之后才可以使用,查询后临时表t会自动删除.

  6. Nginx+Springboot+Vue 前后端分离 解决跨域问题

    1:前端vue 写完 打包 npm run build prod 2: 后端api 写完打包 springboot mvn package -Dmaven.test.skip=true 3: ngin ...

  7. Convolutional Neural Networks for Visual Recognition

    http://cs231n.github.io/   里面有很多相当好的文章 http://cs231n.github.io/convolutional-networks/ Table of Cont ...

  8. Windows Server 2012 云硬盘如何挂载

    那么首先科普一下,云服务器的数据盘(也就是我们买的云硬盘)默认是脱机状态,不自动挂载的.下面来教大家win2012环境如何挂载硬盘,其实和03.08的大同小异就是入口不同了.   点击“工具”中的“计 ...

  9. oradebug工具使用(转载)

    在之前的HangAnalyze 中有使用oradebug命令,在这篇文章里,我们主要是重点看一下这个oradebug命令: Oracle HANGANALYZE 功能诊断 DB hanging htt ...

  10. ssh远程执行远程执行命令

    ssh zxrac1 '/home/oracle/tmp/t5.sh 2 2 >/dev/null 2>&1 &'