Analysis of C/C++ (level three) real questions in December 2021#中国电子学院#National Youth Software Programming Level Examination

insert image description here

Question 1: The house number of my house

My family lives in a short alley, and the house numbers of this alley are numbered sequentially starting from 1.
If the sum of all the house numbers minus twice the house number of my house is exactly equal to n, find the house number of my house and how many houses there are in total.
The data is guaranteed to have a unique solution.
Time limit: 1000
Memory limit: 65536
Input
a positive integer n. n < 100000.
Output
One line, containing two positive integers, which are the house number of my house and the total number of houses, separated by a single space.
Sample input
100
Sample output
10 15

This problem can be solved by traversing the house numbers of the short alleys. We can start from the first house number, try one by one, calculate the sum of all house numbers minus twice the current house number, if it is equal to the given n, then find the solution.

The specific solution ideas are as follows:

(1) Enter a positive integer n.

(2) Initialize the current house number as 1, and the total number of houses is 0.

(3) Traversing house numbers of short alleys:

  • Calculate the sum of all house numbers minus twice the current house number.

  • If it is equal to the given n, output the current house number and how many houses there are in total, and then end the program.

  • Otherwise, add one to the current house number, and add one to the total number of houses.

    </

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132381123