有问题的C程序找到2个数字之间的公因数。 [英] Having problem with the C program to find the common factors between 2 numbers.

查看:87
本文介绍了有问题的C程序找到2个数字之间的公因数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序不会打印常用的,而是打印初始化值,该值旨在按程序中的编码递增。



我认为问题在于while循环。



如果更正以下代码而不是编写完全不同的代码,将不胜感激。



我尝试了什么:



 # include   <   conio.h  >  
#include < stdio.h >

int main(){

int a,b,i,j;
int total_a = 0 ,total_b = 0 ;

printf( 输入你的整数:\ n);
scanf( %d%d,& a,& b);

for (i = 1 ,j = 1 ; i< = a,j< = b; i ++,j ++){

if (a %i == 0
total_a = total_a * 10 + i;

if (b%j == 0
total_b = total_b * 10 + j;

}

/ * printf(%d%d ,total_a,total_b);
int total = 0;
printf(%d,total); * /


int rem_a,rem_b,total = < span class =code-digit> 0
;

while (total_a!= 0 ){

rem_a = total_a%10;

while (total_b!= 0 ){

rem_b = total_b%10;

if (rem_a == rem_b){
total = total * 10 + rem_a;
}

total_b / = 10 ;
}

total_a / = 10 ;
}

printf( %d,总计) ;

return 0 ;
}

解决方案

嗯。你需要考虑更多:你的代码没有你想象的那样。

 for(i = 1,j = 1; i< = a, j< = b; i ++,j ++){



在这个循环中, i j 将始终具有相同的值;他们将从一开始,然后他们都将是两个,依此类推。我不确定他们什么时候会停止,因为我之前没有看过带逗号的情况,我怀疑它会编译 - 我在平板电脑上并且目前没有C编译器我不能检查,但我不认为它是有效的C代码。



可能,你需要一对嵌套的循环,而不是一个循环......


The program doesn't print the common but instead the initialized value which is intended to increment as coded in the program.

And i think the problem lies in the while loop.

it would be appreciated if the following code is corrected instead of writing totally different code.

What I have tried:

#include <conio.h>
#include <stdio.h>

int main(){
	
	int a, b, i, j;
	int total_a=0, total_b=0;
	
	printf("Enter your integers: \n");
	scanf(" %d%d", &a, &b);
	
	for(i=1, j=1; i<=a,j<=b; i++,j++){
		
		if(a%i==0)
			total_a = total_a*10 + i;
			
		if(b%j==0)
			total_b = total_b*10 + j;
		
	}
	
	/*printf(" %d %d", total_a, total_b);
	int total=0;
	printf(" %d", total);*/

	int rem_a, rem_b, total =0;

	while(total_a!=0){
		
		rem_a = total_a%10;
		
		while(total_b!=0){
			
			rem_b =total_b%10;
			
			if(rem_a==rem_b){
				total = total*10 + rem_a;
			}
			
			total_b/=10;
		}
		
		total_a/=10;
	}
	
	printf(" %d", total);
	
	return 0;
}

解决方案

Um. You need to think some more about this: your code doesn't do what you think it will.

for(i=1, j=1; i<=a,j<=b; i++,j++){


Inside this loop, i and j will always have the same value; they will both start at one, then they will both be two, and so on. I'm not sure when they will stop, because I've not seen a condition with a comma in it before and I doubt it will compile - I'm on a tablet and have no C compiler at the moment s I can't check, but I don't think it's valid C code.

Probably, you need a nested pair of loops, rather than a single loop...


这篇关于有问题的C程序找到2个数字之间的公因数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆