减少c中的分数

时间:2016-11-15 17:18:37

标签: c reduction

#include <stdio.h>
#include <stdlib.h>
int main(){
    system("color f0");
    int a,b,c,d,e,f,g,h,z;
    printf("First numerator:");
    scanf("%d",&a);
    printf("First denominator:");
    scanf("%d",&b);
    printf("Second numerator:");
    scanf("%d",&c);
    printf("Second denominator:");
    scanf("%d",&d);

    a=a*d;
    c=c*b;
    e=a+c;
    f=b*d;
    printf("Simple form:%d/%d\n",e,f);
    return 0;
}

这是我的代码我希望将这个简单的分数尽可能地降低到最低但不使用数学库

2 个答案:

答案 0 :(得分:2)

你的代码做了一些奇怪的事情:

首先,您要求用户提供两个提名者和两个分母。

所以你的行

printf("Second numerator:");
scanf("%d",&c);
printf("Second denominator:");
scanf("%d",&d);

是多余的,你可以删除它们。

其次,你排队

a=a*d;
c=c*b;
e=a+c;
f=b*d;

可怕 - 读者(和你)将会丢失1个字母的名字。

那么,为什么不为提名者提供名称nominator的变量,为分母提供denominator的变量?等等?

所以用这个替换你的整个代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numerator, denominator, smaller, i;

    system("color f0");

    printf("Numerator  : ");
    scanf("%d",&numerator);

    printf("Denominator: ");
    scanf("%d",&denominator);

    printf("\nOriginal fraction: %d/%d\n", numerator, denominator);

    // Method: We get the smaller number from numerator and denominator
    // and will try all numbers from it decreasing by 1 for common divisor

    smaller = numerator < denominator ? numerator : denominator;

    for (i = smaller; i > 1; --i)
        if (numerator % i == 0 && denominator % i ==0)
        {
            numerator   /= i;
            denominator /= i;
            break;
        }

    printf("Reduced fraction : %d/%d\n", numerator, denominator);
    return 0;
}

答案 1 :(得分:0)

堆栈溢出!= / r / homeworkhelp

伪码算法:

    var path = require('path');
var webpack = require('webpack');

var libraryName = 'webapp';
var version = '1.0';

var config = {
  entry: './app-src/proto.index.js',
  output: { 
    path: __dirname+"/build/lib/", 
    publicPath: "/build/",
    filename: libraryName+'.all.js',
    library: libraryName,
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  target: 'node',
  node: {
      fs: 'empty',
      net: 'empty',
      tls: 'empty',
      crypto: 'empty'
    },
  resolve: {
    root: [
       path.resolve('./app-src/'),
       path.resolve('../cmn/'),
    ]
  },
  module: {
    loaders: [
        { test: /\.json$/, loader: "json-loader" },
        // JS and JSX
        {
            test: /.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
                presets: ['es2015', 'react'],
                retainLines: true
            }
        },
        {
            test: /\.(woff|woff2|eot|ttf)$/,
            loader: 'file-loader?name=fonts/[name].[ext]'
        }
    ]
  },

};

module.exports = config;

检查某些内容是否是最低级的:

get a fraction in the form of a/b

while a/b is not in lowest terms:
    find a common divisor, k
    divide a by k  
    divide b by k
end while

我要离开除数发现者给你,否则它将容易。