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.
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.
相关文章推荐
- ACM pku 1003 解题报告(练习输入输出)
- acm pku 2234 解题报告(取子问题)
- Pku acm 2075 Tangled in Cables数据结构题目解题报告(十一)最小生成树:prim算法&amp;amp;二叉查找树
- Pku acm 2492 A Bug's Life数据结构题目解题报告(十)---- 并查集的应用
- Pku acm 2406 Power Strings数据结构题目解题报告(十八)----kmp算法
- POJ 1004Financial Management解题报告——【PKU ACM】
- 有向图的汇点 -- 兼 ACM PKU POJ 2186 ( Popular Cows ) 解题报告
- Pku acm 1961 Period数据结构题目解题报告(十九)----kmp算法
- POJ 1005I Think I Need a Houseboat解题报告——【PKU ACM】
- Pku acm 1579 Function Run Fun 动态规划题目解题报告(二)
- Pku acm 1088 滑雪 动态规划题目解题报告(十五)
- Pku acm 1014 Dividing 动态规划题目解题报告(十七)
- Pku acm 1125 Stockbroker Grapevine 动态规划题目解题报告(十九)
- ACM PKU POJ 1740 解题报告 -- 男人八题之四
- Pku acm 1961 Period数据结构题目解题报告(十九)----kmp算法
- Pku acm 1274 The Perfect Stall 数据结构题目解题报告(十三)---- 匈牙利算法求二分图的最大匹配
- POJ 1006Biorhythms解题报告——生理周期——【PKU ACM】
- Pku acm 1179 Polygon 动态规划题目解题报告(二十)
- 约瑟夫 (Joseph) -- ACM PKU 1012 解题报告
- pku acm 2362 square 解题报告