Skip to content

Instantly share code, notes, and snippets.

@bilinin
Created October 26, 2015 17:08
Show Gist options
  • Save bilinin/aef83c2c1a326f9b86c0 to your computer and use it in GitHub Desktop.
Save bilinin/aef83c2c1a326f9b86c0 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
string str="15?+15?=30?",
str_cpu=str;
int flag=0;
int question='?'-'0';
int a=0,b=0,c=0,d=0;
int posPlus=-1,
posEq=-1;
// Определение позиций знаков '=' и '+'
for (int i=0;i<str.size();i++){
switch(str[i]){
case '+': posPlus=i; break;
case '=': posEq=i; break;
default: break;
}
}
for (int pos=0;pos<posPlus;pos++){
a=str[posPlus-pos-1]-'0';
b=str[posEq-pos-1]-'0';
c=str[str.size()-pos-1]-'0';
// 1 неизвестная
if((a==question)&&(b!=question)&&(c!=question)){
a=c-b-d;
if(a<0) a=a+10;
}
else if((a!=question)&&(b==question)&&(c!=question)){
b=c-a-d;
if(b<0) b=b+10;
}
else if((a!=question)&&(b!=question)&&(c==question)){
c=(a+b+d)%10;
}
//2 неизвестных
else if((a==question)&&(b==question)&&(c!=question)){
if(flag==0){
a=c;
b=0;
}
else if(flag==1){
a=c-5;
b=5;
}
}
else if((a!=question)&&(b==question)&&(c==question)){
if(flag==0){
c=a;
b=0;
}
else if(flag==1){
b=10-a;
c=(a+b+d)%10;
}
}
else if((a==question)&&(b!=question)&&(c==question)){
if(flag==0){
c=b;
a=0;
}
else if(flag==1){
a=10-b;
c=(a+b+d)%10;
}
}
//3 неизвестных
else if((a==question)&&(b==question)&&(c==question)){
if(flag==0){
a=0;
b=0;
c=0;
}
else if(flag==1){
a=5-d;
b=5;
c=0;
}
}
cout<<"pos="<<pos<<" a="<<a<<" b="<<b<<" c="<<c<<" d="<<d;
if(c==(a+b+d)%10) {
cout << " flag=" << flag << " good " << endl;
str_cpu[posPlus-pos-1]=(char)a+'0';
str_cpu[posEq-pos-1]=(char)b+'0';
str_cpu[str.size()-pos-1]=(char)c+'0';
//if(flag) flag=0;
}
else{
cout<<" flag="<<flag<<" impossible step"<<endl;
if(!flag){
cout<<"change flag"<<endl;
flag=1;
pos=pos-2;
}
else{
cout<<"impossible";
return 0;
}
}
d=(a+b+d)/10;
}
cout<<str_cpu;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment