ผมช่วยเขียน code ให้ครับ สำหรับกรณี +-* และเครื่องหมายวางไว้ระหว่างตัวเลขเท่านั้น (เพิ่งสังเกตว่าวางไว้หน้าสุดได้ แต่ขี้เกียจแก้แล้วครับ) แต่จะถูกต้องมั้ย ก็ลองตรวจสอบกันเองนะครับ ฮ่าๆ
เวลาใช้งานให้ได้ผลลัพธ์ดูง่าย ก็ให้ sort ผลลัพธ์ด้วย
เช่น หากตั้งชื่อโปรแกรมว่า findjump.exe ก็ให้พิมพ์คำสั่งนี้ที่ Command Prompt
findjump | sort > jumpnumber.txt
ก็จะได้ไฟล์ jumpnumber.txt ที่เรียงผลลัพธ์ตั้งแต่ 1 เป็นต้นไป
มีหลายบรรทัดที่ได้ผลลัพธ์เท่ากัน ใครอยากฟิลเตอร์ทิ้งก็แก้ code ตามสบายครับ
C Code:
#include <stdio.h>
void main(void)
{
int aiOper[8]; /* Store Operator Type in 8 Positions. Values are 0 (+), 1 (-), 2 (*) */
int aiNumber[9]; /* Store Sequence of Number */
int i, j; /* Counting or Loop Variables */
long lOne, lTwo, lResult;
int iOper;
/* Initialize Array Operator */
for(i=0; i < 8; i++)
aiOper[i] = 0;
/* Initialize Array Sequence Number From 1, 2, 3, ..., 8, 9 */
for(i=0; i < 9; i++)
aiNumber[i] = i+1;
/* Create Operator Pattern. Number of Operator Pattern = 3^8 */
for(i=0; i < 6561; i++)
{
/* Create Number in Base 3 */
if(i > 0)
aiOper[7]++;
j=7;
while(aiOper[j] > 2)
{
aiOper[j] = 0;
aiOper[j-1]++;
j--;
}
/* One Operator Pattern Complete. To do : Write Your Code Below */
/* Calculate Its Value */
lOne = aiNumber[0];
iOper = aiOper[0];
lTwo = aiNumber[1];
for(j=1; j < 8; j++)
{
if(iOper == 0 || iOper == 1) /* Current Operator is + Addition or - Subtract. Example 1 + 2 */
{
if(aiOper[j] == 0 || aiOper[j] == 1) /* Next Operator is still + Addition or - Subtract. Example 1 + 2 - 3 */
{
if(iOper == 0) /* Current Operator is + Addition */
lOne += lTwo;
else /* Current Operator is - Subtract */
lOne -= lTwo;
iOper = aiOper[j];
lTwo = aiNumber[j+1];
/* Reduce to 3 - 3 */
}
else if(aiOper[j] == 2) /* Next Operator is * Multiply. Example 1 + 2 * 3 */
{
lTwo *= aiNumber[j+1];
/* Reduce to 1 + 6 */
}
}
else if(iOper == 2) /* Current Operator is * Multiply. Example 1 * 2 */
{
lOne *= lTwo;
iOper = aiOper[j];
lTwo = aiNumber[j+1];
/* Example 1 * 2 + 3 , Reduce to 2 + 3 */
/* Example 1 * 2 * 3 , Reduce to 2 * 3 */
}
}
if(iOper == 0)
lResult = lOne + lTwo;
else if(iOper == 1)
lResult = lOne - lTwo;
else if(iOper == 2)
lResult = lOne * lTwo;
if(lResult > 0)
{
/* Print Its Value */
/* Print Complete Number and Operator Pattern */
for(j=1; j < 9; j++)
{
if(aiOper[j-1] == 0)
else if(aiOper[j-1] == 1)
else if(aiOper[j-1] == 2)
}
}
}
}