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

讨论区关于1001这道题的提醒都注意到了,但是提交还是报WA。附代码请教。

Posted by wangchao at 2005-12-10 23:42:05 on Problem 1001
就是把数字存放按位存到数组里面,按位乘。


#include <stdio.h>
#include <stdlib.h>

#define ARR_OFFSET	400
#define BUF_SIZE	20
#define	CHECK_ARG

int
main(void)
{
	char	buf[BUF_SIZE];
	int	n;
	int	src_arr[ARR_OFFSET*2],dst_arr[ARR_OFFSET*2],tmp_arr[ARR_OFFSET*2];
	int	debug_i;
	float	ff;

	memset(buf,0,sizeof(buf));

	while(scanf("%s %d",buf,&n)!=EOF) {
		if(n==1) {
			#ifdef	CHECK_ARG
			ff=atof(buf);
			if(ff>99.999001 || ff<=0.0000000001 || n<0 || n>25) {
				ff=0;
				n=0;
				continue;
			}
			#endif
			printf("%s\n",buf);
		}
		else {
			#ifdef	CHECK_ARG
			ff=atof(buf);
			if(ff>99.999001 || ff<=0.0000000001 || n<0 || n>25) {
				ff=0;
				n=0;
				continue;
			}
			#endif
			init_array(src_arr,dst_arr,tmp_arr,buf);

			deal_array(src_arr,dst_arr,tmp_arr,n);

			print_array(dst_arr);

		}
		memset(buf,0,sizeof(buf));
		n=0;
		ff=0;
	}
	return 0;
}

int
init_array(int arr1[ARR_OFFSET*2],int arr2[ARR_OFFSET*2],int arr3[ARR_OFFSET*2],char s[BUF_SIZE])
{
	int	i,j=0,k;
	char	*tmp_idx,*dot_pos;
	char	tmp[BUF_SIZE],tmp_c[2];

	memset(tmp,0,sizeof(tmp));
	sprintf(tmp,"%f",atof(s));

	tmp_idx=tmp;
	dot_pos=strchr(tmp,'.');

	for(i=0;i<ARR_OFFSET*2;i++) {
		arr1[i]=0;
		arr2[i]=0;
		arr3[i]=0;
	}

	while(*tmp_idx) {
		memset(tmp_c,0,sizeof(tmp_c));
		strncpy(tmp_c,tmp_idx,1);

		if(tmp_idx!=dot_pos) {
			k=ARR_OFFSET-(tmp_idx-dot_pos+1)+j;
			arr1[k]=atoi(tmp_c);
			arr2[k]=atoi(tmp_c);
			arr3[k]=atoi(tmp_c);
		}
		else {
			j++;
		}
		tmp_idx++;
	}

	return 0;
}

int
deal_array(const int src_arr[ARR_OFFSET*2],int dst_arr[ARR_OFFSET*2],int tmp_arr[ARR_OFFSET*2],const int n)
{
	int	i,j,p;
	int	value,level;

	for(p=1;p<n;p++) {
		for(i=0;i<ARR_OFFSET*2;tmp_arr[i]=dst_arr[i],dst_arr[i]=0,i++);
		for(i=0;i<ARR_OFFSET*2;i++) {
			if(src_arr[i]==0) {
				continue;
			}
			for(j=0;j<ARR_OFFSET*2;j++) {
			if(tmp_arr[j]>0) {
					value=src_arr[i]*tmp_arr[j];
					level=(i-ARR_OFFSET)+(j-ARR_OFFSET);
					update_array(dst_arr,value,level);
        			}
			}
		}
	}
	return 0;
}

int
update_array(int da[ARR_OFFSET*2],int v,int level)
{
	int	bal;

	bal=v+da[ARR_OFFSET+level];

	if(bal<10) {
		da[ARR_OFFSET+level]=bal;
	}
	else {
		da[ARR_OFFSET+level]=bal%10;

		bal=bal/10;
		level++;
		update_array(da,bal,level);
	}
	return 0;
}

int
print_array(int parr[ARR_OFFSET*2])
{
	int	p;
	int	i_can,d_can;
	int	i_p=0;
	char	i_str[ARR_OFFSET],d_str[ARR_OFFSET],tmp[1];

	memset(i_str,0,sizeof(i_str));
	memset(d_str,0,sizeof(d_str));
	memset(tmp,0,sizeof(tmp));

	for(p=ARR_OFFSET*2-1;p>=ARR_OFFSET;p--) {
		if(parr[p]>0)
			i_can=1;
		if(i_can==1) {
			sprintf(tmp,"%d",parr[p]);
			strncpy(&i_str[i_p],tmp,1);
			i_p++;
		}
	}

	for(p=0;p<ARR_OFFSET;p++) {
		if(parr[p]>0)
			d_can=1;

		if(d_can==1) {
			sprintf(tmp,"%d",parr[p]);
			strncpy(&d_str[ARR_OFFSET-p-1],tmp,1);
		}
	}
	if(strlen(d_str)>0 && strlen(i_str)>0) {
		printf("%s.%s\n",i_str,d_str);
	}
	else if(strlen(d_str)>0) {
		printf(".%s\n",d_str);
	}
	else if(strlen(i_str)>0) {
		printf("%s\n",i_str);
	}
	else {
		printf("0\n");
	}
	return 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