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

Re:帮我看看我的程序,老是wrong answer,我确定没有错误啊,是不是格式有要求啊?

Posted by chavez1986 at 2009-08-05 12:12:10 on Problem 1001
In Reply To:帮我看看我的程序,老是wrong answer,我确定没有错误啊,是不是格式有要求啊? Posted by:chavez1986 at 2009-08-04 11:22:23
> /*
>  * main.c
>  *
>  *  Created on: 2009-8-1
>  *      Author: chavez
>  */
> #include <stdio.h>
> 
> #define MAX_LENGHT	5*25
> char szLine[6];
> int n;
> int main() {
> 	while(scanf("%s%d",szLine,&n)!=EOF){
> 		if(n == 0){
> 			printf("1\n");
> 			continue;
> 		}
> 
> 
> 		//szLine[6] = { '0' };//in
> 		int result[MAX_LENGHT] = { 0 };//out
> 		int a[5] = { 0 };//被乘数
> 		int b[MAX_LENGHT] = { 1, 0 };//乘数
> 
> 		int i, dflag = 0;
> 		int dot = 0;//the position of dot
> 		int j = 0;
> 		int k, m;
> 		int length = 0;
> 		int start = 0;
> 		int resultDotPosition;
> 
> 		//judge 小数点后面有没有0
> 
> 		for (i = 0; i < 5; i++) {
> 			if (szLine[i] == '.' && i != 5)
> 				dflag = 1;
> 		}
> 		//deal with in
> 
> 		for (i = 0; i <= 5; i++) {
> 			if (dflag) {
> 				if (szLine[5 - i] == '0') {
> 					if (szLine[4 - i] != '0')
> 						dflag = 0;
> 					dot--;
> 					continue;
> 				}
> 			}
> 
> 			if (szLine[5 - i] != '.') {
> 				a[j] = szLine[5 - i] - '0';
> 				j++;
> 			} else {
> 				dot += i;
> 			}
> 
> 		}
> 
> 		for (k = 0; k < n; k++) {
> 			for (i = 0; i < 5; i++) {
> 				for (j = 0; j < MAX_LENGHT; j++) {
> 					result[i + j] += a[i] * b[j];
> 				}
> 			}
> 
> 			for (i = 0; i < MAX_LENGHT; i++) {
> 				if (result[i] >= 10) {
> 					result[i + 1] += result[i] / 10;
> 					result[i] %= 10;
> 				}
> 			}
> 			if (k == n - 1)
> 				break;
> 			for (i = 0; i < MAX_LENGHT; i++) {
> 				b[i] = result[i];
> 				result[i] = 0;
> 			}
> 
> 		}
> 
> 		for (i = MAX_LENGHT - 1; i >= 0; i--) {
> 			if (result[i]) {
> 				length = i + 1;
> 				break;
> 			}
> 		}
> 		resultDotPosition = dot * n;
> 		m = length - dot * n;
> 		if (m < 0) {
> 			printf(".");
> 			for (j = -m; j > 0; j--) {
> 				printf("0");
> 			}
> 
> 		}
> 		for (i = MAX_LENGHT - 1; i >= 0; i--) {
> 			if (i == resultDotPosition - 1 && m >= 0)
> 				printf(".");
> 			if (start == 1)
> 				printf("%d", result[i]);
> 			else if (result[i]) {
> 				printf("%d", result[i]);
> 				start = 1;
> 			}
> 
> 		}
> 		printf("\n");
> 	}
> 
> 	return 0;
> }
> 
不知道为什么,就是加了个不一定是6个数字的处理,修改如下:
/*
 ============================================================================
 Name        : 1001-exponentiation.c
 Author      : gsj
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

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

#define MAX_LENGHT	5*25


int main(void) {
	//input
	char szLine[6];
	int n;

	//output
	int result[MAX_LENGHT];

	//middle
	int a[5];//被乘数
	int b[MAX_LENGHT];//乘数
	while (scanf("%s%d", szLine, &n) != EOF) {
		//loop index
		int i, j, k;

		//flag
		int dflag = 0;
		int count = 0, dot = 0;
		int length, dotPosition, start,m;
		int sLength;
		//x^0
		if (n == 0) {
			printf("1\n");
			continue;
		}
		//initialization
		memset(result, 0, MAX_LENGHT * sizeof(int));
		memset(b, 0, MAX_LENGHT * sizeof(int));
		memset(a, 0, 5 * sizeof(int));
		b[0] = 1;

		//szLine's length
		sLength = strlen(szLine);
		//判断有没有小数点
		for (i = 0; i < sLength; i++) {
			if (szLine[i] == '.' && i != 5)
				dflag = 1;
		}

		//deal with input
		j = 0;
		for (i = 0; i <= 5; i++) {
			if (dflag) {
				if (szLine[sLength -1 - i] == '0') {
					if (szLine[sLength -2 - i] != '0')
						dflag = 0;
					dot--;
					continue;
				}else
					dflag = 0;
			}

			if (szLine[sLength -1 - i] != '.') {
				a[j] = szLine[sLength -1 - i] - '0';
				count += a[j];
				j++;
			} else
				dot += i;
		}

		//0^n(n!=1)
		if (count == 0) {
			printf("0\n");
			continue;
		}

		//1^n
		if(count == 1 && a[0] == 1){
			printf("1\n");
			continue;
		}

		//multiply
		for (k = 0; k < n; k++) {
			for (i = 0; i < 5; i++)
				for (j = 0; j < MAX_LENGHT; j++)
					result[i + j] += a[i] * b[j];

			for (i = 0; i < MAX_LENGHT; i++) {
				if (result[i] >= 10) {
					result[i + 1] += result[i] / 10;
					result[i] %= 10;
				}
			}
			if (k == n - 1)
				break;
			for (i = 0; i < MAX_LENGHT; i++) {
				b[i] = result[i];
				result[i] = 0;
			}

		}

		//length
		length = 0;
		for (i = MAX_LENGHT - 1; i >= 0; i--) {
			if (result[i]) {
				length = i + 1;
				break;
			}
		}

		//dot's position
		dotPosition = dot * n;
		m = length - dot * n;

		if (m <= 0) {
			printf(".");
			for (j = -m; j > 0; j--) {
				printf("0");
			}

		}
		start = 0;
		for (i = MAX_LENGHT - 1; i >= 0; i--) {
			if (i == dotPosition - 1 && m > 0)
				printf(".");
			if (start == 1)
				printf("%d", result[i]);
			else if (result[i]) {
				printf("%d", result[i]);
				start = 1;
			}

		}
		printf("\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