本文主要是介绍CodeForce 82 A-Double Cola,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include<bits/stdc++.h>
using namespace std;
int main(){
//1 2 3 4
//5 10 20 40
//等比数列 a^n = a^1 * q^(n - 1)
//这个题的q是2
//比如说a^2 = a^1 * 2^1
//题目输入的数是和 求和公式是:a^1 * (1 - q^n) / (1 - q)
//已知n求Sn,再求名字
int nn; //nn是真实 需要 求的名字的位置
cin >> nn;
//5 10 20 40 80 160
//1 2 4 8 16 32
int n; //这是5 10 20 40的等比数列的n
int q = 2;
int a1 = 5;
//nn和Sn能挂上钩嘛? 能
//cout << log(1000000000) / log(2) << endl;
if(nn <= 5){
switch (nn) {
case 1:
cout << "Sheldon" << endl;
break;
case 2:
cout << "Leonard" << endl;
break;
case 3:
cout << "Penny" << endl;
break;
case 4:
cout << "Rajesh" << endl;
break;
case 5:
cout << "Howard" << endl;
break;
}
}else {
for(int i = 0; i <= 30; i++){
//因为数据范围是十的九次方,2^29 < 10^9
int Sn = (5 * (1 - pow(q, i))) / (1 - q);
if(nn < Sn){
//nn = 6, i = 2
//nn = 10, i = 2
//nn = 35, i = 3
//小于的时候证明是在这一轮可以算出答案了
//求一下这一轮中每个名字出现多少次
int circle = pow(q, (i - 1));
//然后算名字在哪个位置
//nn = 6, circle = 2, index = (nn - Sn_1) / circle, index = (6 - 5) / 2 = 0√
//nn = 10, circle = 2, index = (10 - 5) / 2 = 2√
//nn = 35, circle = 4, index = (35 - 15) / 4 = 5√
int Sn_1 = (5 * (1 - pow(q, i - 1))) / (1 - q);
int index = (nn - Sn_1) / circle;
switch (index) {
case 0:
cout << "Sheldon" << endl;
break;
case 1:
cout << "Leonard" << endl;
break;
case 2:
cout << "Penny" << endl;
break;
case 3:
cout << "Rajesh" << endl;
break;
case 4:
cout << "Howard" << endl;
break;
}
break;
}
}
}
return 0;
}
这篇关于CodeForce 82 A-Double Cola的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!