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

数据都通过了,怎么不能接受,求高手指点

Posted by wangminqian at 2009-02-23 23:38:01 on Problem 1001
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
char result1[500]={'\0'};//结果
int result[500]={0};

void reverse(char chr[],int l){
   //颠倒数组
    
    int i;
    char temp;
    for(i=0;i <= l/2;i++){
        temp=chr[i];
        chr[i]=chr[l-i];
        chr[l-i]=temp;
    }
}
 
char* multiple(char a[],char b[]){
   
    unsigned int la,lb;
    unsigned int i,j;
    unsigned int lresult=0; 
    la=strlen(a)-1;
    lb=strlen(b)-1;
    
    reverse(a,la);
    reverse(b,lb);
    
    for(i=0;i <= la;i++)
    for(j=0;j <= lb;j++){
        //精华所在
         
        result[i+j]+=(a[i]-48)*(b[j]-48);
        result[i+j+1]+=result[i+j]/10;
        result[i+j]%=10;
    }
    lresult=i+j+1;
    while(result[lresult] == 0) lresult--;
    
    if(lresult < 0 ){result[0]=0;lresult=0;}

    for(i=0;i<=lresult;i++)
	{result[i]+=48;
	 result1[i]=(char)result[i];
	}
    result1[i]=0;
    reverse(result1,lresult);
	
    return result1;   
}


void main()
{
   char b[6];//整数的字符串
   char a[7];//输入的浮点数   
   unsigned int n,i,j=0;
   unsigned int e=0;//整数位数
   char c[500];//result1的拷贝
   char t[500];//c的拷贝
   char x[500];//最终输出
while(scanf("%s %d",a,&n)==2)
{   
	for(i=0;i<strlen(a);i++)   //小数转换为整数
	 if(a[i]=='.')break;	 
       e=i;
		for(i=0;i<strlen(a);i++)
	{  
		if(a[i]=='.')j++;//j为小数点个数
	}
	for(i=0;i<strlen(a);i++)
		if((a[i]>='0'&&a[i]<='9')||(a[i]='.'));//字符串由数字和小数点组成
		else 
			exit(0);
		if(j<=1&&a[0]!='.')//小数点数目少于一个且不再第一位
		{if(a[0]=='0'&&a[1]!='.')exit(0);}//0在第一位第2位是小数点
		else exit(0);
   for(i=0;i<strlen(a);i++)
   {
    if(i>=e)b[i]=a[i+1];
     else b[i]=a[i];
   }
   b[i]='\0';

   if(n==1)strcpy(c,b);
   for(i=2;i<=n;i++) 
   {
	   if(i==2){
		        strcpy(t,b);
			    strcpy(c,multiple(b,t));
				strcpy(t,c);
				memset(result1,0,sizeof(result1));
				memset(result,0,sizeof(result));
	   }
	   else { 
           memset(c,0,sizeof(c));
		   reverse(b,strlen(b)-1);//每进行一次运算,b都被颠倒一次
		   strcpy(c,multiple(b,t));
           memset(t,0,sizeof(t));
		   strcpy(t,c);
		   memset(result1,0,sizeof(result1));
		   memset(result,0,sizeof(result));
	   }
   }

  if(n*(strlen(b)-e)>=strlen(c))
  {
	  for(i=0;i<=n*(strlen(b)-e)+1;i++)
	  {if(i==0)x[i]='.';
	   if(i>0&&i<=n*(strlen(b)-e)-strlen(c))x[i]='0';
	   if(i>n*(strlen(b)-e)-strlen(c))
	   x[i]=c[i-(n*(strlen(b)-e)-strlen(c))-1];
	  }
  }
  else{
   for(i=0;i<=strlen(c);i++)
   {if(i>strlen(c)-n*(strlen(b)-e))x[i]=c[i-1];
    if(i==strlen(c)-n*(strlen(b)-e))x[i]='.';
	if(i<strlen(c)-n*(strlen(b)-e))x[i]=c[i];
   }        
   x[i]='\0';
  }

  for(i=0;i<strlen(x);i++)
	  if(x[i]=='.')break;
	  j=strlen(x)-1;
  	 while(1)
	 {if(x[j]=='0'&&j>i){x[j]='\0';j--;}
	   else break;
	 }
	 if(x[0]=='0'&&x[1]=='.')i=1;
	   else i=0; 
	 while(i<=j){
         if(i==strlen(x)-1&&x[i]=='.')x[i]='\0';
		 printf("%c",x[i]);
		 i++;
	 }
    printf("\n");
    memset(a,0,sizeof(a));
	n=0;
	j=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