当前位置: 代码迷 >> 综合 >> hdu 6828-Little Rabbit‘s Equation
  详细解决方案

hdu 6828-Little Rabbit‘s Equation

热度:82   发布时间:2024-02-07 14:02:49.0

hdu 6828-Little Rabbit’s Equation

又忘开 long long了,!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;ll toTen(string a, int bit){int i,b=1,sum=0,length=a.length();for(i=length-1;i>=0;i--){if(a[i]>='A')sum+=(a[i]-'A'+10)*b,b*=bit;else sum+=(a[i]-'0')*b,b*=bit;}return sum;
}int main(){int d,e,flag=0;string s;while(cin>>s){e=2,flag=0;string a,b,c;for(int i=0,k=0; i<s.length(); i++){if(s[i]>='0'&&s[i]<='9'){int x=s[i]-'0'+1;if(x>e)e=x;}if(s[i]>='A'&&s[i]<='F'){int x=s[i]-'A'+11;if(x>e)e=x;}if(s[i]=='*'||s[i]=='/'||s[i]=='+'||s[i]=='-'){string s1(s,0,i);k=i,a=s1;if(s[i]=='*')d=1;if(s[i]=='/')d=2;if(s[i]=='+')d=3;if(s[i]=='-')d=4;}if(s[i]=='='){string s2(s,k+1,i-k-1),s3(s,i+1);k=i,b=s2,c=s3;}}//cout<<a<<" "<<toTen(a,e)<<" "<<b<<" "<<toTen(b,e)<<" "<<toTen(c,e)<<" "<<c<<" "<<e<<" "<<d<<endl;for(; e<=16; e++){//cout<<toTen(a,e)<<" "<<e<<endl;if(d==1)if(toTen(a,e)*toTen(b,e)==toTen(c,e)){cout<<e<<endl,flag=1;break;}if(d==2)if(toTen(a,e)*1.0/toTen(b,e)==1.0*toTen(c,e)){cout<<e<<endl,flag=1;break;}if(d==3)if(toTen(a,e)+toTen(b,e)==toTen(c,e)){cout<<e<<endl,flag=1;break;}if(d==4)if(toTen(a,e)-toTen(b,e)==toTen(c,e)){cout<<e<<endl,flag=1;break;}}if(!flag)cout<<-1<<endl;}return 0;
}
  相关解决方案