程序崩溃和有关算术运算编码的一些问题 [英] Program Crashing and some questions about arithmetic operations in coding

查看:119
本文介绍了程序崩溃和有关算术运算编码的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜计算器社区!我有一些问题,我的code。我目前是学生所以基本上我是个初学者。用欧几里德算法,如下所示的code应该分裂,分两个数,直到商达到0,但它只是在最后的分割处理停止转动商0.我不知道之前是否因为这个程序崩溃。希望从你们一个初学者友好的答复。谢谢! (抱歉,如果有已经这样一个存在的问题)

这里的code:

  INT商,quotient2,其余remainder2,X,Y;INT富()
{
    的printf(请输入一个数字:);
    scanf函数(%d个,&安培; X);
    的printf(请输入另一个数字:);
    scanf函数(%d个,&安培; Y);    如果(Y> = X){
        quotient2 = Y / X;
        remainder2 = Y%X;
        的printf(%D =%D(%D)+%d个\\ N,Y,X,quotient2,remainder2);        如果(quotient2!= 0){
            做{
                Y = X;
                X = remainder2;
                quotient2 = Y / X;
                remainder2 = Y%X;
                的printf(%D =%D(%D)+%d个\\ N,Y,X,quotient2,remainder2);
            }而(quotient2!= 0);
        }
    }否则如果(X> Y){
        商= X / Y;
        其余= X%Y;
        的printf(%D =%D(%D)+%d个\\ N,X,Y,商,余数);        如果(商!= 0){
            做{
                X = Y;
                Y =余数;
                商= X / Y;
                其余= X%Y;
                的printf(%D =%D(%D)+%d个\\ N,X,Y,商,余数);
            }而(商!= 0);
        }
    }    系统(暂停);
    返回0;
}


解决方案

如果你的欧几里德算法是计算两个数的最大公约数,这里是做到这一点的方法之一。注意:它不会除以0,它停止当除数是0。

所以问题是,而不是反对分割后卫由0,这是迭代的结束条件反正。

 的#include<&stdio.h中GT;无符号GCD(无符号X,无符号Y){
    无符号Z者除外;
    如果(X == 0 ||ÿ== 0){
        返回0;
    }
    而((Z = Y%X)!= 0){
        Y = X;
        X = Z者除外;
    }
    返回X;
}INT主要(无效)
{
    的printf(20〜20:%U \\ N,GCD(20,20));
    的printf(20〜0:%U \\ N,GCD(20,0));
    的printf(0〜20:%U \\ N,GCD(0,20));
    的printf(20〜16:%U \\ N,GCD(20,16));
    的printf(16〜20:%U \\ N,GCD(16,20));
    的printf(20〜15:%U \\ N,GCD(20,15));
    的printf(15〜20:%U \\ N,GCD(15,20));
    的printf(1〜2%U \\ N,GCD(1,2));
    的printf(2〜1:%U \\ N,GCD(2,1));
    返回0;
}

程序输出:

  20〜20:20
20〜0:0
 0〜20:0
20〜16:4
16〜20:4
20〜15:5
15〜20:5
 1〜2:1
 2〜1:1的

请注意有没有必要换的参数。该算法无论哪种方式圆他们。

Hi stackoverflow community! I'm having some problems with my code. I'm currently a student so basically I'm a beginner. Using Euclidean Algorithm, the code shown below should divide and divide two numbers until the quotient reach 0 but it just stops at the last dividing process just before the quotient turns 0. I don't know if the program crashes because of this. Hoping for a beginner-friendly reply from you guys. Thanks! (sorry if there's already an existing question like this)

Here's the code:

int quotient,quotient2,remainder,remainder2,x,y;

int foo()
{
    printf("Enter a number: ");
    scanf("%d", &x);
    printf("Enter another number: ");
    scanf("%d", &y);

    if(y >= x){
        quotient2 = y / x;
        remainder2 = y % x;
        printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);

        if(quotient2 != 0){
            do{
                y = x;
                x = remainder2;
                quotient2 = y / x;
                remainder2 = y % x; 
                printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2); 
            } while(quotient2 != 0);    
        }
    } else if(x > y){
        quotient = x / y;
        remainder = x % y;
        printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);

        if(quotient != 0){
            do{
                x = y;
                y = remainder;
                quotient = x / y;
                remainder = x % y;
                printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);
            } while(quotient != 0);
        }
    }

    system("pause");
    return 0;
}

解决方案

If your "Euclidean algorithm" is computing the GCD of two numbers, here is one way to do it. Note it does not divide by 0, it stops when the divisor would be 0.

So the point is, rather than guard against dividing by 0, that's the iteration's end condition anyway.

#include <stdio.h>

unsigned gcd(unsigned x, unsigned y) {
    unsigned z;
    if (x == 0 || y == 0) {
        return 0;
    }
    while ((z = y % x) != 0) {
        y = x;
        x = z;
    }
    return x;
}

int main(void)
{
    printf("20 ~ 20 : %u\n", gcd(20, 20));
    printf("20 ~ 0  : %u\n", gcd(20,  0));
    printf(" 0 ~ 20 : %u\n", gcd( 0, 20));
    printf("20 ~ 16 : %u\n", gcd(20, 16));
    printf("16 ~ 20 : %u\n", gcd(16, 20));
    printf("20 ~ 15 : %u\n", gcd(20, 15));
    printf("15 ~ 20 : %u\n", gcd(15, 20));
    printf(" 1 ~  2 : %u\n", gcd( 1,  2));
    printf(" 2 ~  1 : %u\n", gcd( 2,  1));
    return 0;
}

Program output:

20 ~ 20 : 20
20 ~ 0  : 0
 0 ~ 20 : 0
20 ~ 16 : 4
16 ~ 20 : 4
20 ~ 15 : 5
15 ~ 20 : 5
 1 ~  2 : 1
 2 ~  1 : 1

Note there is no need to swap the arguments. The algorithm works no matter which way round they are.

这篇关于程序崩溃和有关算术运算编码的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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