您的位置:首页 > 其它

codeforces round 299 div2 题解

2015-04-28 16:22 337 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: