동적배열과 버블정렬 연습을 했다.
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable:4996)
int input(int lower, int upper)
{
//cout << "input()" << endl;
int A;
while (1)
{
scanf("%d", &A);
if (A >= lower && A <= upper)
{
break;
}
else
{
;
}
}
return A;
}
void input_arr(int* arr, int n)
{
int i;
for (i = 0; i < n; i++)
{
arr[i] = input(0, 100);
}
return;
}
void bubble_sort(int *arr, int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else
{
;
}
}
}
return;
}
void bubble_sort_descending(int* arr, int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else
{
;
}
}
}
return;
}
int find_result(int *A, int *B, int N)
{
int i;
int result = 0;
int* temp_B;
temp_B = (int*)malloc(N * sizeof(int));
if (temp_B == NULL)
{
printf("fail\n");
return 1;
}
else
{
;
}
for (i = 0; i < N; i++)
{
temp_B[i] = B[i];
}
bubble_sort(A, N);
bubble_sort_descending(temp_B, N);
for (i = 0; i < N; i++)
{
result += A[i] * temp_B[i];
}
free(temp_B);
return result;
}
int main(void)
{
int N;
int* A;
int* B;
N = input(1, 50);
A = (int*)malloc(N * sizeof(int));
B = (int*)malloc(N * sizeof(int));
if (A == NULL || B == NULL)
{
printf("fail\n");
return 1;
}
else
{
;
}
input_arr(A, N);
input_arr(B, N);
printf("%d\n", find_result(A, B, N));
free(A);
free(B);
return 0;
}