ural 1261. Tips(进制运算)
2013-11-10 11:56
106 查看
1261. Tips
Time limit: 1.0 secondMemory limit: 64 MB
The favorite resting place of the Ural programmers is Threenland island. There is only one tribulation: dollars and euro don’t go about here. So the tourists are to exchange their money into threets (the currency of Threenland). There go about 1 threet, 3 threets, 9 threets, 27 threets, …, 3k threets, … Once programmer Vasechkin, after the N-threets bill was given to him found out, that he’d got one paper of each denomination. Waiters in Threenland have a custom to keep the change. Waiters like to get the tip that can be presented by a set of papers in which paper of each denomination appears not more than once. Otherwise their feelings are hurt. They have a peeve on a client if they don’t get tips at all. Help please Vasechkin to pay for the dinner and not to hurt the waiter.
Input
consists of an integer N. 1 ≤ N ≤ 107.Output
Output two integers separated with a space – that is the sum that Vasechkin is to pay and an amount of tips. If there are several solutions choose any of them. If there is no solution output 0. Remember that Ural programmers are not rich, so Vasechkin can’t pay more than 4294967291 threets.Sample
input | output |
---|---|
5 | 9 4 |
思路:利用进制运算的思想,将给出的数n变成3进制表示,然后再2出现的位置加1,直到没有了2为止,并且在加的过程中记录加数;最后这个加数便是b;
#include <iostream> using namespace std; int tip=0,bill=0,n,m=1; int main() { cin >> n; bill=n; do { if (n%3==2) { tip+=m; ++n; } n/=3,m*=3; }while (n); if (!tip) tip=m; cout <<bill+tip<< ' ' << tip << endl; return 0; }
View Code
相关文章推荐
- ural 1261. Tips
- nyoj 244 16 进制简单运算
- URAL 1500. Pass Licenses 枚举+位运算
- Datediff函数 助你实现不同进制时间之间的运算
- 38.16进制的简单运算
- 使用 Shell 运算进行进制转换 16进制转10进制
- 位运算--求一个 数二进制中1的个数
- 进制的存储和运算(3)——浮点数强制转换小议
- 第002课 进制的算术运算(1)
- 好的JAVA思路——利用补码运算和位运算的巧用来实现进制转化
- 进制间的转换,以及逻辑运算,位运算在进制间的使用
- 计算机体系结构--进制及其运算
- c语言中的位运算与进制(一)
- 二进制到十六进制转换法加各种进制的运算
- java、进制、数据类型与运算
- 两位数乘法运算——进制的编程
- URAL 1468 Fraction(化k进制小数模拟)
- java进制的转换与运算
- python time 转换&运算tips
- 二进制到十六进制转换法加各种进制的运算