当前位置:网站首页>C language 171 Number of recent palindromes
C language 171 Number of recent palindromes
2022-04-23 02:37:00 【yangqin@1225】
Title Description
Enter an integer , Find the palindrome number with the smallest absolute value of its difference . When there are two solutions , Take the smaller solution .
Input
Enter as one 行, Include an integer N , Integers N The length of is 1 To 1000 Between ( contain 1000 ).
Output
The output has only one integer , Is the palindrome number with the smallest absolute value of the difference from the input number .

Problem analysis : Big data , You should consider converting the input to a string and then calculating
#include <stdio.h>
#include <string.h>
int is_backnum_str(char *n)
{
int len = strlen(n);
int left = 0, right = len - 1;
while (left < right)
{
if (n[left] != n[right])
return 0;
left++;
right--;
}
return 1;
}
char *get_num_str(char *a)
{
int len=strlen(a);
int i=0;
while(i<len && a[i]=='0'){
i++;
}
char t[1024];
strcpy(t,a);
strcpy(a,&t[i]);
return a;
}
int compare_abstr(char *a, char *b)
{
// Delete the previous redundant 0 Then compare the size
get_num_str(a);
get_num_str(b);
int lena = strlen(a), lenb = strlen(b);
if (lena > lenb)
return 1;
if (lena < lenb)
return -1;
return strcmp(a, b);
}
void swap(char *a, char *b)
{
char t[1024] = {
0};
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}
// When it comes in , Guarantee a>=b
char *add(char *a, char *b, char *ans)
{
int lena = strlen(a), lenb = strlen(b), maxlen = strlen(a) + 1;
memset(ans, '0', maxlen);
int i = lena - 1, j = lenb - 1, k = maxlen - 1, bit = 0;
while (j >= 0 || i >= 0)
{
bit = j >= 0 ? a[i] - '0' + b[j] - '0' : a[i] - '0';
ans[k] += bit;
if (ans[k] > '9')
{
ans[k] -= 10;
ans[k - 1] += 1;
}
j--;
i--;
k--;
}
get_num_str(ans);
return ans;
}
// a-b, When incoming, ensure that a>=b
char *sub(char *a, char *b, char *ans)
{
int lena = strlen(a), lenb = strlen(b), maxlen = strlen(a);
memset(ans, '0', maxlen);
int i = lena - 1, j = lenb - 1, k = maxlen - 1, bit = 0;
while (j >= 0 || i >= 0)
{
bit = j >= 0 ? (a[i] - '0') - (b[j] - '0') : a[i] - '0';
ans[k] += bit;
if (ans[k] < '0')
{
ans[k] += 10;
ans[k - 1] -= 1;
}
j--;
i--;
k--;
}
get_num_str(ans);
return ans;
}
char *reverse(char *src)
{
// The number of palindromes entered , Then return directly
if (is_backnum_str(src))
return src;
int len = strlen(src), left = 0, right = len - 1;
while (left < right)
{
src[right--] = src[left++];
}
return src;
}
int main()
{
char src[1024] = {
0};
scanf("%s", src);
// If it's palindrome number , Go straight back to
if (is_backnum_str(src))
{
printf("%s\n", src);
return 0;
}
char ans1[1024] = {
0}, ans2[1024] = {
0}; // Store two solutions
strcpy(ans1, src);
reverse(ans1); // Get the first solution
int len = strlen(src), idx = (len - 1) / 2;
char tmp[1024] = {
0}; // Storage is +/- The amount of
memset(tmp, '0', len);
tmp[idx] = '1'; // Only the first one idx For the purpose of 1, For all other 0;
char res[1024] = {
0}; // Storage ans1 +/- tmp The amount of
// Calculate the second solution , If ans1=src, explain src Itself is palindrome number , This situation has been dealt with earlier , So here the two must not be equal
// 1. ans1>src, Such as the input src=91210,ans1=91219,ans2: take ans1-100=91119;
// 2. ans1<src, Such as the input src=10219,ans1=10201,ans2: take ans1+100=10301
// 3. Consider subtraction and abdication : src=91010,ans1=91019,ans2: take ans1-100=90919(ans1-tmp=res), then 90919(res) Flip to get ans2=90909; Calculation tmp when ,idx=(5-1)/2; That is, only idx by 1, For all other 0;
// 4. Consider the case of additive carry : src=10919,ans1=10901, ans2: take ans1+100=11001(ans1+tmp=res), then 11001(res) Flip to get ans2=11011
// 5. Consider special circumstances 1, The change of total digits after subtraction and abdication , Such as : src=1000, ans1=1001, ans2: take ans1-100 obtain 901(ans1-tmp=res), Turn it over and get 909,909 Than src Less 1 position , At this time will be (idx+1) Set as 9 that will do , namely ans2=999;
// 6. Consider special circumstances 2, The change of the total number of digits after addition and carry ,9998>9999>9899>9889. To add and carry , The front must be 99……, Got ans1>src, therefore ans2 It's impossible to add , That is, it is impossible to add and carry .
strcpy(ans2, ans1);
if (compare_abstr(ans1, src) > 0)
sub(ans2, tmp, res); // situation 1, situation 3 ( stay add and sub The carry has been handled in the method / Abdication )
else
add(ans2, tmp, res); // situation 2, situation 4
if (strlen(res) < strlen(ans1))
res[idx] = '9'; // situation 5:1000>1001>991>999
reverse(res);
strcpy(ans2, res);
// printf("the two val:%s,%s\n", ans1, ans2);
if (compare_abstr(ans1, ans2) > 0)
{
swap(ans1, ans2); // ans1<src<ans2
}
char diff1[1024] = {
0}, diff2[1024] = {
0}; // Store two solutions and ans1,ans2 The difference between the
sub(src, ans1, diff1);
sub(ans2, src, diff2);
printf("%s\n", compare_abstr(diff1, diff2) <= 0 ? ans1 : ans2);
return 0;
}

Beginners C Language , If there is any optimization in code writing , Welcome to correct ~
leetcode Same topic
Partial optimization : If the input is palindrome number , The calculation ± Number of palindromes after
#include <stdio.h>
#include <string.h>
#define MAX 20
int is_backnum_str(char *n)
{
int len = strlen(n);
int left = 0, right = len - 1;
while (left < right)
{
if (n[left] != n[right])
return 0;
left++;
right--;
}
return 1;
}
char *get_num_str(char *a)
{
int len=strlen(a);
if(len==1) return a;
int i=0;
while(i<len && a[i]=='0'){
i++;
}
char t[MAX];
strcpy(t,a);
strcpy(a,&t[i]);
return a;
}
int compare_abstr(char *a, char *b)
{
// Delete the previous redundant 0 Then compare the size
get_num_str(a);
get_num_str(b);
int lena = strlen(a), lenb = strlen(b);
if (lena > lenb)
return 1;
if (lena < lenb)
return -1;
return strcmp(a, b);
}
void swap(char *a, char *b)
{
char t[MAX] = {
0};
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}
// When it comes in , Guarantee a>=b
char *add(char *a, char *b, char *ans)
{
int lena = strlen(a), lenb = strlen(b), maxlen = strlen(a) + 1;
memset(ans, '0', maxlen);
int i = lena - 1, j = lenb - 1, k = maxlen - 1, bit = 0;
while (j >= 0 || i >= 0)
{
bit = j >= 0 ? a[i] - '0' + b[j] - '0' : a[i] - '0';
ans[k] += bit;
if (ans[k] > '9')
{
ans[k] -= 10;
ans[k - 1] += 1;
}
j--;
i--;
k--;
}
get_num_str(ans);
return ans;
}
// a-b, When incoming, ensure that a>=b
char *sub(char *a, char *b, char *ans)
{
int lena = strlen(a), lenb = strlen(b), maxlen = strlen(a);
memset(ans, '0', maxlen);
int i = lena - 1, j = lenb - 1, k = maxlen - 1, bit = 0;
while (j >= 0 || i >= 0)
{
bit = j >= 0 ? (a[i] - '0') - (b[j] - '0') : a[i] - '0';
ans[k] += bit;
if (ans[k] < '0')
{
ans[k] += 10;
ans[k - 1] -= 1;
}
j--;
i--;
k--;
}
get_num_str(ans);
return ans;
}
char *reverse(char *src)
{
// The number of palindromes entered , Then return directly
if (is_backnum_str(src))
return src;
int len = strlen(src), left = 0, right = len - 1;
while (left < right)
{
src[right--] = src[left++];
}
return src;
}
char * nearestPalindromic(char * src){
char ans1[MAX] = {
0}, ans2[MAX] = {
0}; // Store two solutions
int len = strlen(src), idx = (len - 1) / 2;
char tmp[MAX] = {
0}; // Storage is +/- The amount of
memset(tmp, '0', len);
tmp[idx] = '1'; // Only the first one idx For the purpose of 1, For all other 0;
char res[MAX] = {
0}; // Storage ans1 +/- tmp The amount of
// The input itself is the palindrome number ,src=22,ans1=11,ans2:33( Independent computing logic , , respectively, -1,+1 Then reverse it )
if(is_backnum_str(src)){
sub(src,tmp,ans1);
if (strlen(ans1) < len) ans1[idx]='9';
add(src,tmp,ans2);
reverse(ans1);
reverse(ans2);
}else{
strcpy(ans1, src);
reverse(ans1); // Get the first solution
// Calculate the second solution
// 1. ans1>src, Such as the input src=91210,ans1=91219,ans2: take ans1-100=91119;
// 2. ans1<src, Such as the input src=10219,ans1=10201,ans2: take ans1+100=10301
// 3. Consider subtraction and abdication : src=91010,ans1=91019,ans2: take ans1-100=90919(ans1-tmp=res), then 90919(res) Flip to get ans2=90909; Calculation tmp when ,idx=(5-1)/2; That is, only idx by 1, For all other 0;
// 4. Consider the case of additive carry : src=10919,ans1=10901, ans2: take ans1+100=11001(ans1+tmp=res), then 11001(res) Flip to get ans2=11011
// 5. Consider special circumstances 1, The change of total digits after subtraction and abdication , Such as : src=1000, ans1=1001, ans2: take ans1-100 obtain 901(ans1-tmp=res), Turn it over and get 909,909 Than src Less 1 position , At this time will be (idx+1) Set as 9 that will do , namely ans2=999;
// 6. Consider special circumstances 2, The change of the total number of digits after addition and carry ,9998>9999>9899>9889. To add and carry , The front must be 99……, Got ans1>src, therefore ans2 It's impossible to add , That is, it is impossible to add and carry .
strcpy(ans2, ans1);
if (compare_abstr(ans1, src) >0){
sub(ans2, tmp, res); // situation 1, situation 3 ( stay add and sub The carry has been handled in the method / Abdication )
if (strlen(res) < strlen(ans1)) res[idx] = '9'; // situation 5:1000>1001>991>999
}else{
add(ans2, tmp, res); // situation 2, situation 4
}
reverse(res);
strcpy(ans2, res);
}
if (compare_abstr(ans1, ans2) > 0) swap(ans1, ans2); // ans1<src<ans2
char diff1[MAX] = {
0}, diff2[MAX] = {
0}; // Store two solutions and ans1,ans2 The difference between the
sub(src, ans1, diff1);
sub(ans2, src, diff2);
src=(char *)malloc(sizeof(char)*MAX);
strcpy(src,compare_abstr(diff1, diff2) <= 0 ? ans1 : ans2);
return src;
}

版权声明
本文为[yangqin@1225]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230236319093.html
边栏推荐
- Interpretation of the future development of smart agriculture
- [XJTU computer network security and management] Lecture 2 password technology
- arduino esp8266 网络升级 OTA
- 5W of knowledge points
- 十六、异常检测
- If you want to learn SQL with a Mac, you should give yourself a good reason to buy a Mac and listen to your opinions
- Applet canvas canvas half ring
- 小程序 canvas 画布半圆环
- 认识进程(多线程_初阶)
- [untitled]
猜你喜欢

002_ Redis_ Common operation commands of string type

Implementation of distributed scenario business operation log (based on redis lightweight)
![[xjtu Computer Network Security and Management] session 2 Cryptographic Technology](/img/b0/263e8dcbfeb2ce9f504a9c8eb76b07.png)
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology

Flink stream processing engine system learning (I)

Day18 -- stack queue

Fashion MNIST 数据集分类训练

Real math problems in 1958 college entrance examination
![[untitled]](/img/60/421cda552055664357af47d1a956af.png)
[untitled]

006_ redis_ Jedis quick start

Web learning record (medium)
随机推荐
Handwritten memory pool and principle code analysis [C language]
Talk about current limiting
Implementation of distributed scenario business operation log (based on redis lightweight)
数仓建表111111
5W of knowledge points
机器学习(周志华) 第十四章概率图模型
Push data from onenet cloud platform to database
MySQL C language connection
They are all intelligent in the whole house. What's the difference between aqara and homekit?
Intelligent agricultural management model
期中汇总(概论+应用层+运输层)
arduino esp8266 网络升级 OTA
1、 Sequence model
程序设计天梯赛 L1-49 天梯赛分配座位(模拟),布响丸辣
Web learning record (medium)
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
Synchronized lock and its expansion
Execute external SQL script in MySQL workbench and report error
Parental delegation model [understanding]
Renesas electronic MCU RT thread development and Design Competition