您的位置:首页 > 其它

ACM pku 2719 解题报告(都是输入输出惹的祸)

2006-01-22 17:40 411 查看
Faulty Odometer
Time Limit:1000MS Memory Limit:65536K
Total Submit:619 Accepted:381
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 3 to the digit 5, always skipping over the digit 4. This defect shows up in all positions (the one's, the ten's, the hundred's, etc.). For example, if the odometer displays 15339 and the car travels one mile, odometer reading changes to 15350 (instead of 15340).

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

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

13
15
2003
2005
239
250
1399
1500
999999
0

Sample Output

13: 12
15: 13
2003: 1461
2005: 1462
239: 197
250: 198
1399: 1052
1500: 1053
999999: 531440

Source
Rocky Mountain 2005

这一题我用一个函数d,求出10的幂含有的4的个数,用GetCount4()求出任意整数含4的个数,然后用INPUT的数一减便是原来十进制的数。

因为我用DOUBLE,但是输入输出都用%f,导致WA了无数次。天,都是基础不好惹的祸。

代码如下:

/*final3*/
#include <stdio.h>
#include <math.h>
long d(long n)
{
if(n==10)return 1;
return d(n/10)*9+n/10;
}

/* get the last digit */
long GetLastDigit ( long x )
{
return (long)( (x/10.0+0.0009-x/10) *10 );
}

long GetCount4(double x)
{
double temp;
long count4 = 0 ;
long wei = 0,last;

if( ( x/10.0 - floor(x/10) ) > 0.4 ) {
count4++;
}

x = floor(x/10);
wei = 10;
while( x >= 1 ){
last = GetLastDigit(x);

if( last != 0 ){

if( last > 4){
count4 = (last-1) * d(wei) + wei + count4;

}/*if inside*/
else{
count4 = last * d(wei) + count4;

}
}/*if*/
x = floor(x/10);
wei *=10;
}/*while*/
return (count4);

}/*GetCount4*/

main()
{
double myx[100000];
int i = 0;
scanf("%lf",&myx[0]);

while(myx[i]!=0)
{
i++;
scanf("%lf",&myx[i]);
}
i = 0;
while(myx[i]!=0){
printf("%ld: %ld/n",(long)myx[i],((long)(myx[i]))-GetCount4(myx[i]) );
i++;
}
}/*main*/

这题也可以用九进制来做。小于4的直接用X*9^N,大于4的用(X-1)*9^N.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: