使用冪的角數字形成數字


什麼是角數字?

數字的角數字指的是最右邊的數字和最左邊的數字。

例如,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) −

  • 建立向量 poweranswer,分別用於儲存冪和最終答案。

  • 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 的冪的角數字形成數字的問題。nx 是問題中給定的兩個數字。

首先通過幾個示例解釋了該問題,然後討論了該方法。

本文給出了演算法、虛擬碼和 C++ 程式。還提到了時間和空間複雜度。

更新於: 2023 年 3 月 10 日

98 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.