hdu 4278 Faulty Odometer YY题目

http://acm.hdu.edu.cn/showproblem.php?pid=4278

题意:

一个特殊的汽车行程计数器,当每个位出现3或者8时直接跳到下一位;

0 1 2 4 5 6 7 9 

10 11 12 14 15 16 17 19 

20 21 22 24 25 26 27 29

......

思路:

YY发现其实可以映射到8进制数,(0 - 9)表示10进制数,将(0 1 2 4 5 6 7 9 )映射到(0 - 8)其实就是8进制的表示。

View Code
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <set>

#include <map>

#include <string>



#define CL(a,num) memset((a),(num),sizeof(a))

#define iabs(x)  ((x) > 0 ? (x) : -(x))

#define Min(a,b) (a) > (b)? (b):(a)

#define Max(a,b) (a) > (b)? (a):(b)



#define ll long long

#define inf 0x7f7f7f7f

#define MOD 100000007

#define lc l,m,rt<<1

#define rc m + 1,r,rt<<1|1

#define pi acos(-1.0)

#define test puts("<------------------->")

#define maxn 100007

#define M 100007

#define N 50007

using namespace std;

//freopen("din.txt","r",stdin);



int find(char ch){

    int a = ch - '0';

    if (a >= 3 && a < 8) a--;

    else if (a >= 8) a -= 2;

    return a;

}

int Pow(int x){

    int sum = 1,i;

    for (i = 1; i <= x; ++i) sum *= 8;

    return sum;

}

int solve(char *s){

    int i,j;

    int len = strlen(s);

    int num = 0;

    for (i = len - 1,j = 0; i >= 0 && j < len; --i,++j){

        int x = find(s[i]);// 关键是将0-9映射

        num += x*Pow(j);//转化成8进制 

    }

    return num;

}

int main(){

    //freopen("din.txt","r",stdin);

    char str[12];

    while (scanf("%s",str)){

        if (str[0] == '0') break;

        printf("%s: %d\n",str,solve(str));

    }

    return 0;

}

 

 

你可能感兴趣的:(dom)