TopCoder SRM665 Div2 A
2015-08-12 23:23
405 查看
Problem Statement
A lucky number is a positive integer consisting of only the digits 4 and 7.Given an int a, return an int b strictly greater than a, such that a XOR b is a lucky number. (See Notes for the definition of XOR.) The number b should be in the range 1 to 100, inclusive. If such a number does not exist, return -1. If there are multiple such b, you may return any of them.
Definition
Class:
LuckyXorMethod:
constructParameters:
intReturns:
intMethod signature:
int construct(int a)(be sure your method is public)
Limits
Time limit (s):2.000
Memory limit (MB):
256
Stack limit (MB):
256
Notes
XOR is the bitwise exclusive-or operation. To compute the value of P XOR Q, we first write P and Q in binary. Then, each bit of the result is computed by applying XOR to the corresponding bits of the two numbers, using the rules 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, and 1 XOR 1 = 0.For example, let’s compute 21 XOR 6. In binary these two numbers are 10101 and 00110, hence their XOR is 10011 in binary, which is 19 in decimal.
You can read more about the XOR operation here: https://en.wikipedia.org/wiki/Exclusive_or
Constraints
a is between 1 and 100, inclusive.
Examples
0)
4Returns: 40
4 XOR 40 = 44, 44 is a lucky number.
1)
19Returns: 20
19 XOR 20 = 7
2)
88Returns: 92
88 XOR 92 = 4
3)
36Returns: -1
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
题意
只包含4和7的数字是幸运数字,现在给一个数字a(0≤a≤100),要求返回一个整数b(a<b≤100)使得a⊕b为一个幸运数字题解
a,b都小于一百,转换成二进制最多为7位0⊕0=0,所以a⊕b转换成二进制最多有7位,二进制七位以下的幸运数字有4,7,44,47,74,77,没了。。然后如果a⊕b=c那么b⊕c=a,为啥呢。。因为0⊕0=0,1⊕1=0,1⊕0=1,0⊕1=1。。没了。。然后用上面的六个幸运数组异或枚举就好,能够造出大于a的就输出,不能就返回−1代码
class LuckyXor { public: int construct(int); }; int LuckyXor::construct(int a) { int s[]={4,7,44,47,74,77}; int t; for(int i=0;i<6;i++) { t=a^s[i]; if(t<=100&&t>=1&&t>a) return a^s[i]; } return -1; }
相关文章推荐
- linux mmap 映射文件
- BAPC2014 B&&HUNNU11582:Button Bashing(BFS)
- linux常用命令
- 常用的操作系统镜像下载windows、linux 和MacOS
- Spark入门实战系列--2.Spark编译与部署(中)--Hadoop编译安装
- Centos7&Centos6 Root密码破解详解
- opencv获取图像的像素值
- Linux 强大的 curl 命令
- linux利器expect的使用
- 外网访问自己的tomcat
- linux内存管理之malloc
- Linux id 命令
- 重置CentOS 7的Root密码
- 海外网站加速及防攻击免费利器
- 海外网站加速及防攻击免费利器
- 双系统Window+Linux,卸载Linux
- 面试题:2014百度 linux中大内核锁(BKL--Big Kernel Lock)和自旋锁(FIFO Ticket Spinlock)
- centos6.5 设置ssh无密码登录
- linux用户及组基本概念
- linux随笔1:硬盘组成与分区的复习