Codeforces Round #299 (Div. 2)B. Tavas and SaDDas点击进入题目页面
B. Tavas and SaDDas
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ≤ 109).
Output
Print the index of n among all lucky numbers.
Sample test(s)
input
4
output
1
input
7
output
2
input
77
output
6
题意:给你一个数,输出比它小的幸运数的个数,幸运数仅由4和7组成。
题解:简单的排列组合问题。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int i,j,t,k,sum=0,arr[10],m=0;
char a[10];
memset(a,0,sizeof(char));
scanf("%s",a);
i=strlen(a);
k=i;
for(j=0,i--;i>=0;j++,i--)
{arr[j]=a[i]-'0';if(arr[j]<4) m=1;}
for(j=0;j<k;j++){
if(m==1) printf("%d\n",0);
else if(arr[j]>=7) sum+=j==0?2:2<<j;
else if(arr[j]>=4) sum+=j==0?1:2<<j-1;
}
if(m==0) printf("%d\n",sum);
getchar();getchar();
return 0;
}