原文:
给定arr[]
,其中包含n
个不同的正整数,任务是打印给定数组中的前k
个不同的。
数字
n
是莫兰数,如果n
除以其是。示例:18、21、27、42、45
示例:
输入:
arr[] = {192, 21, 18, 138, 27, 42, 45}, k = 4
输出:
21, 27, 42, 45
说明:
- 整数 21 的位数之和为
2 1 = 3
。因此,将 21 除以其位数之和得出21/3 = 7
,这是质数。- 整数 27 的位数之和为
2 7 = 9
。因此,将 27 除以其位数之和得出27/9 = 3
,这是质数。- 整数 42 的位数之和为
4 2 = 6
。因此,将 42 除以其位数之和得出42/6 = 7
,这是质数。- 整数 45 的位数之和为
4 5 = 9
。因此,将 45 除以其位数之和得出45/9 = 5
,这是质数。输入:
arr[] = {127, 186, 198, 63, 27, 91}, k = 3
输出:
27, 63, 198
方法:请按照以下步骤解决问题:
-
遍历排序后的数组,对于每个元素,
-
如果确定为真,则将元素插入中,并递增计数器直到达到
k
。 -
当集合的时,打印集合中的元素。
下面是上述方法的实现:
c
#include
#include
#include
using namespace std;
// function to calculate the
// sum of digits of a number
int digisum(int a)
{
// stores the sum of digits
int sum = 0;
while (a) {
// add the digit to sum
sum = a % 10;
// remove digit
a = a / 10;
}
// returns the sum
// of digits
return sum;
}
// function to check if a number
// is prime or not
bool isprime(int r)
{
bool s = true;
for (int i = 2; i * i <= r; i ) {
// if r has any divisor
if (r % i == 0) {
// set r as non-prime
s = false;
break;
}
}
return s;
}
// function to check if a
// number is moran number
bool ismorannumber(int n)
{
int dup = n;
// calculate sum of digits
int sum = digisum(dup);
// check if n is divisible
// by the sum of digits
if (n % sum == 0) {
// calculate the quotient
int c = n / sum;
// if the quotient is prime
if (isprime(c)) {
return true;
}
}
return false;
}
// function to print the first k
// moran numbers from the array
void firstkmorannumber(int a[],
int n, int k)
{
int x = k;
// sort the given array
sort(a, a n);
// initialise a set
set s;
// traverse the array from the end
for (int i = n - 1; i >= 0
&& k > 0;
i--) {
// if the current array element
// is a moran number
if (ismorannumber(a[i])) {
// insert into the set
s.insert(a[i]);
k--;
}
}
if (k > 0) {
cout << x << " moran numbers are"
<< " not present in the array" << endl;
return;
}
set::iterator it;
for (it = s.begin(); it != s.end(); it) {
cout << *it << ", ";
}
cout << endl;
}
// driver code
int main()
{
int a[] = { 34, 198, 21, 42,
63, 45, 22, 44, 43 };
int k = 4;
int n = sizeof(a) / sizeof(a[0]);
firstkmorannumber(a, n, k);
return 0;
}
java
import java.io.*;
import java.util.*;
class gfg{
// function to calculate the
// sum of digits of a number
static int digisum(int a)
{
// stores the sum of digits
int sum = 0;
while (a != 0)
{
// add the digit to sum
sum = a % 10;
// remove digit
a = a / 10;
}
// returns the sum
// of digits
return sum;
}
// function to check if a number
// is prime or not
static boolean isprime(int r)
{
boolean s = true;
for(int i = 2; i * i <= r; i )
{
// if r has any divisor
if (r % i == 0)
{
// set r as non-prime
s = false;
break;
}
}
return s;
}
// function to check if a
// number is moran number
static boolean ismorannumber(int n)
{
int dup = n;
// calculate sum of digits
int sum = digisum(dup);
// check if n is divisible
// by the sum of digits
if (n % sum == 0)
{
// calculate the quotient
int c = n / sum;
// if the quotient is prime
if (isprime(c))
{
return true;
}
}
return false;
}
// function to print the first k
// moran numbers from the array
static void firstkmorannumber(int[] a,
int n, int k)
{
int x = k;
// sort the given array
arrays.sort(a);
// initialise a set
treeset s = new treeset();
// traverse the array from the end
for(int i = n - 1; i >= 0 && k > 0; i--)
{
// if the current array element
// is a moran number
if (ismorannumber(a[i]))
{
// insert into the set
s.add(a[i]);
k--;
}
}
if (k > 0)
{
system.out.println(x " moran numbers are"
" not present in the array");
return;
}
for(int value : s)
system.out.print(value ", ");
system.out.print("\n");
}
// driver code
public static void main(string[] args)
{
int[] a = { 34, 198, 21, 42,
63, 45, 22, 44, 43 };
int k = 4;
int n = a.length;
firstkmorannumber(a, n, k);
}
}
// this code is contributed by akhilsaini
python3
import math
# function to calculate the
# sum of digits of a number
def digisum(a):
# stores the sum of digits
sums = 0
while (a != 0):
# add the digit to sum
sums = a % 10
# remove digit
a = a // 10
# returns the sum
# of digits
return sums
# function to check if a number
# is prime or not
def isprime(r):
s = true
for i in range(2, int(math.sqrt(r)) 1):
# if r has any divisor
if (r % i == 0):
# set r as non-prime
s = false
break
return s
# function to check if a
# number is moran number
def ismorannumber(n):
dup = n
# calculate sum of digits
sums = digisum(dup)
# check if n is divisible
# by the sum of digits
if (n % sums == 0):
# calculate the quotient
c = n // sums
# if the quotient is prime
if isprime(c):
return true
return false
# function to print the first k
# moran numbers from the array
def firstkmorannumber(a, n, k):
x = k
# sort the given array
a.sort()
# initialise a set
s = set()
# traverse the array from the end
for i in range(n - 1, -1, -1):
if (k <= 0):
break
# if the current array element
# is a moran number
if (ismorannumber(a[i])):
# insert into the set
s.add(a[i])
k -= 1
if (k > 0):
print(x, end =' moran numbers are not '
'present in the array')
return
lists = sorted(s)
for i in lists:
print(i, end = ', ')
# driver code
if __name__ == '__main__':
a = [ 34, 198, 21, 42,
63, 45, 22, 44, 43 ]
k = 4
n = len(a)
firstkmorannumber(a, n, k)
# this code is contributed by akhilsaini
c
using system;
using system.collections;
using system.collections.generic;
class gfg{
// function to calculate the
// sum of digits of a number
static int digisum(int a)
{
// stores the sum of digits
int sum = 0;
while (a != 0)
{
// add the digit to sum
sum = a % 10;
// remove digit
a = a / 10;
}
// returns the sum
// of digits
return sum;
}
// function to check if a number
// is prime or not
static bool isprime(int r)
{
bool s = true;
for(int i = 2; i * i <= r; i )
{
// if r has any divisor
if (r % i == 0)
{
// set r as non-prime
s = false;
break;
}
}
return s;
}
// function to check if a
// number is moran number
static bool ismorannumber(int n)
{
int dup = n;
// calculate sum of digits
int sum = digisum(dup);
// check if n is divisible
// by the sum of digits
if (n % sum == 0)
{
// calculate the quotient
int c = n / sum;
// if the quotient is prime
if (isprime(c))
{
return true;
}
}
return false;
}
// function to print the first k
// moran numbers from the array
static void firstkmorannumber(int[] a,
int n, int k)
{
int x = k;
// sort the given array
array.sort(a);
// initialise a set
sortedset s = new sortedset();
// traverse the array from the end
for(int i = n - 1; i >= 0 && k > 0; i--)
{
// if the current array element
// is a moran number
if (ismorannumber(a[i]))
{
// insert into the set
s.add(a[i]);
k--;
}
}
if (k > 0)
{
console.writeline(x " moran numbers are"
" not present in the array");
return;
}
foreach(var val in s)
{
console.write(val ", ");
}
console.write("\n");
}
// driver code
public static void main()
{
int[] a = { 34, 198, 21, 42,
63, 45, 22, 44, 43 };
int k = 4;
int n = a.length;
firstkmorannumber(a, n, k);
}
}
// this code is contributed by akhilsaini
输出:
42, 45, 63, 198,
时间复杂度:o(n ^(3/2))
。
辅助空间:o(n)
。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处