標籤:hdu4278 進位轉換 c++ faulty odometer
題目大意:里程錶會錯過0 1 2 3 4 5 6 7 8 9中的 3 和 8 兩個數字 也就是 正常的里程錶顯示0 1 2 3 4 5 6 7 8 9
壞掉的里程錶只會顯示 0 1 2 4 5 6 7 9
其實可以看做:0 1 2 3 4 5 6 7 即壞掉的里程錶顯示的是8進位的數字,只需先將顯示的數字換成8進位數,在把8進位數字換位10進位數字就是答案。
#include <iostream>#include <map>#include <sstream>#include <cstring>#include <cstdio>#include <cmath>#include <string>using namespace std;map<char ,char> mp;string str;long long ans;long long trans(long long a){ int y,b=0,i=0; while(a>=1){ y=a%10; b+=y*pow(8.0,i); i++; a=a/10; } return b;}long long fuck(){ string tps; int len=str.length(); long long tmp,tp; for(int i=0;i<len;i++){ tps.push_back(mp[str[i]]); } stringstream ss(tps); ss>>tmp; tp=trans(tmp); return tp;}int main(){ mp['0']='0'; mp['1']='1'; mp['2']='2'; mp['4']='3'; mp['5']='4'; mp['6']='5'; mp['7']='6'; mp['9']='7'; while(cin>>str){ if(str=="0") return 0; cout<<str<<": "; ans=fuck(); cout<<ans<<endl; }}
HDU4278 Faulty Odometer(進位轉化問題)