หัวข้อ: jump number puzzle
ดูหนึ่งข้อความ
  #11  
Old 13 มิถุนายน 2005, 14:54
TOP's Avatar
TOP TOP ไม่อยู่ในระบบ
ผู้พิทักษ์กฎขั้นสูง
 
วันที่สมัครสมาชิก: 27 มีนาคม 2001
ข้อความ: 1,003
TOP is on a distinguished road
Smile

ผมช่วยเขียน code ให้ครับ สำหรับกรณี +-* และเครื่องหมายวางไว้ระหว่างตัวเลขเท่านั้น (เพิ่งสังเกตว่าวางไว้หน้าสุดได้ แต่ขี้เกียจแก้แล้วครับ) แต่จะถูกต้องมั้ย ก็ลองตรวจสอบกันเองนะครับ ฮ่าๆ

เวลาใช้งานให้ได้ผลลัพธ์ดูง่าย ก็ให้ sort ผลลัพธ์ด้วย
เช่น หากตั้งชื่อโปรแกรมว่า findjump.exe ก็ให้พิมพ์คำสั่งนี้ที่ Command Prompt

findjump | sort > jumpnumber.txt

ก็จะได้ไฟล์ jumpnumber.txt ที่เรียงผลลัพธ์ตั้งแต่ 1 เป็นต้นไป
มีหลายบรรทัดที่ได้ผลลัพธ์เท่ากัน ใครอยากฟิลเตอร์ทิ้งก็แก้ code ตามสบายครับ

C Code:
  1. #include <stdio.h>
  2.  
  3. void main(void)
  4. {
  5.     int aiOper[8]; /* Store Operator Type in 8 Positions. Values are 0 (+), 1 (-), 2 (*) */
  6.     int aiNumber[9]; /* Store Sequence of Number */
  7.    
  8.     int i, j; /* Counting or Loop Variables */
  9.  
  10.     long lOne, lTwo, lResult;
  11.     int iOper;
  12.    
  13.     /* Initialize Array Operator */
  14.     for(i=0; i < 8; i++)
  15.         aiOper[i] = 0;
  16.    
  17.     /* Initialize Array Sequence Number From 1, 2, 3, ..., 8, 9 */
  18.     for(i=0; i < 9; i++)
  19.         aiNumber[i] = i+1;
  20.    
  21.     /* Create Operator Pattern. Number of Operator Pattern = 3^8 */
  22.     for(i=0; i < 6561; i++)
  23.     {
  24.         /* Create Number in Base 3 */
  25.         if(i > 0)
  26.             aiOper[7]++;
  27.        
  28.         j=7;
  29.         while(aiOper[j] > 2)
  30.         {
  31.             aiOper[j] = 0;
  32.             aiOper[j-1]++;
  33.             j--;
  34.         }
  35.        
  36.         /* One Operator Pattern Complete. To do : Write Your Code Below */
  37.  
  38.         /* Calculate Its Value */
  39.         lOne = aiNumber[0];
  40.         iOper = aiOper[0];
  41.         lTwo = aiNumber[1];
  42.         for(j=1; j < 8; j++)
  43.         {
  44.             if(iOper == 0 || iOper == 1)  /* Current Operator is + Addition or - Subtract. Example 1 + 2 */
  45.             {
  46.                 if(aiOper[j] == 0 || aiOper[j] == 1) /* Next Operator is still + Addition or - Subtract. Example 1 + 2 - 3 */
  47.                 {
  48.                     if(iOper == 0) /* Current Operator is + Addition */
  49.                         lOne += lTwo;
  50.                     else /* Current Operator is - Subtract */
  51.                         lOne -= lTwo;
  52.                    
  53.                     iOper = aiOper[j];
  54.                     lTwo = aiNumber[j+1];
  55.                     /* Reduce to 3 - 3 */
  56.                 }
  57.                 else if(aiOper[j] == 2) /* Next Operator is * Multiply. Example 1 + 2 * 3 */
  58.                 {
  59.                     lTwo *= aiNumber[j+1];
  60.                     /* Reduce to 1 + 6 */
  61.                 }
  62.             }
  63.             else if(iOper == 2) /* Current Operator is * Multiply. Example 1 * 2 */
  64.             {
  65.                 lOne *= lTwo;
  66.                 iOper = aiOper[j];
  67.                 lTwo = aiNumber[j+1];
  68.                 /* Example 1 * 2 + 3 , Reduce to 2 + 3 */
  69.                 /* Example 1 * 2 * 3 , Reduce to 2 * 3 */
  70.             }
  71.         }
  72.        
  73.         if(iOper == 0)
  74.             lResult = lOne + lTwo;
  75.         else if(iOper == 1)
  76.             lResult = lOne - lTwo;
  77.         else if(iOper == 2)
  78.             lResult = lOne * lTwo;
  79.        
  80.         if(lResult > 0)
  81.         {
  82.             /* Print Its Value */
  83.             printf("%6ld = ", lResult);
  84.            
  85.             /* Print Complete Number and Operator Pattern */
  86.             printf("%d", aiNumber[0]);
  87.             for(j=1; j < 9; j++)
  88.             {
  89.                 if(aiOper[j-1] == 0)
  90.                     printf("+");
  91.                 else if(aiOper[j-1] == 1)
  92.                     printf("-");
  93.                 else if(aiOper[j-1] == 2)
  94.                     printf("*");
  95.                
  96.                 printf("%d", aiNumber[j]);
  97.             }
  98.             printf("\n");
  99.         }
  100.     }
  101. }
__________________
The difference between school and life?
In school, you're taught a lesson and then given a test.
In life, you're given a test that teaches you a lesson.

25 มีนาคม 2007 14:26 : ข้อความนี้ถูกแก้ไขแล้ว 3 ครั้ง, ครั้งล่าสุดโดยคุณ TOP
ตอบพร้อมอ้างอิงข้อความนี้