【FOJ】Problem 1548 猪数

Problem 1548 猪数.
样例的15进制1 1怎么没有了我挺秃然的
VC++AC|GUN++WA

思路

  • 遍历1-300的每个数字:
    ① 将该数字的平方转换成B进制
    ② 将B进制的该数字的平方转换成字符串
    ③ 判断该字符串是否回文–是则输出(两个B)

代码

#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;

int change(int B, int n, char a[]){
    int x, len=0;
    int i = (int)(log((double)(n))/log((double)B));   //找到最高位
    while(i>=0){     //转成字符串
        x = n / pow((float)B, (float)i);
        n -= x*pow((float)B, (float)i);
        i--;
        if(x<10)
            a[len++] = x+'0';
        else
            a[len++] = x-10+'A';
    }
    return len;
}

int main(){
    int B;
    char two[15], self[15];
    int len1, len2, temp;
    while(scanf("%d", &B)!=EOF){
        for(int n=1; n<=300; n++){
            memset(two, '\0', sizeof(two));
            memset(self, '\0', sizeof(self));
            len1 = change(B, n*n, two);
            temp = 0;      //判断是否回文
            for(int i=0; i<=len1/2; i++)
                if(two[i]!=two[len1-1-i]){
                    temp = 1;
                    break;
                }
            if(temp==0){
                len2 = change(B, n, self);
                printf("%s %s\n", self, two);
            }
        }
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值