HDU 4278 - Faulty Odometer

9 篇文章 0 订阅

Problem Description

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

 

 

Input

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

 

 

Output

  Each line of input will produce exactly one line ofoutput, which will contain: the odometer reading from the input, a colon, oneblank 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

 

思路:

1、 按照每一位多走的路程数打表

2、 把输入看成八进制数

 

我用的方法1,比赛时没有想到方法2


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int f[12][12];
int a[12];

int main()
{
	int i, j;
	memset(f, 0, sizeof(f));
	for (j = 4; j <= 7; j++)
		f[0][j] = -1;
	f[0][9] = -2;

	f[1][1] = -2;
	int temp = -1;
	for (i = 2; i<10; i++)
	{
		temp *= 10;
		f[i][1] = f[i - 1][1] * 8 + temp * 2;
	}

	temp = -1;
	for (i = 1; i<10; i++)
	{
		f[i][2] = 2 * f[i][1];
		for (j = 4; j <= 7; j++)
			f[i][j] = (j - 1)*f[i][1] + temp * 10;
		f[i][9] = 7 * f[i][1] + temp * 20;

		temp *= 10;
	}

	int n;
	while (scanf("%d", &n) != EOF && n)
	{
		int d = 0, sum = n, ori = n;
		while (n != 0)
		{
			a[d++] = n % 10;
			n /= 10;
		}

		for (i = 0; i<d; i++)

		{
			sum += f[i][a[i]];
		}

		printf("%d: %d\n", ori, sum);
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值