A1024 Palindromic Number (25分)(大整数的四则运算)

一、技术总结见上篇博客

二、参考代码

#include<iostream>
#include<cstring>
using namespace std;
struct bign{
	int d[1000];
	int len;
	bign(){
		memset(d, 0, sizeof(d));
		len = 0;
	}
};
bign change(char str[]){
	bign c;
	for(int i = 0; i < strlen(str); i++){
		c.d[c.len++] = str[strlen(str) - 1 - i] - '0';
	}
	return c;
}
bign add(bign a){
	bign c;
	int carry = 0;
	for(int i = 0; i < a.len; i++){
		int temp = a.d[i] + a.d[a.len - 1 - i] + carry;
		c.d[c.len++] = temp % 10;
		carry = temp / 10;
	}
	if(carry != 0){
		c.d[c.len++] = carry;
	}
	return c;
}
bool judge(bign a){
	for(int i = 0; i < a.len/2; i++){
		if(a.d[i] != a.d[a.len - 1 - i]) return false;
	}
	return true;
}
int main(){
	int k, cnt = 0;
	char str[1000];
	scanf("%s%d", str, &k);
	bign c = change(str);
	for(int i = 0; i < k; i++){
		if(i == 0 && judge(c)){
			break;
		}
		c = add(c);
		if(judge(c)){
			cnt++;
			break;
		}
		cnt++;
	}
	for(int i = c.len - 1; i >= 0; i--){
		printf("%d", c.d[i]);
		if(i == 0) printf("\n");
	}	
	printf("%d", cnt);
	return 0;
}
posted @ 2020-07-01 21:36  睿晞  阅读(200)  评论(0编辑  收藏  举报