给定a,b和c,它们是等式x = b *(sumdigits(x)^ a)+ c的一部分。
其中sumdigits(x)确定数字x的所有数字的总和。任务是找出满足方程的x的所有整数解,并按升序打印。
鉴于此,1 <= x <= 10 9
例子:
Input: a = 3, b = 2, c = 8
Output: 10 2008 13726
Values of x are: 10 2008 13726. For 10, s(x) is 1; Putting value of s(x) in equation b*(s(x)^a)+c we get 10, and as 10 lies in range 0
Output: 1 31 337 967
Values of x that satisfy the equation are: 1 31 337 967
方法:对于给定的x范围, sumdigits(x)可以在1 <= s(X)<= 81的范围内,即0
下面是上述方法的实现:
C++
// C++ program to find the numbers of
// values that satisfy the equation
#include
using namespace std;
// This function returns the sum of
// the digits of a number
int getsum(int a)
{
int r = 0, sum = 0;
while (a > 0) {
r = a % 10;
sum = sum + r;
a = a / 10;
}
return sum;
}
// This function creates
// the array of valid numbers
void value(int a, int b, int c)
{
int co = 0, p = 0;
int no, r = 0, x = 0, q = 0, w = 0;
vector v;
for (int i = 1; i < 82; i++) {
// this computes s(x)^a
no = pow((double)i, double(a));
// this gives the result of equation
no = b * no + c;
if (no > 0 && no < 1000000000) {
x = getsum(no);
// checking if the sum same as i
if (x == i) {
// counter to keep track of numbers
q++;
// resultant array
v.push_back(no);
w++;
}
}
}
// prints the number
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
}
// Driver Code
int main()
{
int a = 2, b = 2, c = -1;
// calculate which value
// of x are possible
value(a, b, c);
return 0;
}
Java
// Java program to find the numbers of
// values that satisfy the equation
import java.util.Vector;
class GFG
{
// This function returns the sum of
// the digits of a number
static int getsum(int a)
{
int r = 0, sum = 0;
while (a > 0)
{
r = a % 10;
sum = sum + r;
a = a / 10;
}
return sum;
}
// This function creates
// the array of valid numbers
static void value(int a, int b, int c)
{
int co = 0, p = 0;
int no, r = 0, x = 0, q = 0, w = 0;
Vector v = new Vector();
for (int i = 1; i < 82; i++)
{
// this computes s(x)^a
no = (int) Math.pow(i, a);
// this gives the result of equation
no = b * no + c;
if (no > 0 && no < 1000000000)
{
x = getsum(no);
// checking if the sum same as i
if (x == i)
{
// counter to keep track of numbers
q++;
// resultant array
v.add(no);
w++;
}
}
}
// prints the number
for (int i = 0; i < v.size(); i++)
{
System.out.print(v.get(i)+" ");
}
}
// Driver Code
public static void main(String[] args)
{
int a = 2, b = 2, c = -1;
// calculate which value
// of x are possible
value(a, b, c);
}
}
// This code is contributed by
// PrinciRaj1992
Python 3
# Python 3 program to find the numbers
# of values that satisfy the equation
# This function returns the sum
# of the digits of a number
def getsum(a):
r = 0
sum = 0
while (a > 0) :
r = a % 10
sum = sum + r
a = a // 10
return sum
# This function creates
# the array of valid numbers
def value(a, b, c):
x = 0
q = 0
w = 0
v = []
for i in range(1, 82) :
# this computes s(x)^a
no = pow(i, a)
# this gives the result
# of equation
no = b * no + c
if (no > 0 and no < 1000000000) :
x = getsum(no)
# checking if the sum same as i
if (x == i) :
# counter to keep track
# of numbers
q += 1
# resultant array
v.append(no)
w += 1
# prints the number
for i in range(len(v)) :
print(v[i], end = " ")
# Driver Code
if __name__ == "__main__":
a = 2
b = 2
c = -1
# calculate which value
# of x are possible
value(a, b, c)
# This code is contributed
# by ChitraNayal
C#
// C# program to find the numbers of
// values that satisfy the equation
using System;
using System.Collections.Generic;
class GFG
{
// This function returns the sum of
// the digits of a number
static int getsum(int a)
{
int r = 0, sum = 0;
while (a > 0)
{
r = a % 10;
sum = sum + r;
a = a / 10;
}
return sum;
}
// This function creates
// the array of valid numbers
static void value(int a, int b, int c)
{
int no, x = 0, q = 0, w = 0;
List v = new List();
for (int i = 1; i < 82; i++)
{
// this computes s(x)^a
no = (int) Math.Pow(i, a);
// this gives the result of equation
no = b * no + c;
if (no > 0 && no < 1000000000)
{
x = getsum(no);
// checking if the sum same as i
if (x == i)
{
// counter to keep track of numbers
q++;
// resultant array
v.Add(no);
w++;
}
}
}
// prints the number
for (int i = 0; i < v.Count; i++)
{
Console.Write(v[i]+" ");
}
}
// Driver Code
public static void Main(String[] args)
{
int a = 2, b = 2, c = -1;
// calculate which value
// of x are possible
value(a, b, c);
}
}
// This code has been contributed by Rajput-Ji
PHP
0)
{
$r = $a % 10;
$sum = $sum + $r;
$a = (int)($a / 10);
}
return $sum;
}
// This function creates
// the array of valid numbers
function value($a, $b, $c)
{
$co = 0;
$p = 0;
$no;
$r = 0;
$x = 0;
$q = 0;
$w = 0;
$v = array();
$u = 0;
for ($i = 1; $i < 82; $i++)
{
// this computes s(x)^a
$no = pow($i, $a);
// this gives the result
// of equation
$no = $b * $no + $c;
if ($no > 0 && $no < 1000000000)
{
$x = getsum($no);
// checking if the
// sum same as i
if ($x == $i)
{
// counter to keep
// track of numbers
$q++;
// resultant array
$v[$u++] = $no;
$w++;
}
}
}
// prints the number
for ($i = 0; $i < $u; $i++)
{
echo $v[$i] . " ";
}
}
// Driver Code
$a = 2;
$b = 2;
$c = -1;
// calculate which value
// of x are possible
value($a, $b, $c);
// This code is contributed
// by mits
?>
Javascript
1 31 337 967
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。