使用幂的角数字形成数字
什么是角数字?
数字的角数字指的是最右边的数字和最左边的数字。
例如,1234 的角数字是 1 和 4。
一位数的角数字将是该数字本身的两倍。
例如,2 的角数字是 2 和 2。
问题陈述
对于给定的两个数字 n 和 x,使用从 1 到 x 的所有 n 的幂的角数字形成一个数字,即 n1、n2....nx。
示例
Input: n = 2, x = 4 Output: 22448816
解释
21 = 2. Corner digits = 2, 2. 22 = 4. Corner digits = 4, 4. 23 = 8. Corner digits = 8, 8. 24 = 16. Corner digits = 1, 6.
因此,使用上面所有数字的角数字形成的数字是 22448816。
Input: n = 16, x = 5 Output: 1626466616
解释
161 = 16. Corner digits = 1, 6. 162 = 256. Corner digits = 2, 6. 163 = 4096. Corner digits = 4, 6. 164 = 65536. Corner digits = 6, 6. 165 = 1048576. Corner digits = 1, 6.
因此,使用上面所有数字的角数字形成的数字是 1626466616。
算法
依次找出从 1 到 X 的所有 N 的幂。
将幂存储在 power 数组中。
创建一个 answer 数组来存储最终输出。
将 power 数组的第一个和最后一个数字存储在 answer 数组中。
打印 answer 数组。
伪代码
函数 main() −
从用户处获取 n 和 x 的输入。
函数调用 corner_digits_number()。
函数 corner_digits_number(int n, int x) −
创建向量 power 和 answer,分别用于存储幂和最终答案。
在 power 中存储 1。(n 的 0 次幂)。
对于 i=1 到 i=x −
函数调用 store_next_power()
将 power 的最后一位数字存储在 answer 中。
将 power 的第一位数字存储在 answer 中。
函数调用 print_output()。
函数 store_next_power(vector<int>&power, int n) −
初始化 carry = 0,product = 1。
对于 i=0 到 i=power.size()-1 −
Product -> power[i] * n + carry。
power[i] -> product %10。
carry -> product/10。
当 carry 不等于零时 −
将 carry % 10 存储在 power 中。
carry -> carry /10。
函数 print_output(vector<int>answer) −
对于 i=0 到 i=answer.size()-1 −
打印 answer[i]。
示例
下面是一个 C++ 程序,用于从 1 到 x 的所有 n 的幂的角数字中形成一个数字。
#include<iostream>
#include<vector>
using namespace std;
//This function calculates the next power value and stores
// it in the power vector
void store_next_power(vector<int>&power, int n){
int carry = 0, product;
for(int i=0 ; i < power.size() ; i++){
//Calculating the power of n
product = ( power[i] * n ) + carry;
//store the digit of power for the particular index
power[i] = product % 10 ;
//the carry will be added to the next index's value.
carry = product / 10 ;
}
//The carry will also be added to the power vector.
while(carry){
power.push_back(carry % 10);
carry = carry / 10 ;
}
}
//Print the final output
void print_output(vector<int>v){
for(int i=0 ; i < v.size() ; i++){
cout<<v[i]<<", ";
}
}
//This function prints the number formed by the corner digits
// of all the powers of n from 1 to x.
void corner_digits_number(int n, int x){
//vector to store the powers
vector<int>power;
//Store n raised to the power 0.
//This will be useful in finding the next power of n.
power.push_back(1);
//vector to store the final output
vector<int>answer;
//Calculate all the powers of n from 1 and x
for(int i=0 ; i < x ; i++){
//Function call to store the next power value
//in the vector
store_next_power(power,n);
//add the first and last digits in the power vector
//to the answer vector
// The last digit in the power is first pushed to the answer vector
// because it contains the unit place value.
answer.push_back(power.back());
answer.push_back(power.front());
}
//Function call to print the final number.
print_output(answer);
}
int main(){
int n = 6, x = 4;
//Function call to print the required number
cout<< "Corner digits for n=6 and x =4 are: "<<endl;
corner_digits_number(n,x);
return 0;
}
输出
Corner digits for n=6 and x =4 are: 6, 6, 3, 6, 2, 6, 1, 6,
本文讨论了使用从 1 到 x 的所有 n 的幂的角数字形成数字的问题。n 和 x 是问题中给定的两个数字。
首先通过几个示例解释了该问题,然后讨论了该方法。
本文给出了算法、伪代码和 C++ 程序。还提到了时间和空间复杂度。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP