给定一个数字,任务是使用命令行参数检查该数字是否为Armstrong。 n位数字的正整数称为if的n阶阿姆斯特朗数(order是位数)。
abcd... = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + ....
例子:
Input: 153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153
Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input: 1253
Output: No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723
Input: 1634
Output: Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
方法:
- 由于该数字是作为命令行参数输入的,因此不需要专用的输入行
- 从命令行参数中提取输入数字
- 提取的数字将为String类型。
- 将此数字转换为整数类型并将其存储在变量中,例如num
- 计算数字num的数字位数(或查找顺序)并将其存储在变量中,例如n。
- 对于输入数字num中的每个数字r,计算r n 。
- 如果所有这些值的总和等于num
- 如果它们不同,则编号不是Armstrong
- 如果相同,则数字为Armstrong
程序:
C
// C program to check if a number is Armstrong
// using command line arguments
#include
#include /* atoi */
// Function to calculate x raised to the power y
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to calculate order of the number
int order(int x)
{
int n = 0;
while (x) {
n++;
x = x / 10;
}
return n;
}
// Function to check whether the given number is
// Armstrong number or not
int isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
if (sum == x)
return 1;
else
return 0;
}
// Driver code
int main(int argc, char* argv[])
{
int num, res = 0;
// Check if the length of args array is 1
if (argc == 1)
printf("No command line arguments found.\n");
else {
// Get the command line argument and
// Convert it from string type to integer type
// using function "atoi( argument)"
num = atoi(argv[1]);
// Check if it is Armstrong
res = isArmstrong(num);
// Check if res is 0 or 1
if (res == 0)
// Print No
printf("No\n");
else
// Print Yes
printf("Yes\n");
}
return 0;
}
Java
// Java program to check if a number is Armstrong
// using command line arguments
class GFG {
// Function to calculate x
// raised to the power y
public static int power(int x, long y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
// Function to calculate order of the number
public static int order(int x)
{
int n = 0;
while (x != 0) {
n++;
x = x / 10;
}
return n;
}
// Function to check whether the given number is
// Armstrong number or not
public static int isArmstrong(int x)
{
// Calling order function
int n = order(x);
int temp = x, sum = 0;
while (temp != 0) {
int r = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
if (sum == x)
return 1;
else
return 0;
}
// Driver code
public static void main(String[] args)
{
// Check if length of args array is
// greater than 0
if (args.length > 0) {
// Get the command line argument and
// Convert it from string type to integer type
int num = Integer.parseInt(args[0]);
// Get the command line argument
// and check if it is Armstrong
int res = isArmstrong(num);
// Check if res is 0 or 1
if (res == 0)
// Print No
System.out.println("No\n");
else
// Print Yes
System.out.println("Yes\n");
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
输出:
- 在C中:
- 在Java:
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。