您好,欢迎访问一九零五行业门户网

回文自拍数

如果一个数字可以仅使用其自己的数字和某些数学运算来表示,则该数字被视为“自拍数字”。
例如,936是一个自拍号码。
$$\mathrm{936\:=\:(\sqrt{9})!^{3}\:+\:6!\:=\:216\:+\:720\:=\:第936章
这里可以看到,对原数的数字进行了一系列运算,结果与原数相等。
回文自拍号码是一种特殊的自拍号码。他们满足自拍乘法规则。
考虑一个数字 x。
设 x 的数字反转后的数为 $\mathrm{x^\prime}$。
令 y 为由 x 的数字以不同顺序组成的数字。
设 y 的数字反转后的数为 $\mathrm{y^\prime}$。
回文自拍数满足以下方程 -
$$\mathrm{x\:×\:x^\prime\:=\:y\:×\:y^\prime}$$
问题陈述对于给定的数字x,根据自拍乘法规则求其回文自拍数。
示例input: 1224output: 2142
说明 -
给定 x = 1224
所以 $\mathrm{x^\prime}$ = 4221 是将 x 的数字反转得到
令 y = 2142。y 是使用 x 的数字以不同顺序形成的
所以 $\mathrm{y^\prime}$ = 2412 是将 y 的数字反转得到的
$\mathrm{x\:×\:x^\prime}$ = 1224 × 4221 = 5166504 和 $\mathrm{y\:×\:y^\prime}$ = 2142 × 2412 = 5166504sincex× x' = y × y',y为x的回文自拍数。
input 4669:output: 6496
说明 -
给定 x = 4669
所以 $\mathrm{x^\prime}$ = 9664 是将 x 的数字反转得到
令 y = 6496。y 是使用 x 的数字以不同顺序形成的
所以 $\mathrm{y^\prime}$ = 6946 是将 y 的数字反转得到的
$\mathrm{x\:×\:x^\prime}$ = 4669 × 9664 = 45121216 和 $\mathrm{y\:×\:y^\prime}$ = 6496× 6946= 45121216由于 x× x' = y × y',y 是 x 的回文自拍数。
input: 456output: no palindromic selfie number exists
说明 -
给定 x = 456
所以 $\mathrm{x^\prime}$ = 654 是通过将 x 的数字反转得到的
令 y = 546。y 是使用 x 的数字以不同顺序形成的
所以 $\mathrm{y^\prime}$ = 645 是通过将 y 的数字反转得到的
$\mathrm{x\:×\:x^\prime}$ = 456 × 654 = 298224 和 $\mathrm{y\:×\:y^\prime}$ = 546× 645= 352170由于 $\mathrm{x\:×\:x^\prime}$ ≠ $\mathrm{y\:×\:y^\prime}$,因此 y 不是 x 的回文自拍照数。没有其他 456 的排列也满足自拍乘法规则。
解决方案查找给定数字的回文自拍照数字的解决方法相当直观且易于理解。
该方法包括以下步骤 -
定义一个“反向”函数
接受一个整数作为输入
将其转换为字符串
反转字符串
将其转换回整数。
定义一个函数“swap”
采用整数 i 和 j 作为输入
将整数转换为字符串
交换字符串中的第 i 个和第 j 个字符
将字符串转换回整数。
定义一个函数“置换”
采用整数、l、r 和一组“排列”作为输入。
它递归地生成整数数字的所有可能排列
它将它们存储在“排列”集中。
定义一个函数“palindromic_selfie”
采用整数“num”和一组“permutations”作为输入。
它使用“permute”函数生成整数“num”的所有可能的排列
然后,它通过将数字及其逆序的乘积与排列及其逆序的乘积进行比较,检查这些排列中的任何一个是否满足回文自拍属性。
如果找到这样的排列,则返回该数字。否则,返回-1。
在主函数中,设置一个数字“n”和一个用于存储排列的空集。
使用“n”和空集调用“palindromic_selfie”函数,并存储返回结果。
如果返回结果为-1,则打印“不存在回文自拍数”。否则,打印返回结果。
示例:c++ 程序以下 c++ 程序查找给定整数的回文自拍编号(如果存在)并返回它。它通过使用 permute() 函数找到给定数字的所有可能的排列,然后使用 reverse() 函数确定给定数字和该数字的任何排列是否满足 palindrome_selfie() 函数中的自拍乘法规则来实现此目的。如果不存在这样的数字,则会打印“no palindrome selfie number exists”。
#include <bits/stdc++.h>using namespace std;// function to reverse the digits of a numberint reverse(int num){ // converting number to string string str = to_string(num); reverse(str.begin(), str.end()); // converting string to integer num = stoi(str); return num;}// function that swaps the digits i and j in the numint swap(int num, int i, int j){ char temp; // converting number to string string s = to_string(num); // swap the ith and jth character temp = s[i]; s[i] = s[j]; s[j] = temp; // convert the string back to int and return return stoi(s);}// function to get all possible permutations of the digits in numvoid permute(int num, int l, int r, set<int> &permutations){ // adds the new permutation obtained in the set if (l == r) permutations.insert(num); else{ for (int i = l; i <= r; i++){ // swap digits to get a different ordering int num_copy = swap(num, l, i); // recurse to next pair of digits permute(num_copy, l + 1, r, permutations); } }}// function to check for palindrome selfie numberint palindromic_selfie(int num, set<int>& permutations) { // length of the number required for calculating all permutations of the digits int l = to_string(num).length() - 1; permute(num, 0, l, permutations); // calculate all permutations //remove the number and its reverse from the obtained set as this is the lhs of multiplicative equation auto n1 = permutations.find(reverse(num)); auto n2 = permutations.find(num); if (n1 != permutations.end()) permutations.erase(n1); if (n2 != permutations.end()) permutations.erase(n2); // go through all other permutations of the number for (set<int>::iterator it = permutations.begin(); it != permutations.end(); it++) { int num2 = *it; // check if selfie multiplicative rule holds i.e. x * reverse(x) = y * reverse(y) if (num * reverse(num) == num2 * reverse(num2)) { return num2; } } // if no such number found return -1;}int main(){ int n = 1234; cout << n: << n << endl; set<int> permutations; int ans = palindromic_selfie(n, permutations); if (ans == -1) { cout << no palindromic selfie number exists << endl; } else{ cout << ans << endl; } return 0;}
输出n: 1234no palindromic selfie number exists
时间和空间复杂度分析时间复杂度:o(n!)此代码的时间复杂度为 o(n!),其中 n 是输入数字的位数。这是因为有 n! n 位数字的排列,并且 permute() 方法生成数字的所有潜在排列。
空间复杂度:o(n!)由于集合“排列”包含所有可能的数字组合,等于 n!,因此该代码的空间复杂度为 o(n!)。 verse() 和 swap() 函数的空间复杂度为 o(n),因为它们还生成长度为 n 的临时字符串。空间复杂度为 o(n!) 的排列集合主导了整个代码的空间复杂度。
结论回文自拍数是数学中一个有趣的概念。它们满足自拍乘法方程。本文讨论了一种方法来查找一个数字是否具有回文自拍号码,如果是,则返回它。对问题的概念、解决方法、c++程序以及程序的时间和空间复杂度进行了深入分析。
以上就是回文自拍数的详细内容。
其它类似信息

推荐信息