unsigned int calcRank(unsigned long n, unsigned int count, hash* table)
{
	// If the rank has already been determined for n
	if (table->count(n) > 0)
	{
		//std::cout << n << " already determined to be " << table->at(n) << std::endl;
		return table->at(n);
	}

	// Else, the rank has not been determined
	else
	{
		if (fmod(n, 2) == 0)
		{
			count = count +  calcRank(n / 2, count, table);
			table->insert({ n, count });
			return count;
		}

		else
		{
			count = count + calcRank(3 * n + 1, count, table);
			table->insert({ n, count });
			return count;
		}

	}
}
int main(){

	hash* ranks = new(hash);
	ranks->insert({ 1, 1 });

	unsigned int max = 1;
	unsigned int temp_max = 0;
	unsigned int num = 1;

	clock_t begin = clock();
	for (unsigned int i = 500000; i < 1000000; i++)
	{
		temp_max = calcRank(i, 1, ranks);
		if (temp_max > max)
		{
			num = i;
			max = temp_max;
		}
	}
	clock_t end = clock();
	double seconds = end - begin / CLOCKS_PER_SEC;
	std::cout << "Clock: " << seconds << std::endl;

	std::cout << num << " produces the longest chain of " << max << " numbers." << std::endl;

	return 0;
}
예제 #3
0
 string getPermutation(int n, int k) {
     k--;    //so the first permutation ranks 0
     vector<int> factorial(n, 0);
     calcFactorial(factorial);
     vector<int> rank(n, 0);//i-th element, rank among [i, n)
     calcRank(n, k, rank, factorial);
     return getPermutationFromRank(rank);
 }
예제 #4
0
int main(void) {
    country cou[1001];
    int tmp, maxCon, selCon, i;
    scanf("%d %d", &maxCon, &selCon);
    for(i=0; i<maxCon; i++) {
        scanf("%d", &tmp);
        scanf("%d %d %d", &cou[tmp].gold, &cou[tmp].silver, &cou[tmp].bronze);
    }   
    printf("%d\n", calcRank(selCon, cou, maxCon));
    return 0;
}