下面代码内容是关于C语言找出大于一个数的最小回文数的代码,希望能对码农们有用途。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
char data[10] = {0}, res[10] = {0}, state[10] = {0}, len = 0, pos, bit = 0;
scanf("%s",data);
len = strlen(data);
pos = len/2 - 1;
}
else{
pos = len/2;
}
while(pos - bit >= 0){
if(len % 2 == 1){
if(data[pos - bit] >= data[pos + bit]){
res[pos - bit] = data[pos - bit];
res[pos + bit] = data[pos - bit];
}
else{
data[pos - bit + 1]++;
memset(&data[pos - bit + 2], ‘0‘, len - (pos - bit+1));
bit = 0;
continue;
}
bit ++;
}
else{
if(data[pos - bit] >= data[pos + 1 + bit]){
res[pos - bit] = data[pos - bit];
res[pos + 1 + bit] = data[pos - bit];
}
else{
data[pos - bit ]++;
memset(&data[pos - bit + 1], ‘0‘, len - (pos - bit+1) + 1);
bit = 0;
continue;
}
bit ++;
}
}
printf("回文数为%s", res);
}
原文地址:https://blog.51cto.com/14122754/2386672
时间: 2024-10-03 13:45:23