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;
}
上一篇:[Lua]元表


下一篇:数理统计与数据分析-hw1