码迷,mamicode.com
首页 > 其他好文 > 详细

TC SRM 665 DIV2 A LuckyXor 暴力

时间:2015-08-11 23:19:08      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
};

 

TC SRM 665 DIV2 A LuckyXor 暴力

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4722359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!