Submission
Status:
P-P--P-P--
Subtask/Task Score:
40/100
Score: 40
User: theem1502
Problemset: จับคู่เลขมงคล
Language: c
Time: 0.002 second
Submitted On: 2025-09-20 14:13:37
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
scanf("%d", &num);
int luckynumber;
int *thearray = malloc(num * sizeof(int));
for (int i = 0; i < num; i++) {
scanf("%d", &thearray[i]);
}
scanf("%d", &luckynumber);
for (int i = 0; i < num; i++) {
for (int j = i+1; j< num; j++)
{
if (thearray[j] < thearray[i]) {
int temp = thearray[i];
thearray[i] = thearray[j];
thearray[j] = temp;
}
} }
int firstpointer = 0;
int secondpointer = num-1;
int **destinationarray = malloc(num*sizeof(int*));
for (int i = 0; i < num; i++) {
destinationarray[i] = malloc(2 * sizeof(int));
}
int temp = 0;
for (int i = 0; i < num; i++) {
if (firstpointer >= secondpointer) {
break;
}
int sum = thearray[firstpointer] + thearray[secondpointer];
if (sum == luckynumber) {
destinationarray[temp][0] = thearray[secondpointer];
destinationarray[temp][1] = thearray[firstpointer];
secondpointer -= 1;
temp++;
continue;
}
else if(sum > luckynumber) {
secondpointer-=1;
}
else {
firstpointer += 1;
}
}
if (temp == 0) {
printf("%s", "NO");
return 0;
}
for (int i = 0; i < temp; i++) {
printf("%d %d\n", destinationarray[i][0], destinationarray[i][1]);
}
}