cf.B. Buttons

题意:

题意就是我们小时候都玩过的,有一种锁,每次呢我们只能按一个按钮,如果按钮是正确的,那么这个按钮就不会弹起来,如果是错的,那么之前没有弹起来的按钮都会弹起来。输入的数字是按钮的个数,你需要输出的,是在最坏的可能下你会试多少次能够将锁打开。
举个例子
输入为2 , 输出是3
因为:最坏的情况是1错,2对,1对
输入为3 , 输出为7
因为:最坏的情况是1错,2错,3对,2错,3对,1对,2对
输入为4 , 输出为14
因为:最坏的情况是1错,2错,3错,4对,3错,4对,2错,4对,1对,2错,4对,1对,3对,2对
输入为5 , 输出为25
因为:这个同理,不相信的话你可以按照我上述的方法来试试。
跟据这个规律,我们就得到了我们的代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N];
int main()
{
	int t;
	cin >> t;
	int res = 0;
	int k = t;
	for(int i = 1 ; i <= t ; i++)
	{
		if(i==1)
		{
			res=res+k;
			k--;
		}
		else if(i == t)
		{
			res++;
		}
		else
		{
			res=res+(k-1)*i+1;
			k--;
		}
	}
	cout << res << endl;
	return 0;
}
     
  
发布了107 篇原创文章 · 获赞 3 · 访问量 7112

猜你喜欢

转载自blog.csdn.net/qq_43504141/article/details/104283665
今日推荐