给定一个数字,任务是使用命令行参数检查该数字是否为 Armstrong。 n 位数的正整数称为n阶阿姆斯特朗数(阶数是位数)如果。
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
方法:
- 由于数字是作为命令行参数输入的,因此不需要专用的输入行
- 从命令行参数中提取输入数字
- 这个提取的数字将是字符串类型。
- 将此数字转换为整数类型并将其存储在变量中,例如 num
- 计算数字 num 的数字位数(或查找顺序)并将其存储在一个变量中,例如 n。
- 对于输入数NUM,计算R N每个数字河
- 如果所有这些值的总和等于 num
- 如果它们不相同,则数字不是阿姆斯特朗
- 如果它们相同,则数字是阿姆斯壮
程序:
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++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。