把输入数字每次从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. windows下使用eclipse调试C程序

    一.环境描述 Eclipse IDE for C/C++ Developers version 4.4.0 MinGW  gcc/g++ version 4.8.1;gdb version 7.6.1 ...

  2. qt 5 界面美化

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

  3. win2003 sp2+iis 6.0上部署.net 2.0和.net 4.0网站的方法

    网站环境 IIS6.0,操作系统Windows server2003 sp2,服务器之前已经部署了.net 2.0和asp的网站,现在要部署新开发的.net 4.0网站.本来认为很简单,却遇到了很多问 ...

  4. php的post和get方法

    <?php function post($url,$fields) { $fields_string = ''; foreach($fields as $key=>$value) { $f ...

  5. 转 JavaScript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    收藏一下 1.判断select选项中 是否存在Value=”paraValue”的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.删除select中选中 ...

  6. Echarts-JAVA

    http://www.oschina.net/p/echarts-java http://my.oschina.net/flags/blog/316920

  7. java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.

    java的类必须大写 java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致. 在dos用上下箭头,调用已用过的命令

  8. [dp]HDOJ4960 Another OCD Patient

    题意: 给一个n, 第二行给n堆的价值v[i], 第三行给a[i].  a[i]表示把i堆合在一起需要的花费. 求把n堆变成类似回文的 需要的最小花费. 思路: ①记忆化搜索 比较好理解... dp[ ...

  9. 安装Ubuntu服务器

    安装edX首先需要一台linux或Mac系统的电脑/服务器. 这里以常见的Ubuntu作为服务器系统. Ubuntu的官方网站为http://www.ubuntu.com,中文网站为http://ht ...

  10. cat主要有三大功能

    cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename     只能创建新文件,不能编辑已有文件.3.将几个文件合并为一 ...