//如题:凑14;输入很多个整数(1<=数值<=13),任意两个数相加等于14就可以从数组中删除这两个数, 求剩余数(按由小到大排列);比如:输入数组[9,1,9,7,5,13], 输出数组[7,9]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo_01
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 3,1,1,7,3, 6,7, 7,9, 13, 5, 8 };//要凑14的数组
List<int> list =new List<int>( cou14(arr));//list接收
list.Sort();//从小到大排序
foreach (var item in list)
{
if(item!=14)
Console.Write(item+"=");//输出
}
Console.ReadKey();
}
//================================================================
//凑14 的方法
static int[] cou14(int[] arr)
{
for (int i = 0; i < arr.Length-1; i++)
{
for (int j = i+1; j < arr.Length; j++)
{
if (arr[i] + arr[j] == 14) //如果凑出14,则统统标记为14
{
arr[i] = 14;
arr[j] = 14;
}
}
}
return arr;//返回数组
}
}
}
(ps:我验证了没有出错,VS2012编译器)