当前位置:网站首页>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 烹飪料理
- PTA: 点赞狂魔
- 012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- 006_ redis_ Jedis quick start
- How to recognize products from the perspective of Dialectics
- Real math problems in 1958 college entrance examination
- Leetcode cooking
- Flink real-time data warehouse project - Design and implementation of DWS layer
- Go language ⌈ mutex and state coordination ⌋
- 使用Go语言构建Web服务器
猜你喜欢
随机推荐
Day18 -- stack queue
Target narak
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
Fast and robust multi person 3D pose estimation from multiple views
[XJTU computer network security and management] Lecture 2 password technology
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
wordpress 调用指定页面内容详解2 get_children()
WordPress calls the specified page content. 2 get_ children()
How big the program development of single chip microcomputer project can be, it represents your level of knocking code
牛客手速月赛 48 C(差分都玩不明白了属于是)
Latin goat (20204-2022) - daily question 1
小程序 canvas 画布半圆环
Develop a chrome plug-in from 0 (2)
每日一题(2022-04-21)——山羊拉丁文
Go语言web中间件的使用
006_ redis_ Sortedset type
006_ redis_ Jedis quick start
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
认识进程(多线程_初阶)
电源电路设计原来是这么回事