Game of Connections

Problem Description
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
 
Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
 
Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
 
Sample Input
2
3
-1
 
Sample Output
2
5
 
 
Source
Asia 2004, Shanghai (Mainland China), Preliminary






思路:模拟计算过程,用一个数组存储两个数每一位相加的和,产生的进位保存的数组的下一位当中去,下一位计算的时候加上进位。要考虑到两个数的长度不一样的问题。还有存储和的数组要每次清零,就是因为这个问题半个小时的代码调试了两个小时,还有输出格式,每两个结果之间有一行空行。
#include
#include
using namespace std;
int c[1200],d[1200],sum[1200],i,j,k,len,n,h,e,m;
int main()
{
    char a[1200],b[1200];

    while(cin>>n)
    {
        while(n--)
        {
            memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(a,'0',sizeof(a));
        memset(b,'0',sizeof(b));
        memset(sum,0,sizeof(sum));
        m=0;
           cin>>a>>b;
            len=strlen(a)>=strlen(b)?strlen(a):strlen(b);
            int q=strlen(a)-1;
            int w=strlen(b)-1;
            for(i=q;i>=0;i--)
               c[i]=a[i]-'0';
               for(i=w;i>=0;i--)
               d[i]=b[i]-'0';
               for(i=q,e=w;i>=0&&e>=0;i--,e--)
               {   k=0;
                   if(sum[m]!=0)
                    k=1;
                   sum[m]=c[i]+d[e]+k;
                   if(sum[m]>=10)
                   {
                       sum[m]%=10;
                       sum[m+1]=1;

                   }
                   //cout<=0)
               {
                   for(;i>=0;i--)
                   {
                      k=0;
                   if(sum[m]!=0)
                    k=1;
                   sum[m++]=c[i]+k;
                   }
               }
               else if(e>=0)
               {  for(;e>=0;e--)
                   {
                       k=0;
                   if(sum[m]!=0)
                    k=1;
                   sum[m++]=d[e]+k;
                   }
               }
               h++;
               printf("Case %d:\n",h);
              printf("%s + %s = ", a,b);
               for(i=m-1;i>=0;i--)
               {
                   cout<=1)
                    cout<







 

你可能感兴趣的:(ACM,STEP)