NOIP 1999 邮票面值设计 解题报告

  纯模拟吧~~细节不阐述了,代码如下:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100001
int str[MAX];
int tmp[MAX];
int len;
int k;

int huiwen(void)
{
	int i = 0, j = len - 1;
	while(i < j){
		if(str[MAX - 1 - i] != str[MAX - 1 - j]){
			return 0;
		}
		i++, j--;
	}
	return 1;
}

void deal(void)
{
	int i;
	int j = 0;
	memcpy(tmp, str, sizeof(str));
	for(i = 0; i < len; i++){
		str[MAX - i - 1] = str[MAX - i - 1] + tmp[MAX - len + i] + j;
		j = str[MAX - i - 1] / k;
		str[MAX - i - 1] %= k;
	}
	if(j){
		str[MAX - i - 1] = j;
		len++;
	}
}

void getnum(void)
{
	char c;
	if(scanf("%c", &c) != 1){
		return;
	}
	if(isspace(c)){
		return;
	}
	getnum();
	str[MAX - 1 - len] = (int)c;
	if(isdigit(c)){
		str[MAX - 1 - len] -= '0';
	}else if(isalpha(c)){
		str[MAX - 1 - len] = toupper(str[MAX - 1 - len]);
		str[MAX - 1 - len] -= 'A';
		str[MAX - 1 - len] += 10;
	}
	len++;
}

int main(int argc, char **argv)
{
	int i;
	scanf("%d\n", &k);
	getnum();
	for(i = 0; !huiwen() && i <= 30; i++){
		deal();
	}
	if(i == 31){
		printf("Impossible\n");
	}else{
		printf("%d\n", i);
	}
	return 0;
}

  

posted @ 2011-07-29 15:46  zqynux  阅读(1243)  评论(0编辑  收藏  举报