化简分数题(C语言)

默认分类 | 2015-10-22 20:12:33 | 2680次阅读 | 0评
#include<stdio.h>
int main() {
  int a, b, c, m, n, f, h, gcd;
  scanf("%d %d", &a, &b);
  if (0 <= a && a <= 10000, 0 <= b && b <= 10000) {
  m = a;
  n = b;
  f = m/gcd;
  h = n/gcd;


  if (a < b)
  a ^= b ^= a ^= b;
  while (b != 0) {
  c = a%b;
  a = b;
  b = c;
  }
  gcd = a;
  if (n == 0) {
      printf("Error\n");
  } else {
      if (m == 0) {
        printf("Zero\n");
      } else {
        printf("%d/%d==>%d/%d\n", m/n, f/h);
      }
  }
}
  return 0;
}
出现问题:

改正版

#include<stdio.h>
int main() {
  int a, b, c, m, n;
  scanf("%d %d", &a, &b);
  m = a;
  n = b;
  while (b != 0) {
  c = a%b;
  a = b;
  b = c;
  }
  if (n == 0) {
      printf("Error\n");
  } else {
      if (m == 0) {
        printf("Zero\n");
     } else {
        printf("%d/%d==>%d/%d\n", m, n, m/a, n/a);
    }
  }
  return 0;
}


博友评论,共0条
浏览8345次