POJ在评阅习题时需要向程序提供输入数据,并获取程序的输出结果。因此提交的程序需按照每个习题具体的输入输出格式要求处理输入输出。有的时候,测评系统给出程序的评判结果是“数据错误”或“结果错误”,有可能就与没有正确使用输入输出格式有关。

POJ要求的输出一般有3种情况。

(1)输出一个数据:数据后加换行。

(2)输出一行数据:数据间用一个空格间隔(或指定的间隔符),行尾加换行(换行前可有一个空格)。

(3)输出多行数据:每行的数据间用一个空格间隔(或指定的间隔符),行尾只加换行。

除了少数题目没有输入数据的要求外(例如POJ 1316 Self Numbers),POJ的题目一般要求处理多组测试数据,其数据输入形式一般有如下2种情况。

1.给定测试数据的组数

测试数据为多行。第一行是测试数据的数目t。以下的t行给出待处理的数据。针对这种输入情况,程序的框架一般为:

cin>>nCase;

while (nCase--)

{

//  按要求输入每组测试数据并进行处理

}

【例1】Eva's Problem (POJ 1658)

Description

Eva的家庭作业里有很多数列填空练习。填空练习的要求是:已知数列的前四项,填出第五项。因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习。

Input

第一行是数列的数目t(0 <= t <= 20)。以下每行均包含四个整数,表示数列的前四项。约定数列的前五项均为不大于10^5的自然数,等比数列的比值也是自然数。

Output

对输入的每个数列,输出它的前五项。

Sample Input

2

1 2 3 4

1 2 4 8

Sample Output

1 2 3 4 5

1 2 4 8 16

(1)编程思路。

简单地采用a、b、c、d、e五个变量来保存数列的前5项。输入前4项后,若b-a == c-b && c-b==d-c,则数列为等差数列,e= d + c-b;否则,数列为等比数列,e = c*d/b。

(2)源程序。

#include <iostream>

using namespace std;

int main()

{

int t, a,b,c,d,e;

cin >> t;

while (t--)

{

cin >> a >> b >> c >> d;

if (b-a == c-b && c-b==d-c)

{

e = d + c-b;

cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;

}

else

{

e = c*d/b;

cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;

}

}

return 0;

}

当然,有的POJ题目中要求处理的测试数据就只有一组,例如POJ 1077 Eight。这种情况,直接按要求输入测试数据,进行处理即可。

2.以特定的数据结束输入。

输入包含多组测试数据。每组测试数据占据独立一行。以特定的数据(比如0,或者一个负数)表示输入结束。针对这种输入情况,以每组测试为1个数,且以0作为结束为例,程序的框架一般为:

while (cin>>n && n!=0)

{

//  按要求输入每组测试数据并进行处理

}

【例2】Sum of Factorials (POJ 1775)

Description

John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science, computers, and game theory. He was noted for a phenomenal memory and the speed with which he absorbed ideas and solved problems. In 1925 he received a B.S. diploma in chemical engineering from Zurich Institute and in 1926 a Ph.D. in mathematics from the University of Budapest. His Ph.D. dissertation on set theory was an important contribution to the subject. At the age of 20, von Neumann proposed a new definition of ordinal numbers that was universally adopted. While still in his twenties, he made many contributions in both pure and applied mathematics that established him as a mathematician of unusual depth. His Mathematical Foundations of Quantum Mechanics (1932) built a solid framework for the new scientific discipline. During this time he also proved the mini-max theorem of GAME THEORY. He gradually expanded his work in game theory, and with coauthor Oskar Morgenstern he wrote Theory of Games and Economic Behavior (1944).

There are some numbers which can be expressed by the sum of factorials. For example 9,9=1!+2!+3! Dr. von Neumann was very interested in such numbers. So, he gives you a number n, and wants you to tell him whether or not the number can be expressed by the sum of some factorials.

Well, it's just a piece of cake. For a given n, you'll check if there are some xi, and let n equal to Σ1<=i<=txi!. (t >=1 1, xi >= 0, xi = xj iff. i = j). If the answer is yes, say "YES"; otherwise, print out "NO".

Input

You will get several non-negative integer n (n <= 1,000,000) from input file. Each one is in a line by itself.

The input is terminated by a line with a negative integer.

Output

For each n, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.

Sample Input

9

-1

Sample Output

YES

(1)编程思路。

由于题目要求处理的数据范围为n <= 1,000,000,而10!=3628800>1000000,因此,可以定义一个数组int table[11]预先保存好0!~10!的值。

针对每个输入的测试数据n,用循环

for (i =10; i>=0; i--)

if (table[i]<=n)  n=n-table[i];

进行处理,若n==0,则n可以表示为几个数的阶乘和,输出“YES”。

(2)源程序。

#include <iostream>

using namespace std;

int main()

{

int n,i;

int table[11]={1,1,2,6,24,120,720,5040,40320,362880,3628800};

while (cin>>n && n>=0)

{

if(n == 0)

{

cout<<"NO"<<endl;

continue;

}

for (i =10; i>=0; i--)

{

if (table[i]<=n)  n=n-table[i];

if (n==0)   break;

}

if (i>=0)  cout<<"YES"<<endl;

else   cout<<"NO"<<endl;

}

return 0;

}

POJ数据的输入输出格式的更多相关文章

  1. [ACM训练] ACM中巧用文件的输入输出来改写acm程序的输入输出 + ACM中八大输入输出格式

    ACM中巧用文件的输入输出来改写acm程序的输入输出 经常有见大神们使用文件来代替ACM程序中的IO,尤其是当程序IO比较复杂时,可以使自己能够更专注于代码的测试,而不是怎样敲输入. C/C++代码中 ...

  2. 【C语言入门教程】3.2 数据的输入 与 输出

    在程序的运行过程中,通常需要用户输入一些数据,而程序运算所得到的计算结果等又需要输出给用户,由此实现人与计算机之间的交互.所以在程序设计中,输入输出语句是一类必不可少的重要语句.在 C 语言中,没有专 ...

  3. C语言第一次作业——输入输出格式

    题目1温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码 #include& ...

  4. hdu ACM Steps Section 1 花式A+B 输入输出格式

    acm与oi很大的一个不同就是在输入格式上.oi往往是单组数据,而acm往往是多组数据,而且题目对数据格式往往各有要求,这8道a+b(吐槽..)涉及到了大量的常用的输入输出格式.https://wen ...

  5. Hadoop(七):自定义输入输出格式

    MR输入格式概述 数据输入格式 InputFormat. 用于描述MR作业的数据输入规范. 输入格式在MR框架中的作用: 文件进行分块(split),1个块就是1个Mapper任务. 从输入分块中将数 ...

  6. Hadoop学习之常用输入输出格式总结

    目的 总结一下常用的输入输出格式. 输入格式 Hadoop可以处理很多不同种类的输入格式,从一般的文本文件到数据库. 开局一张UML类图,涵盖常用InputFormat类的继承关系与各自的重要方法(已 ...

  7. C语言输入输出格式符

    C语言输入输出格式符 printf函数(格式输出函数) 1.一般格式 printf(格式控制,输出表列) 例如:printf("i=%d,ch=%c\n",i,ch); 说明: ( ...

  8. PAT IO-04 混合类型数据格式化输入(5)

    /* *PAT IO-04 混合类型数据格式化输入(5) *2015-08-01 作者:flx413 */ #include<stdio.h> int main() { int a; fl ...

  9. IO-04. 混合类型数据格式化输入

    /** *A4-IO-04. 混合类型数据格式化输入 *C语言实现 *测试已通过 */ #include "stdio.h" int main() { float m1,m2; i ...

随机推荐

  1. POJ3020 Antenna Placement —— 最大匹配 or 最小边覆盖

    题目链接:https://vjudge.net/problem/POJ-3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K ...

  2. svn问题:在eclipse里面使用SVN,怎么实现版本回滚呢?

    共有4个答案 我要回答» JustForFly 回答于 2012-04-27 10:20 举报   想回到SVN服务器端的最新版本就使用 team->还原.. 想回到SVN服务器端的其它版本使用 ...

  3. inserting a large number of records with SQLiteStatement.

    Below is a demo application I wrote that creates 100 records programmatically, inserts them using on ...

  4. 【POJ 3107】 Godfather

    [题目链接] 点击打开链接 [算法] 这题描述有些繁琐,先简化一下题意 : 对于一棵无根树,删除一个节点,使得其余的联通块中,最大的联通块最小 那么,这题就很好做了 对这棵树进行一遍DFS,求出每个节 ...

  5. asp.net mvc5 使用百度ueditor 本编辑器完整示例(上)

    最近做一个项目,用到了百度ueditor富文本编辑器,功能强大,在线编辑文档,上传图片\视频.附件. MVC 模型的控制器准备: 1.建立模型. 在项目中Model 文件夹中建立 文章 模型,注意如果 ...

  6. bzoj 4814: [Cqoi2017]小Q的草稿【计算几何】

    //先打个50暴力,10min50分简直美滋滋~ #include<iostream> #include<cstdio> #include<algorithm> u ...

  7. 小程序各种授权封装、saveImageToPhotosAlbum、chooseAddress、getUserInfo、getLocation,保存图片、获取用户信息、获取位置信息、收货地址等

    代码略多,请异步 github __点击链接

  8. springboot(十一)SpringBoot任务

    github地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1 ...

  9. 又一篇Centos7下的asp.net core部署教程

    历程2个多月的学习,我终于从PHP转.Net开发了. 从壹开始前后端分离[ .NETCore2.1 +Vue 2 +AOP+DI]框架 感谢老张的博客,我对asp.net core入门主要就是靠他的博 ...

  10. 【SpringCloud构建微服务系列】微服务网关Zuul

    一.为什么要用微服务网关 在微服务架构中,一般不同的微服务有不同的网络地址,而外部客户端(如手机APP)可能需要调用多个接口才能完成一次业务需求.例如一个电影购票的手机APP,可能会调用多个微服务的接 ...