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

为什么老是WA, 我都快疯了.

Posted by koolcoy at 2006-12-26 00:52:36 on Problem 1002
我是用二叉搜索树做的. 想了n久没找到错误.

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

typedef struct NodeTag {
	int number;
	int duplicate;
	struct NodeTag* left;
	struct NodeTag* right;
} NodeType;

typedef NodeType* TreeType;

/*向二叉搜索树添加节点*/
void addNode(TreeType* tree, int number);
/*遍历二叉树, 输出结果*/
void go(TreeType* tree);
/*初始化数组, 数组用于字母到数字的映射*/
void init(int map[]);

/*Duplicates flag*/
int flag = 0;

int main() {
	int map[255];
	int nNumbers;
	int i;
	int number;
	TreeType tree;
	char ch;

	init(map);
	tree = NULL;
	scanf("%d", &nNumbers);
	for (i = 0; i < nNumbers; i++) {
		number = 0;
		ch = getchar();
		while (isspace(ch)) ch = getchar();
		while (ch != '\n') {
			if ((ch >= 'A' && ch <= 'Y' && ch != 'Q') 
					|| (ch >= '0' && ch <= '9')) {
				number *= 10;
				number += map[ch];
			}
			ch = getchar();
		}
		addNode(&tree, number);
	}
	go(&tree);
	if (!flag) {
		printf("No duplicates.\n");
	}
	return 0;
}

void addNode(TreeType* tree, int number) {
	if (*tree == NULL) {
		*tree = malloc(sizeof(NodeType));
		(*tree)->number = number;
		(*tree)->duplicate = 1;
		(*tree)->left = NULL;
		(*tree)->right = NULL;
		return;
	}
	if ((*tree)->number == number) {
		(*tree)->duplicate++;
	} else {
		if (number > (*tree)->number) {
			addNode(&((*tree)->right), number);
		} else {
			addNode(&((*tree)->left), number);
		}
	}
}

void go(TreeType* tree) {
	if (*tree != NULL) {
		go(&((*tree)->left));
		if ((*tree)->duplicate > 1) {
			flag = 1;
			printf("%03d-%04d %d\n", (*tree)->number / 10000,
					 (*tree)->number % 10000, (*tree)->duplicate);
		}
		go(&((*tree)->right));
		free(*tree);
		*tree = NULL;
	}
}

void init(int map[]) {
	map['0'] = 0;
	map['1'] = 1;
	map['2'] = 2;
	map['3'] = 3;
	map['4'] = 4;
	map['5'] = 5;
	map['6'] = 6;
	map['7'] = 7;
	map['8'] = 8;
	map['9'] = 9;
	map['A'] = map['B'] = map['C'] = 2;
	map['D'] = map['E'] = map['F'] = 3;
	map['G'] = map['H'] = map['I'] = 4;
	map['J'] = map['K'] = map['L'] = 5;
	map['M'] = map['N'] = map['O'] = 6;
	map['P'] = map['R'] = map['S'] = 7;
	map['T'] = map['U'] = map['V'] = 8;
	map['W'] = map['X'] = map['Y'] = 9;
}

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