1. 低效方法
/*
4. 给出一个int型数组,其中有三个元素之和等于其中一个数,现在要求找出这三个数
*/
#include <iostream>
#include <stdio.h>
using namespace std;
void get_three_nums(int& a, int& b, int& c, int nums[], int num,int n);
int main()
{
int nums[5] = {1,2,3,4,5};
int a = 0;
int b = 0;
int c = 0;
get_three_nums(a, b, c, nums, 7, 5);
cout << a <<" "<< b <<" "<< c << endl;
system("pause");
return 0;
}
void get_three_nums(int& a, int& b, int& c, int nums[], int num, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
if (num == nums[i] + nums[j] + nums[k])
{
a = nums[i];
b = nums[j];
c = nums[k];
break;
}
}
}
}
}