S 1.2 palsquare C程序

 

题目:

http://ace.delos.com/usacoprob2?a=vaOx5hM2Vn9&S=palsquare

 

Palindromic Squares
Rob Kolstad

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

PROGRAM NAME: palsquare

INPUT FORMAT

A single line with B, the base (specified in base 10).

SAMPLE INPUT (file palsquare.in)

10

OUTPUT FORMAT

Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself.

SAMPLE OUTPUT (file palsquare.out)

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696


 

题目翻译:

描述

回文数是指从左向右念和从右向左念都一样的数。如12321就是一个典型的回文数。

给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数。用’A’,’B’……表示10,11等等。

[编辑] 格式

PROGRAM NAME: palsquare

INPUT FORMAT:

file (palsquare.in)

共一行,一个单独的整数B(B用十进制表示)。

OUTPUT FORMAT:

file (palsquare.out)

每行两个B进制的符合要求的数字,第二个数是第一个数的平方,且第二个数是回文数。

[编辑] SAMPLE INPUT

10

[编辑] SAMPLE OUTPUT

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696

 

/*
ID:smilecl1
LANG:C
TASK:palsquare
*/

/*
 * TIME: 2012/11/7
 * MODIFY: 2012/11/9
 * EMAIL:
SmileCloud201@gmail.com
 * NAME: LiYun
 */

/*
 * 思路:
 * 直接for一遍,判断square(base进制)是否是回文数。
 */

 

#include <stdio.h>
#include <string.h>
/* 位数最多的是300*300=900 的二进制,一共10位 ,这里是字符串输出,所以定义11位*/
#define N 12

 


/* change()函数用来转化成n进制的数,这里得到的result是数的倒序,即最低位在第一位,最高位在最后一位。这里没有必要
 * 将顺序换过来,刚好利用回文数的特性,倒序和正序一样。对于num直接利用字符串的倒序输出就可以了。
 */
void change(const int num, const int base, char p[N])
{
    int i = 0;
    char tab[21] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
    int number = num;
    while( number )
    {
       /*最先得到的是最低位 */
       p[i++] = tab[number % base];
       number = number / base;
    }
}

 

int is_plalindrome(char num[N])
{
    int i = 0;
    int j = strlen(num) - 1;
    for (i = 0; i < j; ++i, --j)
    {
        if ( num[i] != num[j] )
        return 0;
    }
    return 1;
}

 

int main()
{
    int base;
    int i = 0;
    int j = 0;
    char number[N] = {0};
    char square[N] = {0};
    FILE *fin;
    FILE *fout;

    fin = fopen("palsquare.in", "r");
    fout = fopen("palsquare.out","w");
    fscanf(fin, "%d", &base);
    for (i = 1; i <= 300; ++i)
    {
        change(i, base, number);
        change( i * i, base,square );
        if ( is_plalindrome(square) )
        {
            for (j = strlen(number) - 1; j >= 0 ; --j)
            fprintf(fout, "%c", number[j]);
            fprintf(fout, " %s\n", square);
        }
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值