欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

POJ数据的输入输出格式

程序员文章站 2022-04-10 20:37:58
POJ在评阅习题时需要向程序提供输入数据,并获取程序的输出结果。因此提交的程序需按照每个习题具体的输入输出格式要求处理输入输出。有的时候,测评系统给出程序的评判结果是“数据错误”或“结果错误”,有可能就与没有正确使用输入输出格式有关。 POJ要求的输出一般有3种情况。 (1)输出一个数据:数据后加换 ......

    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; 

}