📜  用幂的角数字形成一个数字

📅  最后修改于: 2021-05-08 15:58:30             🧑  作者: Mango

给定两个整数N和X。以这样的方式制作数字:该数字包含出现在第一个和最后一个数字N^1, N^2, N^3,  .... N^X

例子 :

Input : N = 10, X = 5 
Output : 1010101010
Explanation :
10^1 = 10
10^2 = 100
10^3 = 1000
10^4 = 10000
10^5 = 100000
Take First and Last Digit of each Power to
get required number.

Input : N = 19, X = 4 
Output : 19316911
Explanation :
19^1 = 19
19^2 = 361
19^3 = 6859
19^4 = 130321
Take First and Last Digit of each Power to get required number.

方法 :
1.逐一计算从1到X的N的所有幂。
2.将输出存储在power []数组中。
3.存储从power []数组到result []数组的power [0],即last_digit和power [power_size – 1],即unit_digit。
4.打印结果[]数组。

下面是上述方法的实现:

C++
// C++ program to find number formed by
// corner digits of powers.
#include 
using namespace std;
  
// Find next power by multiplying N with
// current power
void nextPower(int N, vector &power)
{
    int carry = 0;
  
    for (int i=0 ; i < power.size(); i++)
    {
        int prod = (power[i] * N) + carry ;
  
        // Store digits of Power one by one.
        power[i] = prod % 10 ;
  
        // Calculate carry.
        carry = prod / 10 ;
    }
  
    while (carry)
    {
        // Store carry in Power array.
        power.push_back(carry % 10);
        carry = carry / 10 ;
    }
}
  
// Prints number formed by corner digits of
// powers of N.
void printPowerNumber(int X, int N)
{
    // Storing N raised to power 0
    vector power;
    power.push_back(1);
  
    // Initializing empty result
    vector res;
  
    // One by one compute next powers and
    // add their corner digits.
    for (int i=1; i<=X; i++)
    {
        // Call Function that store power
        // in Power array.
        nextPower(N, power) ;
  
        // Store unit and last digits of
        // power in res.
        res.push_back(power.back());
        res.push_back(power.front());
    }
  
    for (int i=0 ; i < res.size(); i++)
        cout << res[i] ;
}
  
// Driver Code
int main()
{
    int N = 19 , X = 4;
    printPowerNumber(X, N);
    return 0 ;
}


Java
// Java program to find number formed by
// corner digits of powers.
import java.io.*;
import java.util.*;
   
public class GFG {
      
    static List power = new ArrayList();
    // Find next power by multiplying N 
    // with current power
    static void nextPower(Integer N)
    {
        Integer carry = 0;
       
        for (int i = 0; i < power.size(); i++)
        {
            Integer prod = (power.get(i) * N) + carry ;
       
            // Store digits of Power one by one.
            power.set(i,prod % 10);
       
            // Calculate carry.
            carry = prod / 10 ;
        }
       
        while (carry >= 1)
        {
            // Store carry in Power array.
            power.add(carry % 10);
            carry = carry / 10 ;
        }
    }
       
    // Prints number formed by corner digits of
    // powers of N.
    static void printPowerNumber(int X, int N)
    {
           
        // Storing N raised to power 0
        power.add(1);
       
        // Initializing empty result
        List res = new ArrayList();
       
        // One by one compute next powers and
        // add their corner digits.
        for (int i = 1; i <= X; i++)
        {
            // Call Function that store power
            // in Power array.
            nextPower(N) ;
       
            // Store unit and last digits of
            // power in res.
            res.add(power.get(power.size() - 1));
            res.add(power.get(0));
        }
       
        for (int i = 0 ; i < res.size(); i++)
            System.out.print(res.get(i)) ;
    }
       
    // Driver Code
    public static void main(String args[])
    {
        Integer N = 19 , X = 4;
        printPowerNumber(X, N);
    }
}
   
// This code is contributed by Manish Shaw
// (manishshaw1)


Python3
# Python3 program to find 
# number formed by 
# corner digits of powers.
   
# Storing N raised to power 0
power = []
  
# Find next power by multiplying 
# N with current power
def nextPower(N) :
    global power
    carry = 0
    
    for i in range(0, len(power)) :
        prod = (power[i] * N) + carry 
    
        # Store digits of 
        # Power one by one.
        power[i] = prod % 10 
    
        # Calculate carry.
        carry = (int)(prod / 10) 
    
    while (carry) :
  
        # Store carry in Power array.
        power.append(carry % 10)
        carry = (int)(carry / 10) 
    
# Prints number formed by corner 
# digits of powers of N.
def printPowerNumber(X, N) :
    global power
    power.append(1)
    
    # Initializing empty result
    res = []
    
    # One by one compute next powers 
    # and add their corner digits.
    for i in range(1, X+1) :
  
        # Call Function that store 
        # power in Power array.
        nextPower(N) 
    
        # Store unit and last 
        # digits of power in res.
        res.append(power[-1])
        res.append(power[0])
    
    for i in range(0, len(res)) :
        print (res[i], end="") 
    
# Driver Code
N = 19 
X = 4
printPowerNumber(X, N)
    
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# program to find number formed by
// corner digits of powers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
  
class GFG {
      
    // Find next power by multiplying N 
    // with current power
    static void nextPower(int N, ref List power)
    {
        int carry = 0;
      
        for (int i = 0; i < power.Count; i++)
        {
            int prod = (power[i] * N) + carry ;
      
            // Store digits of Power one by one.
            power[i] = prod % 10 ;
      
            // Calculate carry.
            carry = prod / 10 ;
        }
      
        while (carry >= 1)
        {
            // Store carry in Power array.
            power.Add(carry % 10);
            carry = carry / 10 ;
        }
    }
      
    // Prints number formed by corner digits of
    // powers of N.
    static void printPowerNumber(int X, int N)
    {
          
        // Storing N raised to power 0
        List power = new List();
        power.Add(1);
      
        // Initializing empty result
        List res = new List();
      
        // One by one compute next powers and
        // add their corner digits.
        for (int i = 1; i <= X; i++)
        {
            // Call Function that store power
            // in Power array.
            nextPower(N, ref power) ;
      
            // Store unit and last digits of
            // power in res.
            res.Add(power.Last());
            res.Add(power.First());
        }
      
        for (int i = 0 ; i < res.Count; i++)
            Console.Write(res[i]) ;
    }
      
    // Driver Code
    public static void Main()
    {
        int N = 19 , X = 4;
        printPowerNumber(X, N);
    }
}
  
// This code is contributed by Manish Shaw
// (manishshaw1)


PHP


输出 :
19316911