zoukankan      html  css  js  c++  java
  • Palindromic Squares

    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*
  • 相关阅读:
    实验三-并发程序 20175201张驰
    20175201 20175215 20175229 实验二 固件程序设计
    20175201 20175215 20175229 实验一 开发环境的熟悉
    #20175201 实验五 网络编程与安全
    云班课选做
    2019-2020-12 20175313 20175328 20175329 实验五 通讯协议设计
    2019-2020-1 20175313 20175328 20175329 实验四 外设驱动程序设计
    2019-2020-1 20175313 20175328 20175329 实验三 并发程序
    20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验三
    20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验二
  • 原文地址:https://www.cnblogs.com/spwkx/p/2591813.html
Copyright © 2011-2022 走看看