A – Faulty Odometer

A – Faulty Odometer                      FaultyOdometer TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others) TotalSubmission(s):2231    AcceptedSubmission(…

                                         Faulty Odometer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2231    Accepted Submission(s): 1537

 

Problem Description

  You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from the digit 2 to the digit 4 and from the digit 7 to the digit 9, always skipping over the digit 3 and 8. This defect shows up in all positions (the one’s, the ten’s, the hundred’s, etc.). For example, if the odometer displays 15229 and the car travels one mile, odometer reading changes to 15240 (instead of 15230).

 

 

Input

  Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 3 and 8.

 

 

Output

  Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.

 

Sample Input

15
2005
250
1500
999999
0

Sample Output

15: 12
2005: 1028
250: 160
1500: 768
999999: 262143

 

Source

2012 ACM/ICPC Asia Regional Tianjin Online

 

 

 

#include<stdio.h>
#include<string.h>
int main()
{
    int n,len,num,ba,i;
    char s[10];
    while(gets(s)!=NULL)
    {
        if(strcmp(s,"0")==0)
            break;
        len=strlen(s);
        num=0;
        //字符串转换成数字
        for(i=0; i<len; i++)
        {
            num=num*10+s[i]-'0';
        }
        //转换成八进制
        for(i=0; i<len; i++)
        {
            if(s[i]>'8')//if(s[i]>=9)
                s[i]-=2;
            else if(s[i]>'3')//if(s[i]>=4)
                s[i]-=1;
        }
        ba=0;
        for(i=0;i<len;i++)
        {
            ba=ba*8+s[i]-'0';
        }
        printf("%d: %d\n",num,ba);
    }
}

此题是将八进制转换成十进制,也可以理解为统计从1~n除去含有‘3’‘8’数字外所有的数字的个数。

今天的文章A – Faulty Odometer分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7704.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注