Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23918
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 196
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-10 09:17
文章分类
文章存档

2014年(31)

我的朋友

分类: Java

2014-07-29 14:05:27

package liu.jyc;
public class Question {
    public static void main(String[] args) {
        fun();
    public static void fun(){
        String[] a = new String[17]; ///
        for(int i = 1;i<=9;i++){
            a[2*(i-1)]=i+"";
        for(int j = 0;j             int p = j;
            for(int i = 0;i<8;i++){  //   一共有8个空是填运算符的
                int k = p%3;//取末位 (说的是3进制的末位)
                p/=3; //   精妙之处!!  去掉3进制最后一位,当下次循环就p%3就取到的是倒数第2位的数字了  我想好久才明白的
                switch(k){
                case 0:
                    a[2*i+1]=" +";
                    break;
                case 1:
                    a[2*i+1]=" -";
                    break;
                case 2:
                    a[2*i+1]="o";//  代表空
                    break;
            //  至此,将数组转换成字符串
            String test = "";
            for(int i = 0;i                 if(!a[i].equals("o")){ ///  是不是空 就拼成字符串
                    test= test+a[i];    
            if(check(test)){
                System.out.println(test.replaceAll(" ", "")+"=110");
     * 判断字符串  test  是否满足题干要求   类似于   123 -4 -56 +789
     * @param a 
     * @return    
    private static boolean check(String test) {
        //  test  是这样的字符串  123 -4 -56 +789
        int sum = 0;
            String[] numbers = test.split(" ");
            for(int j = 0;j                 if(numbers[j].startsWith("+")){
                    numbers[j]= numbers[j].substring(1);
        if(sum==110){
            return true;
        return false;
阅读(111) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~