0
点赞
收藏
分享

微信扫一扫

C#,超级阿格里数字(超级丑数,Super Ugly Number)的算法与源代码

北溟有渔夫 2022-05-01 阅读 66
c#算法

超级阿格里数字(超级丑数,Super Ugly Number)由丑数(Ugly Number)拓展而来,不过其因子质数,是事先给定的。

 

上面的超级丑数由质数因子(5,7,9)生成。

源程序:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
	public static partial class Number_Sequence
	{
		/// <summary>
		/// 超级阿格里数字(超级丑数)
		/// </summary>
		/// <param name="primeArray">给定的质数数组</param>
		/// <param name="n"></param>
		/// <returns></returns>
		public static int SuperUgly_Number(int[] primeArray, int n)
		{
			int size = primeArray.Length;
			if (n <= 0)
			{
				return -1;
			}
			if (n == 1)
			{
				return 1;
			}
			List<int> heap = new List<int>();

			for (int i = 0; i < size; i++)
			{
				heap.Add(primeArray[i]);
			}

			int count = 1;
			int number = 0;
			heap.Sort();

			while (count < n)
			{
				number = heap[0];
				heap.RemoveAt(0);
				if (number != heap[0])
				{
					count++;
					for (int i = 0; i < size; i++)
					{
						heap.Add(number * primeArray[i]);
					}
				}
				heap.Sort();
			}
			return number;
		}
	}
}

————————————————————————————

POWER BY TRUFFER.CN

举报

相关推荐

0 条评论