判断回文数

ACM 766

判断回文数:

第一种方法:将数倒置,与原数比较

void f(int x)
{
    int y=0;
    while(x>0)
    {
        y=(x%10)+y*10;
        x/=10;
    }
    if(x==y)
        cout>>"回文数">>endl;
    else
        cout>>"不是回文数">>endl;
}

第二种方法:用数组代替,进而进行比较

void f(int x[])
{
    int len=strlen(x);
    int n=len/2;
    for(int i=0;i<n;i++)
    {
        if(x[i]!=x[len-i-1])
            cout>>"不是回文数">>endl;
    }
    if(i>=n)
        cout>>"是回文数">>endl;
}

第三种方法:指针比较,与第二种类似

第四种方法:接收数字,通过函数转换成数组

itoa()函数:

# include <stdio.h>
# include <stdlib.h>
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}

itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制...

itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似

posted @ 2016-01-25 03:39  chris_chan1024  阅读(882)  评论(0编辑  收藏  举报