给定数字n,找出可以形成多少个不包含9的n位数字。
例子:
Input : 1
Output : 8
Explanation :
Except 9, all numbers are possible
Input : 2
Output : 72
Explanation :
Except numbers from 90 - 99 and all
two digit numbers that does not end
with 9 are possible.
可以形成的n位数字的总数将为9 * 10 ^(n-1),除了第一个位置以外,所有数字都可以用10个数字(0-9)填充。如果从列表中删除了数字9,则n位数字的总数将为8 * 9 ^(n-1)。
下面是上述想法的实现:
C++
// CPP program to find number of n
// digit numbers that do not
// contain 9 as it's digit
#include
using namespace std;
// function to find number of
// n digit numbers possible
int totalNumber(int n)
{
return 8*pow(9, n - 1);
}
// driver function
int main()
{
int n = 3;
cout << totalNumber(n);
return 0;
}
Java
// Java program to find number of
// n digit numbers that do not
// contain 9 as it's digit
import java.io.*;
public class GFG
{
// function to find number of
// n digit numbers possible
static int totalNumber(int n)
{
return 8 * (int)Math.pow(9, n - 1);
}
// Driver Code
static public void main (String[] args)
{
int n = 3;
System.out.println(totalNumber(n));
}
}
// This code is contributed by vt_m.
Python3
# python program to find number of n
# digit numbers that do not
# contain 9 as it's digit
# function to find number of
# n digit numbers possible
def totalNumber(n):
return 8 * pow(9, n - 1);
# driver function
n = 3
print(totalNumber(n))
# This code is contributed by Sam007
C#
// C# program to find number of
// n digit numbers that do not
// contain 9 as it's digit
using System;
public class GFG
{
// function to find number of
// n digit numbers possible
static int totalNumber(int n)
{
return 8 * (int)Math.Pow(9, n - 1);
}
// Driver Code
static public void Main ()
{
int n = 3;
Console.WriteLine(totalNumber(n));
}
}
// This code is contributed by vt_m.
php
输出:
648