当前位置:网站首页>PAT 1015 进制转换

PAT 1015 进制转换

2022-08-09 11:09:00 AlanLiu6

无语

#include<cstdio>
#include<cstring>

// 十进制转任意进制(暂时只转十进制以内
char str[15];

int prime(int n)
{
   if(n < 2) return 1;      // 坑点
    for(int i = 2; i <= n/2;i++)
    {
        if(n%i == 0)
        {
        //    printf("%d %d\n",i,n/i);
            return 1;
        }
    }
    return 0;
}
int transRadix(int n,int r)
{
    memset(str,'\0',sizeof(str));
    int w = 0;
    int t = n;
    while(t)
    {
        int b = t % r;
        char a = b + '0';
        t = t /r;
        str[w++] = a;
    }
    int sum = 0;
    for(int i = w-1,j = 1;i>=0;i--,j*=r)
    {
        sum += (str[i] - '0')*j;
    }
    return sum;
}

int main()
{
    int N,D;
    while(scanf("%d",&N)!= EOF && N > 0)
    {
        scanf("%d",&D);
        int sum = transRadix(N,D);
        int ans1 = prime(sum);
        int ans2 = prime(N);
        if(!ans1 && !ans2) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

原网站

版权声明
本文为[AlanLiu6]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Alen666/article/details/99366636