首页 > ACM题库 > HDU-杭电 > HDU 4278-Faulty Odometer[解题报告]HOJ
2015
05-23

HDU 4278-Faulty Odometer[解题报告]HOJ

Faulty Odometer

问题描述 :

  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).

输入:

  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.

输出:

  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.

样例输入:

15
2005
250
1500
999999
0

样例输出:

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

其实就是八进制换10进制

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
	int n;
	char num[15];
	while(scanf("%s",num))
	{
		if(num[0]=='0')
			break;
		int l=strlen(num);
		int i;
		int sum=0;
		for(i=0;i<l;i++)
		{
			if(num[i]<'3')
				sum+=(int)(num[i]-'0')*pow(8.0,l*1.0-i-1);
			else if(num[i]<'8')
				sum+=(int)(num[i]-'0'-1)*pow(8.0,l*1.0-i-1);
			else
				sum+=(int)(num[i]-'0'-2)*pow(8.0,l*1.0-i-1);
		}
		printf("%s: %d\n",num,sum);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/juststeps/article/details/8688770