/*Faulty Odometer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1218 Accepted Submission(s): 841 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.. 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 0 Sample Output 15: 12 2005: 1028 250: 160 1500: 768 : Source 2012 ACM/ICPC Asia Regional Tianjin Online */ #include
#include
int main() { char s[10], c[10]; int a, i, b; while(scanf("%s", s) != EOF) { if(s[0] == '0') break; strcpy(c, s); a = strlen(s); b = 0; //求出真实数值 for( i =0; i < a; i++) { if(s[i] >= '4' && s[i] <= '7' ) s[i] -= 1; else if( s[i] == '9') s[i] -= 2; else ; b = b * 10 + s[i] - 48; } int sum = 0; //制转换 for( i = 0; b > 0 ; i++, b /= 10) { int j, k = 1; for( j = 0; j < i; j++) k *= 8; sum += (b % 10) * k; } printf("%s: %d\n", c, sum); } return 0; }
题意:汽车的里程表出了问题,不会计数3和8,现在给出里程表的数字,求其真实的里程。
思路:由于里程表少了2个数字,相当于说里程表是以满八个数来进位的,所以说里程表给出的相当于是一个八进制数,题目要求输出真实的里程数,即将一个八进制数转换为十进制数输出。(注意题目输入给的数字,必须先将其每个位上的数处理成8进制数,才能进行进制转换)
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221623.html原文链接:https://javaforall.net
