4_7计数

题目:

#include<cstdlib>
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;

给你一个幸运数字n。幸运数字是一个十进制表示只包括幸运数字4和7的正整数。比如,47,744和4是幸运数字,5,17和467则不是。
如果我们把所有的幸运数字按照递增排序,那么n是第几个?(以1为底)

Input

第一行有一个整数T(T≤100),代表测试数据组数。
对于每组数据,一个正整数n(1<=n<=100000000,保证n为幸运数字)

Output

输出n在所有幸运数字中的序号(以1为底)

Sample Input

3
4
7
77

Sample Output

1
2
6

/*

int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        int n;
        scanf("%d",&n);
        int ans=0,base=1;
        while(n)
        {
            if(n%10==7)
                ans+=2*base;
            else
                ans+=base;
            base<<=1;
            n/=10;
        }
        printf("%d\n",ans);
    }    
    return 0;
}


4            1*1
7           2*1
44            1*2 + 1*1
47            1*2 + 2*1
74            2*2 + 1*1
77            2*2 + 2*1

*/

int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        int n,ans=0,base=1;
        scanf("%d",&n);
        while(n)
        {
            if(n%10 == 7)
                ans+=base;
            n/=10;
            base<<=1;
        }
        ans+=base;
        printf("%d\n",ans-1);
    }   
    return 0;
}

/*

我们发现4是第一个,7是第二个,44是第三个,47是第四个

不如将4看成0,7看成1,然后就得到二进制数了

4是0

7是1

可是44也变成0了,这就不太好了...于是想到在所有辛运数字前面加上7(也就是所有二进制前加一个1)这样我们就可以再列出一个表了:

4是10

7是11

44是100

47是101

74是110....哇真的是二进制诶.....所以直接将n像我那样拆分,就可以得到一个二进制串,再将二进制串变成十进制后减一即可。

*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值