S 1.2 palsquare C程序

来源:互联网 发布:雷电法术升级数据2017 编辑:程序博客网 时间:2024/04/29 02:25

 

题目:

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 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 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 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 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;
}

 

 

原创粉丝点击