codeforces round 299 div2 题解

A. Tavas and Nafas
将0到99的数字变成英文的表示。处理好特殊情况就行了。

#include <cstdio>
#include <cstring>
#include <cstdlib>

#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

char str[100];

int main(int argc, char *argv[])
{
    int n;
    while (scanf("%d", &n) != EOF) {
        int x = n / 10;
        int y = n % 10;
        if (x == 0) {
        } else if (x == 1) {
            switch (n) {
                case 10: printf("ten\n"); break;
                case 11: printf("eleven\n"); break;
                case 12: printf("twelve\n"); break;
                case 13: printf("thirteen\n"); break;
                case 14: printf("fourteen\n"); break;
                case 15: printf("fifteen\n"); break;
                case 16: printf("sixteen\n"); break;
                case 17: printf("seventeen\n"); break;
                case 18: printf("eighteen\n"); break;
                case 19: printf("nineteen\n"); break;
            }
            continue;
        } else {
            switch (x) {
                case 2: printf("twenty"); break;
                case 3: printf("thirty"); break;
                case 4: printf("forty"); break;
                case 5: printf("fifty"); break;
                case 6: printf("sixty"); break;
                case 7: printf("seventy"); break;
                case 8: printf("eighty"); break;
                case 9: printf("ninety"); break;
            }
            if (y != 0) {
                printf("-");
            }
        }
        str[0] = '\0';
        switch (y) {
            //case 0: strcpy(str, "zero"); break;
            case 1: strcpy(str, "one"); break;
            case 2: strcpy(str, "two"); break;
            case 3: strcpy(str, "three"); break;
            case 4: strcpy(str, "four"); break;
            case 5: strcpy(str, "five"); break;
            case 6: strcpy(str, "six"); break;
            case 7: strcpy(str, "seven"); break;
            case 8: strcpy(str, "eight"); break;
            case 9: strcpy(str, "nine"); break;
        }
        if (n == 0) {
            printf("zero");
        }
        printf("%s\n", str);
    }
    return 0;
}

B. Tavas and SaDDas
定义仅有数字4和数字7组成的数是幸运数字,给定一个仅有数字4和数字7组成的数,问这个数在所有幸运数字中按升序排列是第几个幸运数。我们写下几个幸运数字,就会发现一位数的有2个,两位数的有4个,三位数的有八个,…,一次类推。我们就可以找到k位数的第一个数是第几位,为了找到特定的数,我们发现,幸运数仅有两种数字组成,和二进制有着异曲同工之妙,我们把4看成二进制的0,7看成二进制的1,我们发现这个二进制表示的十进制就是这个幸运数在k位数中的偏移量。

/*************************************************************************
    > File Name: b.cpp
    > Author: gwq
    > Mail: gwq5210@qq.com 
    > Created Time: 2015年04月28日 星期二 15时44分38秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

ll count(ll n)
{
    ll res = 0;
    while (n) {
        ++res;
        n /= 10;
    }
    return res;
}

ll getans(ll n)
{
    ll m = 1;
    ll ans = 0;
    while (n) {
        int x = n % 10;
        n /= 10;
        if (x == 7) {
            ans += m;
        }
        //printf("%d %d %d\n", n, ans, m);
        m *= 2;
    }
    return ans;
}

int main(int argc, char *argv[])
{
    int n;
    while (scanf("%d", &n) != EOF) {

        ll cnt = count(n);
        ll ans = (1 << cnt) - 1 + getans(n);
        printf("%lld\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值