Palindromic Squares

本文介绍了一个程序,该程序接收一个2到20之间的基数,并找出所有1到300之间的整数N,使得N的平方在给定的基数下是回文数。程序实现了进制转换和判断是否为回文数的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

View Code
 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 
 8 using namespace std;
 9 
10 int n;
11 char b[101];
12 char bf[101];
13 
14 
15 void change(int i,int k)
16 {
17     int q=i,e=0,y;
18     if(k<=10)
19     {  
20        while(q!=0)
21        {
22           y=q%k;
23           q=q/k;
24           b[e]='0'+y;
25           e++;
26        }
27     }
28     else if(k>=11)
29     {
30        while(q!=0)
31        {
32           y=q%k;
33           q=q/k;
34           if(y<10) b[e]=y+'0';
35           else
36           b[e]=y-10+'A';
37           e++;
38        }
39     }
40     for(int i=0;i<e;i++)
41     {
42        bf[i]=b[e-i-1];
43     }
44 }
45 
46 
47 
48 bool check(char bf[])
49 {
50     char str[101];;
51     strcpy(str,bf);
52     int flag=true;
53     int i,j;
54     int long1=strlen(str);
55     for(i=0,j=long1-1;i<=j;i++,j--)
56     {
57         if(str[i]!=str[j])
58         {
59            return false;
60            break;
61         }
62     }
63     return true;
64 }
65 
66 
67 int main( )
68 {
69     freopen("palsquare.in","r",stdin);
70     freopen("palsquare.out","w",stdout);
71     cin>>n;
72     char r1[101],r2[101];
73     for(int i=1;i<=300;i++)
74     {
75         for(int j=0;j<=100;j++)
76         {
77            b[j]='\0';
78            bf[j]='\0';
79         }
80         change(i,n);
81         strcpy(r1,bf);
82         change(i*i,n);
83         strcpy(r2,bf);
84         bool ok2=check(r2);
85         if(ok2==true)
86         cout<<r1<<" "<<r2<<endl;
87     }  
88     return 0;
89 }

 

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
这道题主要是考察进制的转换,还有就是超过10的进制的转换是y-10+'A',就这样了,哦,不能给string 赋值char*

转载于:https://www.cnblogs.com/spwkx/archive/2012/07/14/2591813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值