当前位置:网站首页>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
边栏推荐
- leetcode 烹饪料理
- 双亲委派模型【理解】
- Rhcsa second day operation
- tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- day18--栈队列
- 每日一题(2022-04-22)——旋转函数
- JVM class loader
- 机器学习(周志华) 第十四章概率图模型
- They are all intelligent in the whole house. What's the difference between aqara and homekit?
- IAR嵌入式開發STM32f103c8t6之點亮LED燈
猜你喜欢

【无标题】
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售

Flink stream processing engine system learning (I)

认识进程(多线程_初阶)

Day18 -- stack queue

VMware virtual machine installation openwrt as side route single arm route img image to vmdk

高效音乐格式转换工具Music Converter Pro

Halo open source project learning (I): project launch

下载正版Origin Pro 2022 教程 及 如何 激 活

SO库依赖问题
随机推荐
Use of go language web Middleware
Rhcsa day 4 operation
IAR嵌入式开发STM32f103c8t6之点亮LED灯
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
After idea is successfully connected to H2 database, there are no sub files
Daily question (April 22, 2022) - rotation function
How many steps are there from open source enthusiasts to Apache directors?
Intelligent agricultural management model
Global, exclusive and local routing guard
Understanding process (multithreading primary)
IAR embedded development stm32f103c8t6 Lighting LED
tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
[XJTU计算机网络安全与管理]第二讲 密码技术
JSP page nesting
每日一题(2022-04-22)——旋转函数
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
MySQL JDBC编程
Flink stream processing engine system learning (II)
Rhcsa day 1 operation
New book recommendation - IPv6 technology and application (Ruijie version)