📄 dianpai.cpp
字号:
// 3 8 6 4 9 5 7 4 8 3 5 2 8 5 4 1 1 1 1 1 6 7 2 7
#include<iostream>
using namespace std;
int n=0;
double operation(double a,double b,char ch);
void process(double num[]);
void result(double num[]);
int main()
{
double num[4];
int i;
cout<<"enter 4 numbers:\n";
for(i=0;i<4;i++)
cin>>num[i];
result(num);
if(!n)
cout<<"error!"<<endl;
return 0;
}
double operation(double a,double b,char ch)
{
switch(ch)
{
case'+': return (a+b);
case'-': return (a-b);
case'*': return (a*b);
case'/': return (a/b);
default: return 0;
}
}
void process(double num[])
{
char oper[4]={'+','-','*','/'};
double sum=0;
int i,j,k,x;
for(int t=0;t<2;t++)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
if(oper[t]=='-') x=-num[0];
else x=num[0];
sum=operation(x,num[1],oper[i]);
sum=operation(sum,num[2],oper[j]);
sum=operation(sum,num[3],oper[k]);
if(sum==24&&oper[t]=='-')
{
n++;
if((oper[i]=='+'||oper[i]=='-')&&(oper[j]=='/'||oper[j]=='*'))
{
cout<<'('<<'-'<<num[0]<<oper[i]
<<num[1]<<')'<<oper[j]
<<num[2]<<oper[k]
<<num[3]<<"=24"<<endl;
}
else if((oper[j]=='+'||oper[j]=='-')&&(oper[k]=='/'||oper[k]=='*'))
{
cout<<'('<<'-'<<num[0]<<oper[i]
<<num[1]<<oper[j]
<<num[2]<<')'<<oper[k]
<<num[3]<<"=24"<<endl;
}
else
cout<<'-'<<num[0]<<oper[i]
<<num[1]<<oper[j]
<<num[2]<<oper[k]
<<num[3]<<"=24"<<endl;
}
else if(sum==24)
{
n++;
if((oper[i]=='+'||oper[i]=='-')&&(oper[j]=='/'||oper[j]=='*'))
{
cout<<'('<<num[0]<<oper[i]
<<num[1]<<')'<<oper[j]
<<num[2]<<oper[k]
<<num[3]<<"=24"<<endl;
}
else if((oper[j]=='+'||oper[j]=='-')&&(oper[k]=='/'||oper[k]=='*'))
{
cout<<'('<<num[0]<<oper[i]
<<num[1]<<oper[j]
<<num[2]<<')'<<oper[k]
<<num[3]<<"=24"<<endl;
}
else
cout<<num[0]<<oper[i]
<<num[1]<<oper[j]
<<num[2]<<oper[k]
<<num[3]<<"=24"<<endl;
}
}
}
}
}
}
void result(double num[])
{
double a[4];
int i,j,k,t;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j==i)
continue;
for(k=0;k<4;k++)
{
if(k==j||k==i)
continue;
for(t=0;t<4;t++)
{
if(t==i||t==j||t==k)
continue;
a[0]=num[i];
a[1]=num[j];
a[2]=num[k];
a[3]=num[t];
process(a);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -