用枚举法填运算符

演示一个 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);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
 }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值