1.25 B

B - 2

Time limit 1000 ms
Memory limit 262144 kB

Problem Description

Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you’ve guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens.

Consider an example with three buttons. Let’s say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you’ve got two pressed buttons, you only need to press button 1 to open the lock.

Manao doesn’t know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he’s got to push a button in order to open the lock in the worst-case scenario.

Input

A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons the lock has.

Output

In a single line print the number of times Manao has to push a button in the worst-case scenario.

Sample Input

2

3

Sample Output

3

7

问题链接:B - 2

问题简述:

输入n代表有n把锁,每拿错锁就得重来,问最多拿多少次锁必定找到合适的锁

问题分析:

我们来看sample input
当n=4,通过画树状图(如下图)可知道总共需要14次
第一列意为4个数有3个数是错误的,所以是31(1代表在第一列)
第二列意为3个数有2个数是错误的,因为第二列的前提是第一列是选择了“1”,所以要额外+1,所以是2
2(2代表在第二列)
第三列意为2个数有1个数是错误的,所以是1*3
剩下的就是正确的顺序,总共是n也就是4

所以易得公式n+(n-1)*1+(n-2)*2+(n-3)*3······直到n自减到1
在这里插入图片描述

程序说明:

sum用于储存相加的结果
temp用于自增即 1 2 3 4 5 6 7 8 9
最后结果输出sum+n即可

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int sum=0;
	int temp = 1;
	for (int i = n-1;i != 0;i--)
	{
		sum = sum + i * temp;
		temp++;
	}
	cout << sum + n;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86647673