So sánh ba số trong c++

Ta sử dụng 1 biến counter c, khởi tạo c = 0, chúng ta sẽ trừ dần giá trị đồng thời của


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

0 cho


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

1, với mỗi lần như vậy ta tăng c lên


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

1. Số nhỏ nhất trong 3 số


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

0sẽ bằng


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

5 đầu tiên khi trừ dần như vậy. Và giá trị của c sẽ là giá trị của số đó.


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

Nhưng cách này không sử dụng được với số âm.

2.2. Sử dụng phép chia

Ta để ý rằng trong C,


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

7, do vậy, nếu


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

8 thì


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

9. Lợi dụng việc đó ta viết lại


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

0 như sau:


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

Phương pháp này không sử dụng được khi 1 trong 3 giá trị của


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

0 bằng


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

5

2.3. Sử dụng dịch bit và phép trừ

Ta nhận thấy rằng đối với 2 số x, y ta có


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

3 Ta sẽ có giá trị min của


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

4 và


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

5 là:

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

Giải thích như sau: ta thấy rằng phép dịch giá trị


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

6 sang phải


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

7 bits (


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

  1. sẽ cho ta biết


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

9 hay

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

0. Trong trường hợp


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

9 vậy bit thứ

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

2sẽ bằng


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

5 (bit

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

2 là bit dấu,


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

5 là dương,


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

1 là âm), ngược lại

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

0 thì bit

min = y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

2 sẽ bằng


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

1 (bit dấu bằng


# include <stdio.h>
int smallest(int x, int y, int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

1). Vậy, trong trường hợp


# include <stdio.h>
int smallest(int x, int y, int z)
{
  if(!(y/x)){
    return !(z/y)?z:y;
  }else{
    return !(z/x)?z:x;
  }
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

9 hay

min = y + (x-y)&0 // min == y

2:

min = y + (x-y)&0 // min == y

Ngược lại,

min = y + (x-y)&1 // min == x

Từ đó ta implement hàm tìm

min = y + (x-y)&0 // min == y

3 2 số như sau:


# define CHAR_BIT 8
int min(int x, int y){
  return y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)));
}

Từ đó ta có hàm tìm

min = y + (x-y)&0 // min == y

3 3 số như sau:


# include <stdio.h>

# define CHAR_BIT 8
int smallest(int x, int y, int z)
{
  return min(x,min(y,z));
}
int min(int x, int y){
  return y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)));
}
int main()
{
   int x = 12, y = 15, z = 5;
   printf("%d", smallest(x, y, z));
   return 0;
}

Trên đây là 3 cách để tìm min 3 số mà không dùng phép toán so sánh. Cảm ơn các bạn đã theo đọc hết bài. Hẹn gặp lại trong những thử thách thú vị khác.