Donghua oj- Advanced Topics in 30 questions

Here Insert Picture Description

30 seeking the number of columns items

Author: Zhu Kai Time limit: 10S chapters: one-dimensional array

Problem Description :

The number of columns is a very interesting mathematical world of digital arrangement rules, it makes a string of numbers each had some connection between the walls. Many mathematicians of the series had a strong interest in, spend a lot of time to study it, obviously is one of them. One day, he focused on a new series arrangement rules, the rules of the arrangement meets the following conditions:

  1. The first number is a number of columns.

  2. The second number is the number of columns 5.

  3. The number of the i-th row (where i> 2) is the number of the i - number plus the value 1 (i - 3) × 3 + 7.

Obviously quickly calculate the number of the first three columns of numbers:

  1. The first one.

  2. The second is 5.

  3. The third was 12. (The third number is the second number plus (3 - 3) × 3 + 7, i.e., the third number is: 5 + (3 - 3) × 3 + 7 = 12)

But when obviously want to continue to calculate the number of columns down, he found that the amount of calculation increases, more and more difficult to calculate, the calculation speed is getting slower and slower. So, obviously you would turn to the programming experts, to help him write a program to calculate the number of columns in the front 50 and then, when obviously need to know the number of columns in which a digital one, you put that item number tell obviously.
Obviously the question boils down to: It is with a positive integer n, n items requires that you first output value of the number of columns subject described.

Enter a description:

You need to write the program from a standard input device (typically a keyboard) reads a plurality of sets of test data, each set of test data only one row, each row comprising only a positive integer n-(. 1 ≤ n-≤
50). There is no blank line between each test group and the subsequent test data, the test data of the first set of front and behind the last set of test data are also no blank lines. Output Description:

For each set of test data, you need to write the program to calculate a set of corresponding calculation result, and the result of each operation are sequentially written to standard output device (usually a text terminal to start the program, such as Windows command line terminal). Each operation result to an integer, i.e., n-th column value of the number of items described in the title. Forming a single row each calculation result data, which are the first line and the end of the line without any spaces, there is no blank lines between each set of operation and the subsequent one computation result, the first set of computation results and the foregoing computation result after the last group also they do not have any blank lines.
Input Example: Example 1 2 Output: 15

Code:

/*
	T30 求数列项 
*/

#include<stdio.h>
#define MAX_SIZE 52

int main() {
	int n = 0;
	int nums[MAX_SIZE] = {0};
	int i = 0;
	
	nums[1] = 1;
	nums[2] = 5;
	for (i = 3; i <= 50; i++) {
		nums[i] = nums[i - 1] + (i - 3) * 3 + 7;
	}
	
	while (scanf("%d", &n) != EOF) {
		printf("%d\n", nums[n]);
	}
	
	return 0;
} 
Published 46 original articles · won praise 6 · views 7684

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/104522589