洛谷 T59576 下一个回文树(zhoutbafo)

Description

原题链接

Solution

嗯,一道私题,一道大模拟,细节巨多,ztb学长建议我们做的,有助于提高码力。

nekko哥哥写了6.73k的代码。

我个人认为我码力非常的差QWQ,于是就写了。

调了一下午,总算是调出来了。

呼~好有成就感。

我也不打算写题解了(一道大模拟有什么好写的,是吧),只是为了纪念一下。

当然需要的话也可以拿去对拍。

Code(2.94k)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define zuo(x) x
#define you(x) 2 * mid - x + (!t)

using namespace std;

const int N = 2e5 + 10;
int T, flag, xiaoshu;
int a[N], b[N], c[N], d[N];
int z[N], x[N];
char s[N];
int jinwei, bj9, fu;

void init(){
	scanf("%s", s + 1);
	int len = strlen(s + 1);
	int st = 0, pos = 0, End = len;
	if(s[1] == '-')
		fu = 1, st = 1;
	for(int i = 1 + st; i <= len; i++){
		if(s[i] == '.'){
			pos = i;
			break;
		}
	}
	if(pos)
		for(int i = 1 + st; i < pos; i++)
			a[i - st] = s[pos - i + st] - '0';
	else
		for(int i = 1 + st; i <= len; i++)
			a[i - st] = s[len - i + st + 1] - '0';
	a[0] = pos ? pos - st - 1: len - st;
	memcpy(c, a, sizeof(a));
	if(!pos) return;
	while(s[End] == '0') End--;
	if(End - pos) xiaoshu = 1;
	for(int i = pos + 1; i <= End; i++)
		b[i - pos] = s[End - (i - pos) + 1] - '0';
	b[0] = End - pos;
}

void solve(int a[]){
	int mid = (a[0] + 1) >> 1;
	int t = a[0] & 1;		//t=0表示长度为偶数,t=1表示长度为奇数
	for(int i = mid; i >= 1; i--){
		if(a[zuo(i)] != a[you(i)]){
			flag = 0;
			break;
		}
	};
	int i = mid;
	if(flag){
		while(a[zuo(i)] == 9 && zuo(i) >= 1) bj9++, i--; 
		if(bj9 == mid){
			jinwei = 1;
			c[1] = 1;
			for(int i = 2; i <= c[0]; i++)
				c[i] = 0;
			c[++c[0]] = 1;
		}
		else{
			if(zuo(i) != you(i))
				a[zuo(i)]++, a[you(i)]++;
			else a[zuo(i)]++;
		}
	}else{
		while(a[zuo(i)] == a[you(i)]) bj9 += (a[zuo(i)] == 0), i--;
		if(a[zuo(i)] < a[you(i)]){
			for(int j = zuo(i); j >= 1; j--)
				a[j] = a[2 * mid - j + (!t)];
		}else{
			int res = mid;
			while(a[zuo(res)] == a[you(res)] && a[res] == 9) res--;
			a[zuo(res)] = a[you(res)] = a[you(res)] + 1;
			for(int j = zuo(res) - 1; j >= 1; j--)
				a[zuo(j)] = a[you(j)];
			for(int j = zuo(res) + 1; j <= you(res) - 1; j++)
				a[j] = 0;
		}
	}
}

void gsub(int a[], int b[], int c[], int d[]){
	int i;
	if(xiaoshu) c[1]--;
	for(i = 1; i <= c[0]; i++){
		if(c[i] < a[i]){
			c[i] += 10;
			c[i + 1]--;
		}
		c[i] -= a[i];
	}
	while(!c[c[0]] && c[0] > 0) c[0]--;
	if(xiaoshu){
		d[0] = b[0] + 1;
		d[d[0]] = 1;
		for(i = 1; i <= d[0]; i++){
			if(d[i] < b[i]){
				d[i] += 10;
				d[i + 1]--;
			}
			d[i] -= b[i];
		}
		d[0]--;
	}
}

int main(){
	scanf("%d", &T);
	while(T--){
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		memset(d, 0, sizeof(d));
		jinwei = bj9 = xiaoshu = fu = 0;
		flag = 1;
		init();
		if(fu){
			a[1]++;
			int pos = 1;
			while(a[pos] >= 10){
				a[pos] = 0;
				a[++pos]++;
			}
			if(pos > a[0]) a[0] = pos;
			for(int i = a[0]; i >= 1; i--)
				printf("%d", a[i]);
			if(xiaoshu){
				if(!a[0]) printf("0");
				printf(".");
				for(int i = b[0]; i >= 1; i--)
					printf("%d", b[i]);
			}
			printf("
");
			continue;
		}
		solve(c);
				gsub(a, b, c, d);
				for(int i = c[0]; i >= 1; i--)
					printf("%d", c[i]);
				if(xiaoshu){
					if(!c[0]) printf("0");
					printf(".");
					for(int i = d[0]; i >= 1; i--)
						printf("%d", d[i]);
				}
				printf("
");

	}
	return 0;
}

End

本文来自博客园,作者:xixike,转载请注明原文链接:https://www.cnblogs.com/xixike/p/15125396.html

原文地址:https://www.cnblogs.com/xixike/p/15125396.html