1024 Palindromic Number

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤10​10​​) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <map>
#include <sstream>
#include <string.h>

using namespace std;

typedef long long ll;

struct bign
{
    int d[1000];
    int len;
    bign()
    {
        memset(d,0,sizeof(d));
        len=0;
    }
};

bign change(string s)
{
    bign c;
    c.len=s.length();
    for(int i=0; i<c.len; i++)
    {
        c.d[i]=s[c.len-1-i]-'0';
    }
    return c;
}

bign revise(bign d)
{
    bign c;
    c.len=d.len;
    for(int i=0; i<c.len; i++)
    {
        c.d[i]=d.d[c.len-1-i];
    }
    return c;
}

bool check(bign a)
{
    for(int i=0; i<=a.len/2; i++)
    {
        if(a.d[i]!=a.d[a.len-i-1])
            return false;
    }
    return true;
}

bign add(bign a,bign b)
{
    bign c;
    int carry=0;
    for(int i=0; i<a.len||i<b.len; i++)
    {
        int temp=a.d[i]+b.d[i]+carry;
        c.d[c.len++]=temp%10;
        carry=temp/10;
    }
    if(carry>0)
    {
        c.d[c.len++]=carry;
    }
    return c;
}

int num[11];
int main()
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    int step=0;
    bign c,d;
    c=change(s);
    d=revise(c);
//    for(int i=c.len-1; i>=0; i--)
//        cout<<c.d[i];
//    cout<<endl;
    for(int i=0; i<k; i++)
    {
        if(check(c))
        {
            break;
        }
        c=add(c,d);
        d=revise(c);
        step++;
//        for(int i=c.len-1; i>=0; i--)
//            cout<<c.d[i];
//        cout<<endl;
    }
    for(int i=c.len-1; i>=0; i--)
        cout<<c.d[i];
    cout<<endl<<step;
    return 0;
}

 

寻找回文素数 如果一个整数是素数,同时其对应的字符串是回文字符串时,便称其为回文素数。例如,131 既是素数,其对应的字符串131又是回文字符串,所以 131 是回文素数。 输入一个正整数 n , 请你在一行内输出从小到大排列的小于这个数的所有回文素数,每个数字后面一个空格。 编程要求 根据提示,在右侧编辑器补充代码,完善寻找回文素数的小程序。 测试说明 平台会对你编写的代码进行测试: 输入格式 输入一个正整数 输出格式 一行内输出从小到大排列的小于这个数的所有回文素数,每个数字后面一个空格。 测试输入: 191 预期输出: 2 3 5 7 11 101 131 151 181 def is_prime(n): """判断素数的函数,接收一个正整数为参数,参数是素数时返回True,否则返回False。减小判定区间,减少循环次数,提升效率""" #======================Begin================================= # 补充你的代码 #=========================End============================== def plalindrome_prime(number): """接收一个正整数参数number,遍历从0到number之间的所有整数, 若某个数是素数,且转为字符串后是回文字符串,则称其中回文素数 找出并在同一行中输出小于number的所有回文素数,每个数字后一个空格,函数无返回值。""" #======================Begin================================= # 补充你的代码 #=========================End============================== positive_int = int(input()) plalindrome_prime(positive_int)
最新发布
05-17
以下是 Python 实现寻找小于给定数的所有回文素数的完整代码示例: ### 寻找小于给定数的所有回文素数 为了实现这一功能,可以定义两个辅助函数:`is_prime` 用于判断一个数是否为素数,`is_palindrome` 用于判断一个数是否为回文数。接着通过组合这两个函数来筛选出所有满足条件的小于指定数值的回文素数。 ```python def is_prime(num): """判断一个数是否为素数""" if num < 2: return False for i in range(2, int(num ** 0.5) + 1): if num % i == 0: return False return True def is_palindrome(num): """判断一个数是否为回文数""" num_str = str(num) return num_str == num_str[::-1] def find_palindromic_primes(limit): """找出小于给定数的所有回文素数""" palindromic_primes = [] for number in range(2, limit): if is_prime(number) and is_palindrome(number): palindromic_primes.append(number) return palindromic_primes # 主程序部分 try: user_input = int(input("请输入一个正整数作为上限:")) result = find_palindromic_primes(user_input) print(f"小于 {user_input} 的所有回文素数为:{' '.join(map(str, result))}") except ValueError: print("输入错误,请输入有效的正整数") ``` 在此代码中,`find_palindromic_primes` 函数接收一个参数 `limit` 表示查找范围的最大值,并返回一个小于此最大值的所有回文素数组成的列表[^1]。主程序部分负责获取用户输入并调用该函数打印结果。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值