演示一个 5 ? 5?5?5?=5
import java.util.*; public class Text1{ public static void main(String[]args){ int j ;//循环变量 int []type=new int[5];//类加运算符的样式 int sign;//保存运算的正负号 int result;//结果 int count=0;//数量 int []num=new int[6];//保存数组 float left , right;保存中间值,left保存+-运算的值right保存*/运算值 char []oper=new char[]{' ','+','-','*','/'}; System.out.println("请输入五个数"); for(j=1 ;j<=5 ;j++ ){ num[j]=new Scanner(System.in).nextInt(); } System.out.println("请输出结果"); result = new Scanner(System.in).nextInt(); for(type[1]=1 ;type[1]<=4 ;type[1]++ ){ if((type[1]<4) || (num[2]!=0)){ for( type[2]=1 ;type[2]<=4 ;type[2]++ ){ if((type[2]<4) || (num[3]!=0)){ for( type[3]=1 ;type[3]<=4 ;type[3]++ ){ if((type[3]<4) || (num[4]!=0)){ for( type[4]=1 ;type[4]<=4 ;type[4]++ ){ if((type[4]<4) || (num[5]!=0)){ left=0; right = num[1]; sign = 1; for(j=1 ;j<=4 ;j++ ){ //System.out.print(oper[type[4]]+" "); switch(oper[type[j]]){ case '+':{ left = left+sign*right; sign=1; right = num[j+1]; break; } case '-':{ left = left+sign*right; sign=-1; right = num[j+1]; break; } case '*':{ right =right*num[j+1]; break; } case '/':{ right = right/num[j+1]; break; } } } if((left+sign*right)==result){ count++; System.out.print(count+":"); for(j=1 ; j<=4;j++ ){ System.out.print(num[j]+""+oper[type[j]]); } System.out.println(num[5]+"="+result); } } } } } } } } } } }
时间: 2024-12-16 10:31:23