poj 2325 Persistent Numbers (贪心+高精度)
把输入数字每次从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 (贪心+高精度)的更多相关文章
- POJ 2325 Persistent Numbers#贪心+高精度除法
(- ̄▽ ̄)-* 这道题涉及高精度除法,模板如下: ]; ];//存储进行高精度除法的数据 bool bignum_div(int x) { ,num=; ;s[i];i++) { num=num*+ ...
- poj 2325 Persistent Numbers
简单的贪心和高精度运算,主要还是要读懂题. #include"iostream" #include"stdio.h" #include"string& ...
- POJ - 3652 Persistent Bits
“模拟”类型,题型容易,使用bitset库对二进制逐位操作,初始化.十进制转二进制(unsigned int).位操作. POJ - 3652 Persistent Bits Time Limit: ...
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...
- POJ - 2109 Power of Cryptography(高精度log+二分)
Current work in cryptography involves (among other things) large prime numbers and computing powers ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
随机推荐
- org.springframework.web.servlet.view.InternalResourceViewResolver
http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...
- 自动寻路NavMesh
步骤 1.创建地形 2.添加角色 3.创建多个障碍物,尽量摆放的复杂些,用来检测NavMesh的可用性和效率 4.选中地形,在Navigation窗口中,设置Navigation Static 5.依 ...
- 洛谷1508 Likecloud-吃、吃、吃
题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...
- CoreText实现图文混排之点击事件-b
CoreText实现图文混排之点击事件 主要思路 我们知道,CoreText是基于UIView去绘制的,那么既然有UIView,就有 -(void)touchesBegan:(NSSet<UIT ...
- 微软Hololens学院教程-Hologram 211-Gestures(手势)【微软教程已经更新,本文是老版本】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
- java单例模式使用及注意事项
1. 说明 1)单例模式:确保一个类只有一个实例,自行实例化并向系统提供这个实例 2)单例模式分类:饿单例模式(类加载时实例化一个对象给自己的引用),懒单例模式(调用取得实例的方法如getInstan ...
- Centos安装gnome主菜单编辑器无
首选项---主菜单-- 即是alacarte.. centos ===安装 alacarte.noarch 0:0.12.4-1.el6 即可.
- Ollydbg 中断方法浅探
Ollydbg是一个新的32位的汇编层调试软件.适应于windows98.me.2000.xp和2003操作系统.由于他具有图形窗口界面,所以操作方便.直观,是cracker的好工具. 由于Ollyd ...
- 修改Tomcat内存大小
Windows下,在文件/bin/catalina.bat,Linux下,在文件/bin/catalina.sh的前面,增加如下设置: JAVA_OPTS=-Xms[初始化内存大小] -Xmx[可以使 ...
- char 与 unsigned char的本质区别
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...