Translate

Views

Thursday, July 7, 2022

Solution Katis - 4 thought

 Link Problem : https://open.kattis.com/problems/4thought

Submission 9117531

IDDATEPROBLEMSTATUSCPULANGTEST CASES
911753122:56:004 thought
Accepted
0.00 sC++
3/3

Files submitted

4thought.cpp

#include<bits/stdc++.h>
using namespace std;
unordered_map<int, string> a;
void f(int u, string s) {
if (u == 3) {
int n;
if ((s[0]=='+' || s[0]=='-') && (s[1] == '*' || s[1] == '/') && (s[2]=='*' || s[2]=='/')){
// 4 - 4 * 4 / 4
if (s[1] == '*') n= 4*4;
else n= 4/4;
if (s[2]=='*') n*=4;
else n/=4;
if (s[0]=='+') n= 4 + n;
else n= 4 - n;
}
else
if ((s[0]=='+' || s[0]=='-') && (s[1] == '*' || s[1] == '/') && (s[2]=='+' || s[2]=='-')){
// 4 - 4 * 4 + 4
if (s[1] == '*') n= 4*4;
else n = 4/4;
if (s[0]=='+') n= 4+n;
else n= 4-n;
if (s[2]=='-') n= n-4;
else n= n+4;
}
else
if ((s[0]=='+' || s[0]=='-') && (s[1] == '+' || s[1] == '-') && (s[2]=='*' || s[2]=='/')){
// 4 - 4 + 4 / 4
if (s[2] == '*') n= 4*4;
else n = 4/4;
if (s[0]=='+'){
if (s[1]=='+') n= 4+4+n;
else n=4+4-n;
}
else{
if (s[1]=='+') n= 4-4+n;
else n=4-4-n;
}
}
else
if ((s[0]=='*' || s[0]=='/') && (s[1] == '+' || s[1] == '-') && (s[2]=='*' || s[2]=='/')){
// 4 * 4 + 4 / 4
int a,b;
if (s[0] == '*') a= 4*4;
else a = 4/4;
if (s[2] == '*') b= 4*4;
else b = 4/4;
if (s[1] == '+') n=a+b;
else n=a-b;
}
else{
//4*4/4+4;
if (s[0]=='+') n=4+4;
if (s[0]=='-') n=4-4;
if (s[0]=='*') n=4*4;
if (s[0]=='/') n=4/4;
if (s[1]=='+') n=n+4;
if (s[1]=='-') n=n-4;
if (s[1]=='*') n=n*4;
if (s[1]=='/') n=n/4;
if (s[2]=='+') n=n+4;
if (s[2]=='-') n=n-4;
if (s[2]=='*') n=n*4;
if (s[2]=='/') n=n/4;
}
string ans="";
for(int i=0; s[i]; i++) ans=ans+"4 "+s[i]+" ";
ans+="4 = "+to_string(n);
a[n]=ans;
// cout<<n<<" "<<s<<"\n\n";
return;
}
f(u+1, s+"+");
f(u+1, s+"-");
f(u+1, s+"*");
f(u+1, s+"/");
}
int main() {
f(0,"");
int t;
cin >> t;
while(t--){
int n;
cin >> n;
if (a.count(n)==0) cout<<"no solution";
else cout<<a[n];
cout<<"\n";
}
}

No comments: