当前位置:编程学习 > C/C++ >>

找出大于一个数的最小回文数

[cpp]
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
 
/************************************************************************************************************************************************************
****                                                    
****                                        begin:      2012 10 12
****                                                    回文数
****    找出大于一个数的最小回文数
****                                        end  :      2012 10 12
****
************************************************************************************************************************************************************/ 
void main(){ 
    char data[10] = {0}, res[10] = {0}, state[10] = {0}, len = 0, pos, bit = 0; 
    scanf("%s",data); 
 
    len = strlen(data); 
    if(len % 2 == 0){//偶 
        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); 
 
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,