HDOJ 3183 A Magic Lamp(贪心)
2015-11-16 09:47
260 查看
Total Submission(s): 2528 Accepted Submission(s): 991
[align=left]Problem Description[/align]
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
[align=left]Input[/align]
There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
[align=left]Output[/align]
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.
[align=left]Sample Input[/align]
178543 4
1000001 1
100001 2
12345 2
54321 2
[align=left]Sample Output[/align]
13
1
0
123
321
题意: 给出一个数字,在这个数字中取出m个数,问剩下的数最小的结果是多少,不输出前导零。
题解:这个数的长度是strlen,那么含有前导零的结果长度是len=strlen-m。 那么它的第一个数一定是在[0,m]的区间中的最小数,最右的边界一定只能是m,因为除第一个数外,还要取出strlen-m-1个数。假设第一个数的位置是temp, 取第二个数的范围就一定是[temp+1,m+1] ........ 以此类推在去除前导零就可以得到最小的数。
代码如下:
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2528 Accepted Submission(s): 991
[align=left]Problem Description[/align]
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
[align=left]Input[/align]
There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
[align=left]Output[/align]
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.
[align=left]Sample Input[/align]
178543 4
1000001 1
100001 2
12345 2
54321 2
[align=left]Sample Output[/align]
13
1
0
123
321
题意: 给出一个数字,在这个数字中取出m个数,问剩下的数最小的结果是多少,不输出前导零。
题解:这个数的长度是strlen,那么含有前导零的结果长度是len=strlen-m。 那么它的第一个数一定是在[0,m]的区间中的最小数,最右的边界一定只能是m,因为除第一个数外,还要取出strlen-m-1个数。假设第一个数的位置是temp, 取第二个数的范围就一定是[temp+1,m+1] ........ 以此类推在去除前导零就可以得到最小的数。
代码如下:
#include<cstdio> #include<cstring> char num[1010]; char ans[1010]; int main() { int i,temp,m,t,k; while(scanf("%s%d",num,&m)!=EOF) { t=0; k=0; int len=strlen(num)-m; while(len--) { temp=t; for(i=t;i<=m;++i) { if(num[temp]>num[i]) temp=i; } ans[k++]=num[temp]; m++; t=temp+1; } i=0; while(ans[i]=='0'&&i<k)//去除前导零 i++; if(i==k) printf("0\n"); else { for(;i<k;++i) printf("%c",ans[i]); printf("\n"); } } return 0; }
相关文章推荐
- select2 插件使用
- navicat for mysql注册码
- C#编程实现对象与JSON串互相转换实例分析
- ngx_conf_set_str_slot函数解析
- 编程开发的一点学习心得
- Ubuntu 14.10安装显卡驱动
- oracle json 解析函数
- 关于华为x1 7.0无法从eclipse发布的更新as发布的apk
- 跟我学习javascript的浮点数精度
- R性能优化
- 带谷歌搜索功能的搜索镜像地址集
- SQL中的cast()函数
- webclient 爬取网页 禁止自动跳转
- 检测到"_ITERATOR_DEBUG_LEVEL"的不匹配项
- eclipse不能进行保存
- 【web前端】关于窗口内高度,宽度相关的方法
- iOS---升级Xcode之后VVDocumenter-Xcode不能用的解决办法
- 剑指Offer-23-从上往下打印二叉树
- C++中堆和栈的区别,自由存储区、全局静态存储区和常量存储区
- Android setText()异常