| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
1AC 递推识数 递归输出// 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator