Input and output data format of POJ

    POJ exercises when reviewers need to provide input data to the program, and to obtain an output result of the program. Submitted programs thus input and output formats required specific processes input and output requirements for each exercise. Sometimes, the system gives the judge the results of the evaluation program is "data error" or "wrong result," there might not properly use the input and output formats related.

    POJ output generally requires three cases.

    (1) a Data Output: data after linefeed.

    (2) Data Output line: space between a data interval (or intervals specified character), plus end of line wrap (wrap may have a front space).

    (3) Multi-line output data: data between each row with a space interval (or intervals specified character), only the end of line linefeed.

    In addition to the subject requires no input data, a small number (e.g. POJ 1316 Self Numbers), POJ treatment of the subject generally require a plurality of sets of test data which follows the data input in the form of generally two cases.

1. A given number of sets of test data

Test data into multiple lines. T is the number of the first row of test data. T row gives the following data to be processed. In response to this input, the general framework of the program are:

cin >> nCase;

while (nCase--)

{

    // input required to process each test and

}

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

Description

Eva homework, there are many series of fill in the blank exercises. Fill in the blank exercise requirements are: the first four known number of columns, fill out the fifth. As it has been aware of these series may be the only arithmetic or geometric series, she decided to write a program to do these exercises.

Input

The first row is the number of columns the number of t (0 <= t <= 20). Each following line contains four integer representing the number of the first four columns. The first five columns of agreement are natural numbers not greater than 10 ^ 5, the geometric ratio of the number of columns is a natural number.

Output

Each column number of the input, the output of its last five.

Sample Input

2

1 2 3 4

1 2 4 8

Sample Output

1 2 3 4 5

1 2 4 8 16

    (1) programming ideas.

    Simply using a, b, c, d, e five variables to hold the front 5 sequence. After the first four inputs, if ba == cb && cb == dc, as the number of arithmetic sequence, e = d + cb; otherwise, as the number of geometric series, e = c * d / b.

    (2) source.

#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; 

}  

    Of course, some POJ title claim test data processing only one set, e.g. POJ 1077 Eight. This case, the test data required to enter directly, can be processed.

2. At the end of a particular data input.

    Test input comprising a plurality of sets of data. Each test occupies a separate line. It indicates the end of a particular input data (such as 0 or a negative number). In response to this input case, each test is the number 1, and ending with 0 as an example, general procedures for the frame:

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

{

    // input required to process each test and

}

【例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) programming ideas.

    Since the subject of the request for the data processing range n <= 1,000,000, in 10! = 3628800> 1000000, therefore, can define an array int table [11] previously stored Good 0! ~ 10! Value.

    N for each input test data, circulating

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

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

    Processed, if n == 0, then n can be expressed as the number of factorial and several outputs "YES".

    (2) source.

#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; 

}   

Guess you like

Origin www.cnblogs.com/cs-whut/p/11001644.html