1024. Palindromic Number (25)

老是忘了特判!!

本身就是回文数忘了,俩个测试点没过

还有k==0的时候,好像这个测试点没测出来?不过我写的代码还是特判一下,不然出错了


#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void slove();
int main(){
	slove();
	return 0;
}
int k;
void  func(string str, int num);
string Rev(string str);
string Plus(string a, string b);
bool isPalindromic(string);
void slove(){
	string n;
	cin >> n >> k;
	if (isPalindromic(n)){
		cout << n << endl << "0" << endl;
		return;
	}
	if (k == 0){
		cout << n << endl << k << endl;
		return;
	}
	func(n, 1);
}
void func(string str, int num){
	string rev = Rev(str);
	string res = Plus(str, rev);
	if (isPalindromic(res)){
		cout << res << endl;
		cout << num << endl;
		return;
	}
	else if (num == k){
		cout << res << endl << num << endl;
		return;
	}
	else{
		func(res, num + 1);
	}
}
string Rev(string str){
	string t="";
	for (int i = 0; i < str.size(); ++i){
		t = str.at(i) + t;
	}
	return t;
}
string Plus(string a, string b){
	string res = "";
	int lena = a.size();
	int lenb = b.size();
	int jw = 0;
	int sum;
	for (int i = lena-1,  j=lenb-1; i >-1||j>-1; --i,--j){
		if (i < 0) sum = b[j] - '0';
		else if (j < 0) sum = a[i] - '0';
		else sum = a[i] - '0' + b[i] - '0';
		int c = sum + jw;
		jw = c / 10;
		c = c % 10;
		char cc = c + '0';
		res = cc + res;
	}
	while (jw){
		int c = jw % 10;
		char cc = c + '0';
		res = cc + res;
		jw /= 10;
	}
	return res;
}
bool isPalindromic(string str){
	string s = str;
	reverse(str.begin(), str.end());
	if (s==str)
		return true;
	return false;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值