如何获取用户在'./a.out'之后以任意顺序输入的命令行参数整数的GCD?
How to get the GCD of command line argument integers entered by the user after './a.out' in any order?
这个程序return是命令行的GCD
args 仅由用户输入,从最小到最大。
例如:
用户输入:'./a.out 5 10 15 20 25 '
这个程序 returns: "The GCD of the command line args is 5"
但是,我 运行 遇到的问题是,如果用户输入例如:
用户输入:'./a.out 15 10 5 25 20'
本节目return秒:15
谁能告诉我如何解决这个问题?
这就是我的目标:
如果用户输入:'./a.out 15 10 5 25 20'
这个程序应该return: 5
//Header Files
#include<stdio.h>
#include<string.h>
//Main Method
int main(int argc, char *argv[]){
//Declared variables here and print statements
int i,x,y,min;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
//This is the main while loop
while( x !=0 && y !=0 && y != x){
if(x<y){
y=y-x;
}//End first IF statement
if(y<x){
x-x-y;
}//End second IF statement
}//End while loop
//This function returns the converted integral number as an int value
x=atoi(argv[i]);
for(i=2;i<argc;i++){
y=atoi(argv[i]);
}
//The following code gets the GCD and prints from the command line
min = (x>y)?x:y;
for(i=min;i>=1;--i){
if(x%i==0 && y%i==0){
for(i=1;i<argc;i++){
printf("%s\n", argv[i]);
break;}//End for loop
}//End IF statement
}//End For loop
}//End of MAIN
我已经清理过了,这应该对你有用:
在考虑在代码中计算之前初始化每个变量。
#include<stdio.h>
#include<string.h>
//Main Method
int main(int argc, char *argv[]){
//Declared variables here and print statements
int i,x,y,gcd;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
x=atoi(argv[1]);
for(i=2;i<argc;i++){
y=atoi(argv[i]);
while( x !=0 && y !=0 && y != x){
if(x<y){
y=y-x;
}else if(y<x){
x=x-y;
}
}
gcd=x;
}
printf("%d",gcd);
}//End of MAIN
如果您想使用递归。
#include<stdio.h>
#include<string.h>
int GCD(int numbers[], int count)
{
if (count==1)
return numbers[0];
int i, j, min, x, y;
x = numbers[0];
y = numbers[1];
min = (x>y)?x:y;
for(i=min;i>=1;--i){
if(x%i==0 && y%i==0){
int newNumbers[count-1];
newNumbers[0]=i;
for (j=1; j<count-1; j++) {
newNumbers[j] = numbers[j+1];
}
return GCD(newNumbers, count-1);
}
}
return 0;
}
int main(int argc, const char * argv[]) {
//Declared variables here and print statements
int i;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
int numbers[argc];
for(i=1;i<argc;i++){
numbers[i]=atoi(argv[i]);
}
int result = GCD(numbers, argc);
printf("%d\n", result);
}
这个程序return是命令行的GCD args 仅由用户输入,从最小到最大。 例如:
用户输入:'./a.out 5 10 15 20 25 '
这个程序 returns: "The GCD of the command line args is 5"
但是,我 运行 遇到的问题是,如果用户输入例如:
用户输入:'./a.out 15 10 5 25 20'
本节目return秒:15
谁能告诉我如何解决这个问题?
这就是我的目标:
如果用户输入:'./a.out 15 10 5 25 20'
这个程序应该return: 5
//Header Files
#include<stdio.h>
#include<string.h>
//Main Method
int main(int argc, char *argv[]){
//Declared variables here and print statements
int i,x,y,min;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
//This is the main while loop
while( x !=0 && y !=0 && y != x){
if(x<y){
y=y-x;
}//End first IF statement
if(y<x){
x-x-y;
}//End second IF statement
}//End while loop
//This function returns the converted integral number as an int value
x=atoi(argv[i]);
for(i=2;i<argc;i++){
y=atoi(argv[i]);
}
//The following code gets the GCD and prints from the command line
min = (x>y)?x:y;
for(i=min;i>=1;--i){
if(x%i==0 && y%i==0){
for(i=1;i<argc;i++){
printf("%s\n", argv[i]);
break;}//End for loop
}//End IF statement
}//End For loop
}//End of MAIN
我已经清理过了,这应该对你有用:
在考虑在代码中计算之前初始化每个变量。
#include<stdio.h>
#include<string.h>
//Main Method
int main(int argc, char *argv[]){
//Declared variables here and print statements
int i,x,y,gcd;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
x=atoi(argv[1]);
for(i=2;i<argc;i++){
y=atoi(argv[i]);
while( x !=0 && y !=0 && y != x){
if(x<y){
y=y-x;
}else if(y<x){
x=x-y;
}
}
gcd=x;
}
printf("%d",gcd);
}//End of MAIN
如果您想使用递归。
#include<stdio.h>
#include<string.h>
int GCD(int numbers[], int count)
{
if (count==1)
return numbers[0];
int i, j, min, x, y;
x = numbers[0];
y = numbers[1];
min = (x>y)?x:y;
for(i=min;i>=1;--i){
if(x%i==0 && y%i==0){
int newNumbers[count-1];
newNumbers[0]=i;
for (j=1; j<count-1; j++) {
newNumbers[j] = numbers[j+1];
}
return GCD(newNumbers, count-1);
}
}
return 0;
}
int main(int argc, const char * argv[]) {
//Declared variables here and print statements
int i;
printf("Number of command line args is %d\n", argc);
printf("The GCD is:\t");
int numbers[argc];
for(i=1;i<argc;i++){
numbers[i]=atoi(argv[i]);
}
int result = GCD(numbers, argc);
printf("%d\n", result);
}