您的位置:首页 > 其它

Codeforces 535B Tavas and SaDDas 水题一枚

2015-04-15 03:24 197 查看
题目链接:Tavas and SaDDas

Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn't stop doing that. That's why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas' headphones and told him: "If you solve the following problem, I'll return it to you."

The problem is:

You are given a lucky number n. Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

If we sort all lucky numbers in increasing order, what's the 1-based index of n?

Tavas is not as smart as SaDDas, so he asked you to do him a favor and solve this problem so he can have his headphones back.

Input
The first and only line of input contains a lucky number n (1 ≤ n ≤ 10^9).

Output
Print the index of n among all lucky numbers.

题意描述:求出小于等于n并每一位上只包含4或7的数的个数。

算法分析:比赛的时候脑子大半夜短路了,刚开始一直在想枚举n的每个位上的数和4、7的关系,搞来搞去没出结果,后来才发现这样的数其实不多,每位上要求是4或7,一个数最多9位,也就2的9次方这么多呗,暴力求解然后判断这样的数和n的大小关系就OK了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=555;

int n,m;
int an[10][maxn];

int main()
{
while (scanf("%d",&n)!=EOF)
{
if (n<4) {printf("0\n");continue; }
if (n>=4 && n<7) {printf("1\n");continue; }
int cnt=2;
memset(an,0,sizeof(an));
an[1][1]=4 ;an[1][2]=7 ;
for (int i=2 ;i<10 ;i++)
{
int c=1;
int t=1,j=i-1;
while (j) {t*=10;j--; }
int flag=0;
for (int j=1 ;j<=pow(2,i-1) ;j++)
{
an[i][c]=4*t+an[i-1][j];
if (an[i][c]>n) {flag=1;break; }
cnt++;c++;
}
for (int j=1 ;j<=pow(2,i-1) ;j++)
{
an[i][c]=7*t+an[i-1][j];
if (an[i][c]>n) {flag=1;break; }
cnt++;c++;
}
if (flag) break;
}
printf("%d\n",cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: