标签:
LuckyXor
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
TC
Description
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.
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
Input
a is between 1 and 100, inclusive.
Output
int construct(int a)
Sample Input
4
Sample Output
40
HINT
题意
让你找到一个b,使得a^b是幸运数
幸运数指的是只含有4或者7的数
题解:
数据范围只有100,所以直接暴力就好了
代码:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; class LuckyXor{ public: int check(int x) { while(x) { int X=x%10; if(X==4||X==7) x/=10; else return 0; } return 1; } int construct(int a){ int ans=-1; for(int i=a+1;i<=100;i++) { int X=(a^i); if(check(X)==1) { ans=i; break; } } return ans; } };
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/4722359.html