hdu 1063 poj 1001 Exponentiation(幂)

将其转化为整数的大数乘法。

#include <stdio.h>
#include <string.h>

/********************************************************/
void rever(char *res){
	//  ?????????
	int res_len = strlen(res);
	for(int i = 1,j = 0;i < res_len;i += 2,++j){
		res[j] += res[res_len - j - 1];
		res[res_len - j - 1] = res[j] - res[res_len - j - 1];
		res[j] -= res[res_len - j - 1];
	}
	return ;
	
}
int add(char *input1_tem,char *input2_tem,char *res){

	int input1_len = strlen(input1_tem);
	int input2_len = strlen(input2_tem);
	int res_top = 0;
	char input1[150],input2[150];
	strcpy(input1,input1_tem);
	strcpy(input2,input2_tem);
		
	while(input1_len&&input2_len){
		res[res_top ++] = input1[--input1_len] + input2[--input2_len] -'0';		
	}
	while(input1_len){
		res[res_top ++] = input1[--input1_len]; 
	}
	while(input2_len){
		res[res_top ++] = input2[--input2_len];
	}
	
	res[res_top] = '\0';
	for(int i = 0; i < res_top;++i){
		 if(res[i] > '9'){
 			res[i] -= 10;
 			res[i + 1] += 1;
 		}
	}
	if(res[res_top]!='\0'){
		res[res_top++] +='0';
		res[res_top] = '\0';
	}
	rever(res);
	return res_top;
}
/********************************************************/

/********************************************************/
void hi_mul(char *input1_tem,char *input2_tem,char *res){
	
	int input1_len = strlen(input1_tem);
	int input2_len = strlen(input2_tem);
	int res_top = 0,res_tem_top = 0;
	char res_tem[150];
	char input1[150],input2[150];
	strcpy(input1,input1_tem);
	strcpy(input2,input2_tem);
	strcpy(res,"0");
	while(input2_len --){
		res_tem_top	= 0;
		for(int i = input1_len - 1; ~i;--i){
			res_tem[res_tem_top ++ ] = ( input1[i] - '0') * (input2[input2_len]-'0');
		}
		res_tem[res_tem_top] = '\0';
		for(int i = 0; i < res_tem_top; ++i){
			if(res_tem[i] > 9){
				res_tem[i+1] += res_tem[i] / 10;
				res_tem[i] %= 10;
			}
			res_tem[i] += '0'; 
		}
		
		if(res_tem[res_tem_top]!='\0'){
			res_tem[res_tem_top++] += '0';
			res_tem[res_tem_top] = '\0';
		}
	
		rever(res_tem);
		add(res_tem,res,res);
		input1[input1_len++] = '0';
		input1[input1_len] = 0;
	}
	return ;
}
/********************************************************/

/********************************************************/
int find_point(char *input){
	int i= 0,pp; 
	int input_len = strlen(input);
	for(i =  0; i< input_len;++i){
		if(input[i] == '.'){		
			break;
		}
	}
	if(i != input_len){
		
		pp = i;
		for(;i< input_len;++i){
			input[i] = input[i+1];
		}
		return input_len - pp - 1;
	}
	return input_len - i;	
} 

void PrintRes(char *res,int pp){
	int res_top = strlen(res);
	int i = 0;
	for(i = 0; i < res_top - pp;++i){
		if(res[i] != '0'){
			break;
		}	
	}
	for(;i< res_top-pp;++i){
		printf("%c",res[i]);
	}
	if(pp){
		for(i = res_top - 1;i > res_top	- pp - 1;--i ){
			if(res[i] != '0'){
				res[i + 1] = '\0'; 
				break;
			}
		}
		if(i != res_top - pp - 1){
			printf(".%s",&res[res_top - pp]);
		}
	}
	printf("\n");

	
	return ;
}
/********************************************************/

/********************************************************/
int main(int argc, char *argv[])
{
	
	int thePow,p_len,res_pp_top;
	char input[7],res[150];
	
	while(~scanf("%s%d",input,&thePow)){
		
		strcpy(res,"1");
		
		p_len = find_point(input);	//小数点后数的个数 并将小数点删除。 
		//printf("%s %d\n",input,p_len);
		res_pp_top = thePow * p_len;
		while(thePow){
			--thePow; 
			hi_mul(res,input,res);
		}
		PrintRes(res,res_pp_top);
		//printf("%s\n",res);
	}	
	return 0;
}
/********************************************************/


你可能感兴趣的:(hdu 1063 poj 1001 Exponentiation(幂))