Using the simulation genie to answer questions about the hundred-degree Programming Competition

Source: Internet
Author: User

The original question must be done in c ++, but the simulation genie is faster, but the optimization is not carefully analyzed.

   Question 1 continuous positive integer (10 points)

Description: a positive integer may be expressed as the sum of n (n> = 2) continuous positive integers, for example:
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8

Write a program to find all consecutive positive integer sequences that meet the input positive integer.

Input data: A Positive Integer provided to the program as a command line parameter.

Output Data: all positive integer sequences that conform to the topic description are printed on the standard output. Each sequence is a sequence. Each sequence starts from the smallest positive integer of the sequence and is printed in ascending order. If the result has multiple sequences, print the smallest positive integers of each sequence from small to large. In addition, the sequence cannot be repeated. integers in the sequence are separated by a space. If there is no matching sequence, "NONE" is output ".

For example, for 15, the output result is:
1 2 3 4 5
4 5 6
7 8

For 16, the output result is:
NONE

Rating criteria: whether the program output result is correct.

Print (str );

While (nStop () do
Delay (100)

Print ("Press"... _ STOPHKEY... ", and then enter a number to exit the program ")
Print ("enter a positive integer :")

Ns = io. read ();

While (nStop () and (not tonumber (ns) do
Print ("enter a valid integer ");
Ns = io. read ();
End

N = tonumber (ns)

Print ("")

For I = 1, n/2 + 1, 1 do
Local k = 0;
Local j2 = 0;
For j = I, n/2 + 1, 1 do
K = k + j;
J2 = j;
If (k = n) then break end;
End;


If (k = n) then
Local out = n .. "=" .. I;
For y = I + 1, j2, 1 do
Out = out... "+"... y
End;
Print (out );
End;
End;

Print ("")

End;

The size of the second overlapping interval (20 points)

Description: write a program to find out the maximum overlap interval in the input data file described in "input data and format" below.

For A positive integer n, if n is between two positive integers (assume A and B) in A row in the data file, that is, if A <= n <= B or A> = n> = B, n belongs to the row. If n belongs to both I and j, there is an overlap between I and j; the size of the overlap interval is the number of integers belonging to both row I and j.

For example, the overlapping ranges of rows (10 20) and (12 25) are [12 20], and the size is 9; rows (20 10) and (12 18) the overlap interval of is [10 12], and the size is 3; the overlap interval of rows (20 10) and (20 30) is 1.

Input data: The program reads the input data text file named input.txt. The number of lines in the file is between 1 and 1,000,000. Each line uses two integers separated by a space. The size order of these two integers is random, each number is between 1 and 2 ^ 32-1. (For convenience, you can download the input.txt file. In actual running, we will use an input file with different content .) Output Data: The maximum overlap interval in the input data file is printed on the standard output. If no overlap interval exists for all rows, 0 is output.

Scoring standard: The program output result must be correct, and the memory usage must not exceed 256 MB. The faster the program runs, the better.

Print (str );

-- Without this text, simply write
-- Txt = string. load ("input.txt ")

Txt = [[12 20
20 32
1 25
6 7
11 5
6 4
6 8
1 3
2 23
11 22
1 32
4 18
]

Tab = string. split (txt, ""); -- splits multiple rows into an array.

For k, v in pairs (tab) do
Local t = string. split (v .. "", "") -- split the current value into two numbers:
Tab [k] = {tonumber (t [1]), tonumber (t [2])};
End;


N = 0;

For k, x in pairs (tab) do
Sleep (1 );
Local start_n = 0;
Local end_n = 0;
For k, y in pairs (tab) do
Sleep (1 );
If (x [1]> y [1]) and (x [1] <y [2]) then
Start_n = x [1];

If (x [2] <y [2]) then
End_n = x [2];
Else
End_n = y [2];
End;
Elseif (y [1]> x [1]) and (y [1] <x [2]) then
Start_n = y [1];
If (x [2] <y [2]) then
End_n = x [2];
Else
End_n = y [2];
End;
End;
End;

Local n2 = end_n-start_n;
If (n2> n) then
N = n2;
End;
End;

Print ("Maximum overlap interval size" .. n );

Delay (_ prop _loop)

]>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.