English 中文(简体)
C区3个浮标中最快的峰值
原标题:Fastest min max among three floats in C

我认为,这个问题非常清楚: 我有三人,我有两个职能,即权责和权责。

执行最快的是什么?

问题回答

The C standard (C99) provide the >fmaxf >>>>/code> http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/math.h.html 负责人。 我首先要利用它来找到三个浮标中的最大部分,然后检查这是否足以满足你的需要:

float max = fmaxf(a, fmaxf(b, c));
float min = fminf(a, fminf(b, c));

宽松的解决办法是进行两次比较,以发现小数,然后进行两次比较,以找到最高点。 这是一种不理想的情况,因为三个比较(假编码回归(主要、最大)图如下):

function minmax(float a, float b, float c): (float, float)
begin
  boolean altb = (a < b);
  boolean bltc = (b < c);
  if altb and bltc then return (a, c);
  if not altb and not bltc then return (c, a);
  if altb then // Also: c <= b, so b is known to be max
  begin
    if a < c then return (a, b);
    return (c, b);
  end
  if bltc then // Also: b <= a, so b is known to be min
  begin
    if a < c then return (b, c);
    return (b, a);
  end
  // Unreachable.
end

(This is written to be most readable, rather than to minimize the branch count)

比较数字为2比3。 不可能只使用2个比较,因为有3个! = 6个3个浮标,2个比较只能区分4个不同。 这个问题在决策树上很容易看到。

In practice one should rely for optimizations like this one on the compiler.

法典最多可找到三人。

#includ<stdio.h>

void main()

{
    float a,b,c;
    printf("enter a,b,c:");
    scanf("%f%f%f",&a,&b,&c); 
    printf("Greatest no: %f"(a>b)?((a>c)?a:c):((c>b)?c:b)); 
}

2. 反省:

我喜欢Adam Zalcman如此之多的做法,我把它改写在C,这一时间也把分行减少到最低限度(最多3个比较和3个分支)。

struct Pair
{
    float min_value;
    float max_value;
};

struct Pair minmax(float a, float b, float c)
{
    /* truth table:
     * a<b b<c a<c
     *  T   T   *  => (a, c)
     *  T   F   T  => (a, b)
     *  T   F   F  => (c, b)
     *  F   T   T  => (b, c)
     *  F   T   F  => (b, a)      
     *  F   F   *  => (c, a)
     */
    if (a < b) {
        if (b < c) 
            return (struct Pair) {a, c};
        else {
            if (a < c)
                return (struct Pair) {a, b};
            else
                return (struct Pair) {c, b};
        }
    } else {
        if (b < c) {
            if (a < c)
                return (struct Pair) {b, c};
            else
                return (struct Pair) {b, a};
        } else
            return (struct Pair) {c, a};
    }
}

void foo()
{
    struct Pair result = minmax(1.0, 2.0, 3.0);
}

(this may be degenerating into code golf now though ...)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签