[codeforces 1269C] Long Beautiful Integer 字符串处理(类高精度加)

[codeforces 1269C]  Long Beautiful Integer  字符串处理(类高精度加)

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1269/problem/C
 

Problem Lang Verdict Time Memory
C - Long Beautiful Integer GNU C++11 Accepted 31 ms 200 KB

思考过程如下图所示:

该题能很快进入代码层面的主要原因是,上述模拟过程,数据选得好。2019-12-26

/*
3 2
192
3
202
错了上述这组数据,关于9的处理想是想到了,如上,错误处理成3 292,
其实上述192->202有些类似高精度加,当然,该题,没有必要编得这么复杂。
2019-12-16
*/

#include <stdio.h>
#include <string.h>
#define maxn 200010
char a[maxn],b[maxn];
int N,K;
int main(){
	int i,j,pos,check=0,ed;
	scanf("%d%d",&N,&K);
	scanf("%s",a+1);
	strcpy(b+1,a+1);
	for(i=1;i<=K;i++)
		for(j=i;j<=N;j+=K)
			b[j]=b[i];
	for(i=1;i<=N;i++)
		if(b[i]>a[i])break;
		else if(b[i]<a[i]){check=1;break;}
	if(!check)printf("%d\n%s\n",N,b+1);
	else{
		if(i>K)pos=K;
		else pos=i;
		ed=pos;
		while(pos>=1&&a[pos]=='9')pos--;
		if(pos==0){
			a[0]='1';
			for(i=1;i<=ed;i++)a[i]='0';
			strcpy(b,a);
			for(i=0;i<K;i++)
				for(j=i;j<=N;j+=K)
					b[j]=b[i];
			printf("%d\n%s\n",N+1,b);
		}
		else{
			a[pos]=a[pos]+1;
			for(i=pos+1;i<=ed;i++)a[i]='0';
			strcpy(b+1,a+1);
			for(i=1;i<=K;i++)
				for(j=i;j<=N;j+=K)
					b[j]=b[i];
			printf("%d\n%s\n",N,b+1);
		}
	}
	return 0;
}
发布了475 篇原创文章 · 获赞 509 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/103715361
今日推荐