1-9這9個不重複數字,劃分成三個3位數 i j k,使得i : j : k=1:2:3
阿新 • • 發佈:2019-02-08
本題主要考察邏輯分析能力和耐心,一般能會粗暴使用低效的列舉法,即3個關於i,j,k的for迴圈一個個判斷。
1)數字不重複,i最小數字是123,k最大數字是987,i*3<=987,即i最大數字329 ,需要迴圈329-123=206次
2)當i=num, j=2num,k=3num,判斷123456789是否都用到了。
void Start () {
int i, j, k;
int[] visit = new int[10];
for (i = 123; i <= 329; i++)
{
for (int p = 0; p < visit.Length; p++) visit[p] = 0;
visit[i / 100] = 1;
int charNum = i / 10 % 10; //取i的十位字元
if (charNum!=0&&visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = i % 10; //取i的個位字元
if (charNum != 0&&visit[charNum] == 0)
visit[charNum] = 1;
else continue;
j = i * 2;
charNum = j / 100; //取j的百位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = j / 10 % 10; //取j的十位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = j % 10; //取j的個位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
k = i * 3;
charNum = k / 100; //取k的百位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = k / 10 % 10; //取k的十位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = k % 10; //取k的個位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
Debug.Log(k+"\t"+ j+"\t"+ i); //程式碼能夠執行到這裡說明i : j : k=1:2:3,且123456789都被用到了
}
1)數字不重複,i最小數字是123,k最大數字是987,i*3<=987,即i最大數字329 ,需要迴圈329-123=206次
2)當i=num, j=2num,k=3num,判斷123456789是否都用到了。
void Start () {
int i, j, k;
int[] visit = new int[10];
for (i = 123; i <= 329; i++)
{
for (int p = 0; p < visit.Length; p++) visit[p] = 0;
visit[i / 100] = 1;
int charNum = i / 10 % 10; //取i的十位字元
if (charNum!=0&&visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = i % 10; //取i的個位字元
if (charNum != 0&&visit[charNum] == 0)
visit[charNum] = 1;
else continue;
j = i * 2;
charNum = j / 100; //取j的百位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = j / 10 % 10; //取j的十位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = j % 10; //取j的個位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
k = i * 3;
charNum = k / 100; //取k的百位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = k / 10 % 10; //取k的十位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
charNum = k % 10; //取k的個位字元
if (charNum != 0 && visit[charNum] == 0)
visit[charNum] = 1;
else continue;
Debug.Log(k+"\t"+ j+"\t"+ i); //程式碼能夠執行到這裡說明i : j : k=1:2:3,且123456789都被用到了
}