Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112538
  • 博文数量: 23
  • 博客积分: 975
  • 博客等级: 准尉
  • 技术积分: 262
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-19 00:54
文章分类
文章存档

2011年(2)

2010年(3)

2008年(18)

我的朋友

分类:

2008-07-19 02:55:14

#include<stdio.h>
#include<malloc.h>
#include<math.h>

int len,count;

void solution(int n,int line[],int positive,int negative)
{
    int i,nextLine[20];

    /*for debug*///for(i=0;i
    
    //count it!

    for(i=0;i<n;i++) line[i]==1?positive++:negative++;
    //break it when ...

    if(positive>(len*(len+1)/4)||negative>(len*(len+1)/4)) return;
    //it's over and return

    if(n==len)
    {
        count++;
        /*for debug*///printf("p=%d n=%d count=%d\n",positive,negative,count);

        return;
    }
    else
    {
        nextLine[0]=-1;
        for(i=0;i<n;i++) nextLine[i+1]=nextLine[i]*line[i];
        solution(n+1,nextLine,positive,negative);
        nextLine[0]=1;
        for(i=0;i<n;i++) nextLine[i+1]=nextLine[i]*line[i];
        solution(n+1,nextLine,positive,negative);
    }
}

void countEquel()
{
    int sign;
    if((len*(len+1)/2)%2!=0) return;
    else
    {
        sign=-1;
        solution(1,&sign,0,0);
        sign=1;
        solution(1,&sign,0,0);
    }
}

void main()
{
    typedef struct trst
    {
        int result;
        struct trst *next;
    }rst;

    rst *head,*p;
    head=p=(rst*)malloc(sizeof(rst));
    p->next=NULL;
    
    while(1)
    {
        scanf("%d",&len);
        if(len)
        {
            p->next=(rst*)malloc(sizeof(rst));
            p=p->next;
            p->next=NULL;
        }
        else break;
        //count...

        count=0;
        countEquel();
        p->result=count;
    }
    p=head->next;
    while(p)
    {
        printf("%d\n",p->result);
        p=p->next;
    }
}

/*
void main()
{
    len=20;
    count=0;
    countEquel();
    printf("%d\n",count);
}
*/

阅读(1269) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~