CodeForces - 268B Buttons (简单模拟,找规律)

本文探讨了一种涉及按钮序列的复杂锁解锁问题,通过分析最优策略下最坏情况的按钮按下次数,提供了一个C++实现的解决方案,展示了算法设计与问题解决的巧妙结合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
Examples
Input
2
Output
3
Input
3
Output
7
Note
Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes.
问题链接http://codeforces.com/problemset/problem/268/B
问题简述:有n个按钮,要按有且仅有的一个正确按按钮的序列按按钮才能开锁,算出获取正确序列的按按钮最多次数
问题分析:设按钮有n个,则获取第一个数字要按n次,获取第二个数字要按n-1次,以此类推,用一个循环累加次数即可
AC通过的C++语言程序如下:

#include<iostream>
#include<string>
#include<stdio.h>
#include <algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	int n, res = 0;
	cin >> n;
	for (int i = 1; n - i != -1; i++)
	{
		res = res + 1 + (n - i)*i;
	}
	cout << res;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值