九度OJ 1074:对称平方数 (数字特性)

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:4804

解决:2173

题目描述:
打印所有不超过n(n<256)的,其平方具有对称性质的数。
如11*11=121
输入:

无任何输入数据

输出:
输出具有题目要求的性质的数。如果输出数据不止一组,各组数据之间以回车隔开。
样例输入:

样例输出:

来源:
2002年清华大学计算机研究生机试真题(第II套)

思路:

对称性质的判断:先转换成字符串,然后判断回文性质。


代码:

#include <stdio.h>
#include <string.h>
 
int check(int a)
{
    char s1[10], s2[10];
    int i = 0;
    while (a>0)
    {
        s1[i] = a%10;
        a /= 10;
        i++;
    }
    s1[i] = '\0';
 
    for (int j=0; j<i; j++)
    {
        s2[j] = s1[i-1-j];
    }
    s2[i] = '\0';
 
    if (strcmp(s1, s2) == 0)
        return 1;
    else
        return 0;
}
 
int main(void)
{
    int n, n2;
 
    for (n=0; n<256; n++)
    {
        n2 = n*n;
        if (check(n2) == 1)
            printf("%d\n", n);
    }
 
    return 0;
}
/**************************************************************
    Problem: 1074
    User: liangrx06
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:908 kb
****************************************************************/


posted on 2015-10-22 13:17  梁山伯  阅读(394)  评论(0编辑  收藏  举报

导航