Jump to content


Photo

ทดสอบการใช้ if statement ให้กระชับที่สุด c or java


  • Please log in to reply
4 ความเห็นในกระทู้นี้

#1 ทรงธรรม

ทรงธรรม

    ต่อให้ต้องเรียนจนแก่ ก็จะเรียนต่อไป คนเราพัฒนาได้ทุกคน

  • Members
  • PipPipPip
  • 2,157 posts

ตอบ 28 กันยายน พ.ศ. 2556 - 15:09

คือ พอดีมีโจทย์ จากหนังสือ เขาให้มา อย่างนี้อะครับ

 

พอดี เป็นเรื่อง if statement

 

เขาให้โจทย์ว่า ให้ number มา 4 ตัว (จาก standard in)

 

แล้วให้เรา หา smallest และ largest

 

โดยใช้ if statement แค่ 4 ตัว

 

 

ผมก็ลองไปดูของคนอื่นมา แล้ว ก็ลองมาปรับดู

 

คือบางคน ก็ใช้ function อื่นด้วย

 

แต่ผมอยาก ใช้แค่ if จริง ๆ

 

 

 

มันเลยออกมา แบบย่อ ๆ คือ

 

กำหนด int ไว้รองรับ 4 ตัว a , b , c , d

 

กำหนด int ไว้สำหรับการประกบคู่ (คล้ายประกบคู่ 4 ทีม สุดท้าย)

 

int min1 , min2 , max1 , max2

 

กำหนด int สำหรับรอบชิงชนะเลิศ และ รอบชิงที่ 3

 

int min , max

 

 

แล้วก็ดูผล รอบรอง

 

if ( a > b ) {

   min1 = b; max1 = a;

}else{

   min1 = a; max1 = b;

}

 

if ( c > d ) {

   min2 = d; max2 = c;

}else{

   min2 = c; max2 = d;

}

 

แล้วก็ถึงรอบชิง

 

if ( min1 > min2 ) {

   min = min2;

}else{

   min = min1;

}

 

 

if ( max1 > max2 ) {

   max = max1;

}else{

   max = max2

}

 

แบบนี้ จะใช้ if statement 4 คู่ (คือรวม else ด้วย)

 

แต่คือ อยากรู้ว่า แบบนี้ จะใช้ variable มากไปหรือเปล่า

 

แล้วจะมี วิธีไหน ให้กระชับ กว่านี้ ได้อีก

 

แบบ ผมอยากให้ ไม่ใช้ function อื่น เข้าร่วม อะครับ

 

เห็นของ คนอื่น เขาใช้ swap เข้าช่วย


Edited by ทรงธรรม, 28 กันยายน พ.ศ. 2556 - 15:10.

ขอให้พวกเรา ชาวหลากสี และพันธมิตร จงมีชีวิตรอด จากภาวะเศรษฐกิจตกต่ำ ฝีมือปูนา ไปตลอดรอดฝั่งด้วยครับ

 

PEMDAS ย่อมาจาก ลำดับการคำนวณ Parentheses , Exponentials , Multiply , Divide , Add , Subtract

 

FWGHSO ย่อมาจาก ลำดับการประเมินผลของ query  FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY


#2 CockRoachKiller

CockRoachKiller

    ขาประจำ

  • Members
  • PipPipPip
  • 442 posts

ตอบ 30 กันยายน พ.ศ. 2556 - 11:46

/* NO IF */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

float Min(float a,float B )
{
    return(((a+b )-abs(a-B ))/2) ;
}

float Max(float a,float B )
{
    return(((a+b )+abs(a-B ))/2) ;
}

float Min4(float a,float b,float c,float d)
{
    return(Min(Min(a,b ),Min(c,d))) ;
}

float Max4(float a,float b,float c,float d)
{
    return(Max(Max(a,b ),Max(c,d))) ;
}


int main(int argc, char *argv[])
{
    printf("Min=%f Max=%f",Min4(10,11,12,13),Max4(10,11,12,13)) ;
    getch() ;
}
 
 
Your solution is good enough.   I don't see the need in optimizing the number of IF beyond that.  If you really need more speed, just code in assembly and optimize at that level.


Edited by CockRoachKiller, 30 กันยายน พ.ศ. 2556 - 11:48.

Please know your place by do not comment my post that you can not comprehend.

#3 หงส์เฒ่าเสาร์ธรรม

หงส์เฒ่าเสาร์ธรรม

    ขาประจำ

  • Members
  • PipPipPip
  • 1,408 posts

ตอบ 4 ตุลาคม พ.ศ. 2556 - 07:15

if A>B then  max=A min=B  else max=B min=A

if min>C then min=C else if max <C then max=C

if min>D then min=D else if max <D then max=D

 

if statements = n-1


Edited by หงส์เฒ่าเสาร์ธรรม, 4 ตุลาคม พ.ศ. 2556 - 16:05.

จินตนาการสำคัญกว่าความรู้ ... ศีลธรรม เป็นกรอบรักษาจินตนาการให้ดำรงอยู่ด้วยความดีงาม... -_- 


#4 หงส์เฒ่าเสาร์ธรรม

หงส์เฒ่าเสาร์ธรรม

    ขาประจำ

  • Members
  • PipPipPip
  • 1,408 posts

ตอบ 4 ตุลาคม พ.ศ. 2556 - 22:52

I use VBA in Excel and come out this code. 
 
Sub test1()
Dim myArray() As Variant
Dim i As Integer
Dim MaxA As Integer
Dim MinA As Integer
 
myArray = Array(3, 5, 1, 9)
MaxA = myArray(1)
MinA = myArray(1)
 
For i = 1 To UBound(myArray)
 If MaxA < myArray(i) Then
  MaxA = myArray(i)
 Else
   If MinA > myArray(i) Then
    MinA = myArray(i)
   End If
 End If
Next i
 
MsgBox "max =" & MaxA
MsgBox "min =" & MinA
 
End Sub

จินตนาการสำคัญกว่าความรู้ ... ศีลธรรม เป็นกรอบรักษาจินตนาการให้ดำรงอยู่ด้วยความดีงาม... -_- 


#5 CockRoachKiller

CockRoachKiller

    ขาประจำ

  • Members
  • PipPipPip
  • 442 posts

ตอบ 20 ตุลาคม พ.ศ. 2556 - 07:26

For me,
If .. Else if..... should be counted as 2 if. Because in lower level, it has 2 compare instructions.
for loop solution also suffer from the fact that processor need to execute at least 2 compare instructions on each loop.

Anyone who appreciate those solutions should check out C Macro, ex #define. You can hide if in macro and no if in C code. To me, this is cheating.

Scholar should seek the reason behind this simple programming quiz. Here are my humble insight.
- lesser if create more readable code which easy to understand and fix. Most solution should answer this simple requirement.

- lesser if make code run faster due to instruction pipeline. Most solution does not aware of this. In more advance computer engineer you will be asked to write minimum if in machine level code to avoid breaking the instruction pipeline.



At the end, you need to know your objective.
Please know your place by do not comment my post that you can not comprehend.




ผู้ใช้ 0 ท่านกำลังอ่านกระทู้นี้

สมาชิก 0 ท่าน, ผู้เยี่ยมชมทั่วไป 0 ท่าน และไม่เปิดเผยตัวตน 0 ท่าน