把输入数字每次从9-2除,能整除则记录该数字,最后从小到大输出。

应该算是水题,不过窝第一次写高精度除法,虽然1A,不过中间改了好多次。

/******************************************
Problem: 2325 User:
Memory: 684K Time: 110MS
Language: G++ Result: Accepted
******************************************/
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char numStr[1005];
int num[1005];
int num1[1005];
int ans[1005];
int len;
int cnt;
int in;//因为数字位数会越除越小,用in来记录少了多少位 bool div(int n)
{
int i;
int temp = (num[in] < n);
for (i = in; i < len; ++i) num1[i] = num[i];
for (i = in; i < len; ++i) {
num1[i + 1] += (num1[i] % n) * 10;
num1[i] /= n;
}
if (num1[i]) {
num1[i] = 0;
return false;
} else {
in += temp;
for (i = in; i < len; ++i) num[i] = num1[i];
return true;
}
} int main()
{
int i;
while (scanf("%s", numStr) != EOF) {
len = strlen(numStr);
if (len == 2 && numStr[0] == '-' && numStr[1] == '1')
break;
if (len == 1) {
printf("%c%s\n", '1', numStr);
continue;
}
in = cnt = 0;
memset(num1, 0, sizeof num1);
memset(num, 0, sizeof num);
for (i = 0; i < len; ++i) {
num[i] = numStr[i] - '0';
}
while (!(len - in == 1 && num[in] == 1)) {
//printf("in:%d\n", in);
for (i = 9; i >= 2; --i) {
if (div(i)) {
ans[cnt++] = i;
break;
}
}
//printf("i=%d\n", i);
if (i == 1) break;
}
if (i == 1) printf("There is no such number.");
else
for (i = cnt - 1; i >= 0; --i)
printf("%d", ans[i]);
printf("\n");
}
return 0;
}

  

poj 2325 Persistent Numbers (贪心+高精度)的更多相关文章

  1. POJ 2325 Persistent Numbers#贪心+高精度除法

    (- ̄▽ ̄)-* 这道题涉及高精度除法,模板如下: ]; ];//存储进行高精度除法的数据 bool bignum_div(int x) { ,num=; ;s[i];i++) { num=num*+ ...

  2. poj 2325 Persistent Numbers

    简单的贪心和高精度运算,主要还是要读懂题. #include"iostream" #include"stdio.h" #include"string& ...

  3. POJ - 3652 Persistent Bits

    “模拟”类型,题型容易,使用bitset库对二进制逐位操作,初始化.十进制转二进制(unsigned int).位操作. POJ - 3652 Persistent Bits Time Limit:  ...

  4. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  5. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  6. Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)

    一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...

  7. POJ - 2109 Power of Cryptography(高精度log+二分)

    Current work in cryptography involves (among other things) large prime numbers and computing powers ...

  8. POJ 1142 Smith Numbers(史密斯数)

    Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...

  9. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

随机推荐

  1. qt 5 界面美化

    大家都知道,用UI做起界面来非常方便,但是如果我们不熟练他的操作的话,做起来也会有不少布局的麻烦, 所以,我打算写一篇文章来记录自己参考大牛用代码写界面的文章,感谢百度,感谢各位QT大牛的帮助. 所谓 ...

  2. <六> jQuery 获得内容和属性

    获得内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - ...

  3. javascript移动设备触屏事件

    ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件: /** * onTouchEvent */ var div = d ...

  4. gei shilei d

    body, p { margin: 0; padding: 0; font-size: 16px; } div { margin: 0 auto; } .wrapper { width: 1080px ...

  5. Hadoop集群系类文章

    http://www.cnblogs.com/xia520pi/archive/2012/04/08/2437875.html 后续文章地址:http://www.xiapistudio.com/ta ...

  6. UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mongolian, Hmong, Tai Lu, Tai Mau文字的支持

    UNICODE支持欧洲.非洲.中东.亚洲(包括统一标准的东亚象形汉字和韩国象形文字).但是,UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mon ...

  7. Qt for PC,Qt for iOS,Qt for Android (居士的博客)

    http://blog.csdn.net/Esonpo/article/details/38081607 http://blog.csdn.net/Esonpo/article/details/380 ...

  8. 【HDOJ】5046 Airport

    DLX简单题目. /* 5046 */ #include <iostream> #include <string> #include <map> #include ...

  9. hdu1501Zipper(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1501 搜了下记忆化搜索是嘛 然后就看到这个题了 不过一不小心看到代码了 代码又那么短 一不小心给记住了 然后看了 ...

  10. addlinkedserver

    http://blog.sina.com.cn/s/blog_5321db9d0100f89g.html --创建链接服务器 exec sp_addlinkedserver  'ITSV ', ' ' ...