C#实现:给定[0-9]数组,求用数组组成的任意数字的最小值
class Program
{
static void Main(string[] args)
{ List<int> c = new List<int>() { , , , , };
c.Sort();
var result = getNumber(c, );
Console.WriteLine("{0}", result); Console.ReadKey(); } private static int getNumber(List<int> c, int k)
{
c.Sort();
int result = ;
int kk = k;
List<int> eachNum = new List<int>();
//convert each bit of k to a list.
while (k != )
{
eachNum.Add(k % );
k = k / ;
}
//get the each bit which is larger or equal the same position of k.
for (int i = eachNum.Count - ; i >= ; i--)
{
result = result * + getCurrentNum(c, eachNum[i]);
} //If the result by below is small than target. Replace the number from low position.
int j = ;
while (result <= kk && j < eachNum.Count)
{
j++;
if (c.Where(item => item > eachNum[j - ]).Count() == )
{ continue;
} result = (result / (int)Math.Pow(, j)) * (int)Math.Pow(, j);
result = result + getMinValLargerThanT(c.Where(item => item > eachNum[j - ]).First(), c.First(), j - ); } //If the result is still small than target, carry in adding.
if (result <= kk)
{
result = getMinValLargerThanT(c.Where(item => item > ).First(), c.First(), eachNum.Count);
} return result;
} public static int getMinValLargerThanT(int firstIsNotZero, int firstNum, int length)
{
int result = firstIsNotZero;
for (int i = ; i < length; i++)
{
result = result * + firstNum;
}
return result;
} //Get for each number min value larger than the same position.
public static int getCurrentNum(List<int> c, int highNum)
{
foreach (int cItem in c)
{
if (cItem >= highNum)
{
return cItem;
}
}
return ;
}
}
C#实现:给定[0-9]数组,求用数组组成的任意数字的最小值的更多相关文章
- 【POJ2774】Long Long Message(后缀数组求Height数组)
点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...
- Java后缀数组-求sa数组
后缀数组的一些基本概念请自行百度,简单来说后缀数组就是一个字符串所有后缀大小排序后的一个集合,然后我们根据后缀数组的一些性质就可以实现各种需求. public class MySuffixArrayT ...
- 用n(0)次求一个数组里面最大子数组的和(数组可以输入负数)
今天老师布置了题目上的任务,可谓是杀死人脑细胞不偿命呐... 在课上叽叽咕咕的讨论了一节课也没有答案,只得出几个备选方案,一个是通过枚举法将数组里面的子数组和一个个列出来然后在进行比较,可想而知(n2 ...
- 牛客练习赛33 D tokitsukaze and Inverse Number (树状数组求逆序对,结论)
链接:https://ac.nowcoder.com/acm/contest/308/D 来源:牛客网 tokitsukaze and Inverse Number 时间限制:C/C++ 1秒,其他语 ...
- 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- [nowCoder] 两个不等长数组求第K大数
给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14874 Accepted: 5118 De ...
- POJ-2299 Ultra-QuickSort---树状数组求逆序对+离散化
题目链接: https://vjudge.net/problem/POJ-2299 题目大意: 本题要求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 两个数(a, ...
- K:求取数组中最大连续子序列和的四个算法
相关介绍: 求取数组中最大连续子序列和问题,是一个较为"古老"的一个问题.该问题的描述为,给定一个整型数组(当然浮点型也是可以的啦),求取其下标连续的子序列,且其和为该数组的所有 ...
随机推荐
- MC的内存管理和删除机制
先看一下,什么叫做内存的碎片化: 如果用c语言直接 malloc,free 来向操作系统申请和释放内存时, 在不断的申请和释放过程中,形成了一些很小的内存片断,无法再利用. 这种空闲,但无法利用内存的 ...
- Diamond使用向导
一.背景描述 Diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管理. ...
- 15.8.6 AUTO_INCREMENT Handling in InnoDB
1 传统模式 innodb_autoinc_lock_mode (“traditional” lock mode) 2 连续模式 innodb_autoinc_lock_mode (“consecut ...
- SimpleChannelInboundHandler和ChannelInboundHandlerAdapter区别
一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢? 其实 ...
- 《IT蓝豹》完整阅读软件客户端app
完整阅读软件客户端app,本项目里面还有扫描功能,搜索本地书籍,不过扫码功能通过c++层实现的. 本项目来自:https://github.com/JayFang1993/ScanBook Captu ...
- python之路-Day4
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 函数是什么? 函数一词来源于数学,但编程中的「 ...
- jquery-1.3.2.js
Uncaught SyntaxError: Unexpected identifier
- nullcom HackIM2016 -- Programming Question 4
One of the NullCon vidoes talked about a marvalous Russian Gift. The Vidoe was uploaded on [May of 2 ...
- HTML5 新增属性和废除属性
删除的属性大多都是可以用CSS替代的一些样式属性 设置网页标题前面的小图标的大小:size <link rel="icon" href="demo_icon.gif ...
- 点击更多button显示更多数据的功能实现思路代码
此功能是根据自己思路去慢慢做出来的,做的不够专业,希望有懂这个的前辈给自己指点指点. //分界线———————————————————————————————————————————————————— ...