Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

1AC 递推识数 递归输出

Posted by uncleD at 2019-08-29 15:25:41 on Problem 3445
// poj3445.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>

using namespace std;

int table[16];

void iniTable()
{
	table[0] = 0;
	table[1] = 0;

	for (int i = 2; i < 16; i++)
	{
		table[i] = 0;
		for (int j = 0; j < i - 1; j++)
			table[i] += table[j] + 1;

		table[i] += table[i-1];
	}
}

int DecodeSet(string set)
{
	if (set.compare("{}") == 0)
		return 0;
	else if (set.compare("{{}}") == 0)
		return 1;
	else
	{
		int dot = 0;

		for (int i = 0; i < set.length(); i++)
			if (set[i] == ',')
				dot += 1;

		for (int i = 0; i < 16; i++)
			if (table[i] == dot)
				return i;
	}

	return 0;
}


void CodeSet(int n)
{
	if (n == 0)
		cout << "{}";
	else if (n == 1)
		cout << "{{}}";
	else
	{
		cout << "{{}";
		for (int i = 1; i <= n - 1; i++)
		{
			cout << ',';
			CodeSet(i);
		}
		cout << '}';
	}
		
	return;
}

int main()
{
	int nCase;
	string set1;
	string set2;

	iniTable();

	cin >> nCase;
	cin.ignore();

	while (nCase--)
	{
		getline(cin,set1);
		getline(cin,set2);

		int a = DecodeSet(set1);
		int b = DecodeSet(set2);

		CodeSet(a+b);
		cout << endl;
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator