| ||||||||||
| 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 | |||||||||
我这个是递归做的,但是TIME LIMITTED EXCEED,帮忙看一下#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int t;
int k;
char str[65];
double n;
char*a;
double m;
void search(int);
void main()
{
int i,j,len;
double min,max,w;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d",&k);
scanf("%s",str);
scanf("%lf",&n);
min=0;
max=0;
for(j=0;j<k;j++)
{
if(str[j]=='p')
{
w=pow(2,k-1-j);
min+=0*w;
max+=1*w;
continue;
}
else
{
w=pow(2,k-1-j);
min-=1*w;
max+=0*w;
continue;
}
}
if(n<min||n>max)
{
printf("Impossible\n");
continue;
}
else
{
a=(char*)calloc(k+1,sizeof(char));
len=strlen(a);
search(0);
}
}
}
void search(int s)
{
double w;
if(s>=k)
{
if(m==n)
printf("%s\n",a);
}
else
{
if(str[s]=='p')
{
a[s]='1';
w=pow(2,k-1-s);
m+=w*1;
search(s+1);
m-=1*w;
a[s]='0';
w=pow(2,k-1-s);
m+=w*0;
search(s+1);
}
else
{
a[s]='1';
w=pow(2,k-1-s);
m-=1*w;
search(s+1);
m+=1*w;
a[s]='0';
w=pow(2,k-1-s);
m-=0*w;
search(s+1);
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator