Description
A car has an odometer, which can show an integer for the number of kilometres travelled by the car. However, there is a problem with this odometer: it always changes from 3 to 5, and skips the number 4, the number of all bits (bits, 10, hundred, etc.) on the odometer. For example, if the odometer shows 15339 and the car passes 1 km, the odometer shows 15350.
Input
Enter an integer num, indicating the value of the odometer display, not exceeding 9 bits in length, and must not contain an integer 4.
Output
Outputs an integer that represents the mileage actually traveled.
Sample Input MaxSample Output117
This problem seems simple, if you use a regular for one plus, and then skip 4, it will time out.
So you can change the idea, remember that the nine is only contains 0~8 nine numbers, this problem can be seen as No 4 of this number of nine decimal number ...
Bitwise *9, with 4 as the bounds of the accumulation
#include <stdio.h>#include<stdlib.h>intMainvoid){ intnum,sum=0; intI=0, J,k; intm; intstr[Ten]; scanf ("%d", &num); while(num>0) {Str[i]=num%Ten; Num=num/Ten; I++; } for(j=0; j<i;j++){m=1; for(k=1; k<=j;k++) {m*=9; } if(str[j]<=3) Sum+=str[j]*m; if(str[j]>=5)//separated by 4sum+= (str[j]-1)*m; } printf ("%d\n", sum); return 0;}
Zzuli OJ 1145 Problematic odometer 2