[计蒜客][数组]回文数 原创

题目来源 计蒜客程序设计竞赛基础课(蓝桥杯省赛)

算法标签

题目描述

在这里插入图片描述

思路

写的过于复杂,全部备注在AC代码部分

到最后想起来不论针对的是奇数还是偶数个的数字,回文都只需要逆序即可

调试过程代码

#include<iostream>

using namespace std;

const int N = 1e5 + 10;
int a[N], b[N];
int tmpc[N];
int sumcnt = 0;

void swap(int &a, int &b) { a ^= b ^= a ^= b; }

int tmpans = 0;

void putNums(int n)
{
	int t1 = 0;
	while (n)
	{
		a[++t1] = n % 10;
		n /= 10;
	}
	//cout << "t1"<<t1;
	//cout << b[t1] << endl;

	int tmpidx = 0;
	//for (int i = 1; i <= t1; i++)cout << a[i] << " ";
	//cout << endl;
	for (int i = t1; i; i--)b[++tmpidx] = a[i];
	//cout << b[t1-1] << endl;
}

bool isB(int n)
{
	int backans = n;
	int t1 = 0;
	while (n)
	{
		a[++t1] = n % 10;
		n /= 10;
	}
	//cout << t1<<endl;
	tmpans = 0;
	for (int i = 1; i <= t1; i++)tmpans = tmpans * 10 + a[i];
	//for (int i = 1; i <= t1; i++)cout << "a" << i <<"->"<< a[i] <<"       ";
	//cout << endl;
	//cout << "TMPans"<<tmpans;
	if (tmpans == backans)return true;

	return false;
}

void checkCnt(int n)
{
	sumcnt++;
	if (isB(n) == true)return;
	else if (isB(n) == false);

	int tmpN = n;

	int cnt = 0;
	while (n) { n /= 10; cnt++; }

	int r = cnt, l = 1, mid = l + r >> 1;

	for (int i = 1; i <= cnt; i++)tmpc[i] = b[i];
	int tmpidx = 0;
	for (int i = cnt; i; i--)b[++tmpidx] = tmpc[i];


	int tmpN2 = 0;
	for (int i = 1; i <= cnt; i++)tmpN2 = tmpN2 * 10 + b[i];

	int NewTmpAns = tmpN + tmpN2;

	if (isB(NewTmpAns)) { return; }
	else if (isB(NewTmpAns) == false) { putNums(NewTmpAns); checkCnt(NewTmpAns); }
}

void check(int n)
{
	cout << n;
	if (isB(n) == true)return;
	else if (isB(n) == false);
	//cout << 111111;

	int tmpN = n;

	int cnt = 0;
	while (n) { n /= 10; cnt++; }
	//cout << "cnt" << cnt << endl;

	int r = cnt, l = 1, mid = l + r >> 1;

	//cout << endl << "sumr " << r << "suml " << l << "sumMid " << mid << endl;

	//for (int i = 1; i <= cnt; i++) cout << " " << b[i];
	//cout << endl;

	for (int i = 1; i <= cnt; i++)tmpc[i] = b[i];
	int tmpidx = 0;
	for (int i = cnt; i; i--)b[++tmpidx] = tmpc[i];


	int tmpN2 = 0;
	for (int i = 1; i <= cnt; i++)tmpN2 = tmpN2 * 10 + b[i];
	//for (int i = 1; i <= cnt; i++) cout<<" "<< b[i];
	//cout << endl;
	//cout << "TMP2" << tmpN2;

	int NewTmpAns = tmpN + tmpN2;

	//cout << endl;
	//cout << tmpN << " " << tmpN2 << endl;
	//cout << endl;
	//cout << "TMP"<<tmpN2<<endl;

	cout << "--->";
	//getchar();
	//getchar();

	if (isB(NewTmpAns)) { cout << NewTmpAns; return; }
	else if (isB(NewTmpAns) == false) { putNums(NewTmpAns); check(NewTmpAns); }
}

int main()
{
	int n;
	cin >> n;

	checkCnt(n);

	cout << sumcnt - 1 << endl;

	putNums(n);
	check(n);


	return 0;
}

AC代码

#include<iostream>

using namespace std;

const int N = 1e5 + 10;
int a[N], b[N];
int tmpc[N];
int sumcnt = 0;

void swap(int &a, int &b) { a ^= b ^= a ^= b; }//交换

int tmpans = 0;

void putNums(int n)
{
	int t1 = 0;
	while (n)
	{
		a[++t1] = n % 10;
		n /= 10;
	}

	int tmpidx = 0;

	for (int i = t1; i; i--)b[++tmpidx] = a[i];//拆分之后存放,此时B数组是正序,A数组是逆序
}

bool isB(int n)//检查是不是回文
{
	int backans = n;
	int t1 = 0;
	while (n)
	{
		a[++t1] = n % 10;
		n /= 10;
	}
	
	tmpans = 0;
	for (int i = 1; i <= t1; i++)tmpans = tmpans * 10 + a[i];

	if (tmpans == backans)return true;

	return false;
}

void checkCnt(int n)//计数
{
	sumcnt++;
	if (isB(n) == true)return;
	else if (isB(n) == false);

	int tmpN = n;

	int cnt = 0;
	while (n) { n /= 10; cnt++; }

	int r = cnt, l = 1, mid = l + r >> 1;

	for (int i = 1; i <= cnt; i++)tmpc[i] = b[i];
	int tmpidx = 0;
	for (int i = cnt; i; i--)b[++tmpidx] = tmpc[i];


	int tmpN2 = 0;
	for (int i = 1; i <= cnt; i++)tmpN2 = tmpN2 * 10 + b[i];

	int NewTmpAns = tmpN + tmpN2;

	if (isB(NewTmpAns)) { return; }
	else if (isB(NewTmpAns) == false) { putNums(NewTmpAns); checkCnt(NewTmpAns); }
}

void check(int n)//主要自定义函数
{
	cout << n;//输出当前的数字
	if (isB(n) == true)return;//如果是回文就返回
	else if (isB(n) == false);

	int tmpN = n;

	int cnt = 0;
	while (n) { n /= 10; cnt++; }//计算位数

	int r = cnt, l = 1, mid = l + r >> 1;

	for (int i = 1; i <= cnt; i++)tmpc[i] = b[i];
	int tmpidx = 0;
	for (int i = cnt; i; i--)b[++tmpidx] = tmpc[i];//再次逆序存储N


	int tmpN2 = 0;
	for (int i = 1; i <= cnt; i++)tmpN2 = tmpN2 * 10 + b[i];//得到回文N
    
	int NewTmpAns = tmpN + tmpN2;//正序加逆序

	cout << "--->";

	if (isB(NewTmpAns)) { cout << NewTmpAns; return; }//如果是回文,输出当前,返回
	else if (isB(NewTmpAns) == false) { putNums(NewTmpAns); check(NewTmpAns); }//如果不是回文,重新调用
}

int main()
{
	int n;
	cin >> n;

	checkCnt(n);//为了把数量算出来,调用一边check只计数

	cout << sumcnt - 1 << endl;

	putNums(n);//把目标值拆分存入数组
	check(n);//主要自定义函数


	return 0;
}
posted @   俺叫西西弗斯  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示