题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4278
解题思路:
8进制转10进制,如果是9,实际上是7.如果是4-7实际上是3-6。
AC代码(进制转换):
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int res[15],n;
while(scanf("%d",&n),n){
int tmp = n,cnt = 0,k;
while(tmp){
k = tmp%10;
res[cnt] = k<3?k:(k<8?k-1:k-2);
cnt++;
tmp /= 10;
}
tmp = 0;
while(cnt--){
tmp = tmp*8+res[cnt];
}
printf("%d: %d\n",n,tmp);
}
return 0;
}
AC代码(找规律):
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
string str;
int main(){
while(cin>>str){
if(str == "0")
break;
int l = str.size();
ll n = 0,sum = 0,p = pow(10,l-1),tmp = 0;
for(int i = 0; i < l; i++){
if(str[i] >= '8')
sum += ((n-tmp)*2+2)*p;
else if(str[i] >= '3')
sum += ((n-tmp)*2+1)*p;
else
sum += (n-tmp)*2*p;
//cout<<sum<<endl;
n = n*10 + str[i]-'0';
tmp = sum/p;
//cout<<n<<endl;
//cout<<tmp<<endl;
p /= 10;
}
printf("%lld: %lld\n",n,n-sum);
}
return 0;
}