/*
求下一个数的回文数。步骤:假设长度为奇数(偶数方法相同)。
A:number是回文数。
a.考虑全是9的特殊情况,如:999 9 999,取前半部+1,即9999+1(1000 0),
然后将以mid为中心右边面的length/2-1与左边的lenght/2-1相等(1000 0 00),
最后一位等于1(1000 0 00 1).
b.非全是9的情况,如:1239321,取前半部加1,即1239+1,然后将其拷贝到后半个lenght/2的空间。
B: number不是回文数,如12345和54321,以3开始分别比较3两边数的大小。
当第一个number[mid-i] < number[mid+i]时,令number[mid]+1,即3+1,然后后半部分用前半部替代。
当第一个number[mid-i] > number[mid+i]时,后半部分用前半部替代。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
bool number_is_symmetric(char *Input);
char *get_symmetricIsTrue(char *Input);
char *get_symmetricIsFalse(char *Input);
int main(int argc, char **argv)
{
char *number = (char *)malloc(sizeof(char *)*N);
printf("输入回文数:\n");
scanf("%s", number);
printf("number:\t%s\n", number);
char *dst = nullptr;
bool is_symmetric = false;
is_symmetric = number_is_symmetric(number);
if (is_symmetric)
{
dst = get_symmetricIsTrue(number);
printf("dst:\t%s\n", dst);
free(dst);
free(number);
dst = nullptr;
number = nullptr;
}
else
{
dst = get_symmetricIsFalse(number);
printf("dst:\t%s\n", dst);
free(dst);
free(number);
dst = nullptr;
number = nullptr;
}
system("pause");
return 0;
}
bool number_is_symmetric(char *Input)
{
int len = 0;
int count = 0;
char *Input_contrast = nullptr;
len = strlen(Input);
Input_contrast = (char *)malloc(sizeof(char)*(len+1));
for (int i = len-1; i >= 0; i --)
{
Input_contrast[count++] = Input[i];
}
Input_contrast[count] = '\0';
if (0 == strcmp(Input, Input_contrast))
{
free(Input_contrast);
Input_contrast = nullptr;
return true;
}
else
{
free(Input_contrast);
Input_contrast = nullptr;
return false;
}
}
char *get_symmetricIsTrue(char *Input)
{
int len = 0;
int temp = 0;
char *Input_contrast = nullptr;
len = strlen(Input);
Input_contrast = (char *)malloc(sizeof(char)*(len+2));
if (1 == len && Input[0] != '9')
{
Input_contrast[0] = Input[0]+1;
Input_contrast[1] = '\0';
return Input_contrast;
}
if (1 == len%2)
{
memcpy(Input_contrast, Input, len/2+1);
Input_contrast[len/2+1] = '\0';
temp = atoi(Input_contrast);
temp ++;
itoa(temp, Input_contrast, 10);
int length_contrast = strlen(Input_contrast);
if ((len/2+1 == length_contrast))
{ //不是99999
for (int i = 0; i < len/2; i++)
{
Input_contrast[i+len/2+1] = Input_contrast[len/2-1-i];
}
Input_contrast[len] ='\0';
}
else
{ //是99999
for (int i = 0; i < len/2-1; i++)
{
Input_contrast[i+length_contrast] = Input_contrast[length_contrast-1-i];
}
Input_contrast[len] ='1';
Input_contrast[len+1] ='\0';
}
}
else
{
memcpy(Input_contrast, Input, len/2);
Input_contrast[len/2] = '\0';
temp = atoi(Input_contrast);
temp ++;
itoa(temp, Input_contrast, 10);
int length_contrast = strlen(Input_contrast);
if ((len/2 == length_contrast))
{ //不是9999
for (int i = 0; i < len/2; i++)
{
Input_contrast[i+len/2] = Input_contrast[len/2-1-i];
}
Input_contrast[len] ='\0';
}
else
{ //是9999
for (int i = 0; i < len/2-1; i ++)
{
Input_contrast[i+length_contrast] = Input_contrast[length_contrast-1-i];
}
Input_contrast[len] ='1';
Input_contrast[len+1] ='\0';
}
}
return Input_contrast;
}
char *get_symmetricIsFalse(char *Input)
{
int temp = 0;
int InputLength = strlen(Input);
char *Input_contrast = nullptr;
Input_contrast = (char *)malloc(sizeof(char)*(InputLength+1));
strcpy(Input_contrast, Input);
if (1 == InputLength%2)
{
for (int i = 0; i < InputLength/2; i ++)
{
if (Input_contrast[InputLength/2-1-i] < Input_contrast[InputLength/2+1+i])
{
memcpy(Input_contrast, Input, InputLength/2+1);
Input_contrast[InputLength/2+1] = '\0';
temp = atoi(Input_contrast);
temp ++;
itoa(temp, Input_contrast, 10);
for (int i = 0; i < InputLength/2; i++)
{
Input_contrast[i+InputLength/2+1] = Input_contrast[InputLength/2-1-i];
}
Input_contrast[InputLength] ='\0';
return Input_contrast;
}
else if (Input_contrast[InputLength/2-1-i] > Input_contrast[InputLength/2+1+i])
{
memcpy(Input_contrast, Input, InputLength/2+1);
Input_contrast[InputLength/2+1] = '\0';
for (int i = 0; i < InputLength/2; i++)
{
Input_contrast[i+InputLength/2+1] = Input_contrast[InputLength/2-1-i];
}
Input_contrast[InputLength] ='\0';
return Input_contrast;
}
}
}
else
{
for (int i = 0; i < InputLength/2; i ++)
{
if (Input_contrast[InputLength/2-1-i] < Input_contrast[InputLength/2+i])
{
memcpy(Input_contrast, Input, InputLength/2);
Input_contrast[InputLength/2] = '\0';
temp = atoi(Input_contrast);
temp ++;
itoa(temp, Input_contrast, 10);
for (int i = 0; i < InputLength/2; i++)
{
Input_contrast[i+InputLength/2] = Input_contrast[InputLength/2-1-i];
}
Input_contrast[InputLength] ='\0';
return Input_contrast;
}
else if (Input_contrast[InputLength/2-1-i] > Input_contrast[InputLength/2+i])
{
memcpy(Input_contrast, Input, InputLength/2);
Input_contrast[InputLength/2] = '\0';
for (int i = 0; i < InputLength/2; i++)
{
Input_contrast[i+InputLength/2] = Input_contrast[InputLength/2-1-i];
}
Input_contrast[InputLength] ='\0';
return Input_contrast;
}
}
}
}
这是 昨天亚马逊机考题的其中一道,lz当时没想到这么全面地情况,当时思路比较混乱,虽然大体上写出来了,但是由于不满足一些特定情况,不能运行成功。所以,完了以后自己重新写了一下。还有两道也是比较难的,后续再发。如有问题,望留言,万分感谢
输入一串数字,求比这个数大的下一个回文数
最新推荐文章于 2023-02-22 22:12:06 发布