package com.Amazon.interview;

/**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2014年10月25日下午5:14:33
*
************* function description ***************
*
* Question:
*
* Given an array with positive integers and another integer for
* example{7 2 4} 9, you are required to generate an equation, by
* inserting operator add ("+") and minus ("-") among the array . The
* left side of equation are consist of the array and the right side of
* equation is the integer. here the result is 7-2+4=9
*
*
*
* Rules:
*
* Don't include any space in the generated equation. In case there is no
* way to create the equation, please output "Invalid". For example {1 1}
* 10, output is "Invalid"
*
* There is no operator "+" or "-" in front of the first number: Don't
* change the order of the numbers. For example: {7 2 4} 9. 7-2+4=9 is
* correct answer, 4-2+7=9 is wrong answer. There could be multiple
* input, meaning your function could be called multiple times. Do
* remember print a new line after the call.
*
* The length of the integer array is from 1 to 15( include 1 and 15). If
* the length is 1, for example the input {7} 7, the output is 7=7
*
* Sample Input and Output:
*
* Input:
*
* 1 2 3 4 10
*
* 1 2 3 4 5
*
* Output:
*
* 1+2+3+4=10
*
* Invalid
*
*
*
****************************************************
*/ public class GenerateEquation {
public static void createEqualAndPrint(int[] a, int n, int target) {
if (a == null || a.length == 0 || a.length != n || n == 0) {
System.out.println("Invalid"); return;
} if (n < 1 || n > 15) {
System.out.println("Invalid");
return;
} int i = n - 1;
int v = 1;
while (i > 1) {
v = (v << 1) + 1;
i--;
} int sum = 0;
String s = null;
while (v > 0) {
sum = a[0];
s = a[0] + "";
for (int j = n - 1; j > 0; j--) {
int c = v >> (j - 1);
if ((c & 1) == 1) {
sum += a[n - j];
s += "+" + a[n - j];
} else {
sum -= a[n - j];
s += "-" + a[n - j];
}
}
if (sum == target) {
System.out.println(s + "=" + target);
return;
} else {
v--;
}
} System.out.println("Invalid.");
} /**
* @param args
*/
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4 }; createEqualAndPrint(a, 4, 2);
} }

面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0的更多相关文章

  1. VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同

    1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...

  2. Maven实战错误笔记:使用mvn archetype:generate报错:Unable to add module to the current project as it is not of packaging type 'pom'

    在使用mvn archetype:generate生成Maven实战03:HelloWorld中的HelloWorld的项目骨架时报了这个错,从字面上分析是可能与pom.xml文件有关,然后我看了一下 ...

  3. Idiomatic Python手记一: average in FP way

    方法一: import operator def average(*args): return reduce(operator.add, args) / len(args) if args else ...

  4. Delphi泛型动态数组的扩展--转贴

    此文章转载于http://www.raysoftware.cn/?p=278&tdsourcetag=s_pcqq_aiomsg的博客 从Delphi支持泛型的第一天起就有了一种新的动态数组类 ...

  5. [Hive - Tutorial] Querying and Inserting Data 查询和插入数据

    Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...

  6. C/C++ 笔试题

    /////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...

  7. C/C++笔试题(很多)

    微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...

  8. python公司面试题集锦 python面试题大全

    问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...

  9. 【转】C/C++程序员应聘常见面试题深入剖析

    1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面试题的内涵.文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见­. 许多面试题看似简单,却需要深厚的基 ...

随机推荐

  1. Cocos2d-x 3.0- 在Visual Studio 2012中执行測试项目

    Cocos2d-x - 怎样在Win32执行cpp-tests 2014年4月30日 星期三 小雨 微凉 稍显疲惫 注:本篇文章来自Cocos2d-x官网,小巫仅仅是粗略翻译眼下最新版本号的,教大家怎 ...

  2. SilkTest天龙八部系列1-初始化和构造函数

    SilkTest没有提供专门的构造函数机制,但是在类对象生成的过程中,会先初始化在类中申明的变量.我们可以在初始化该变量的时, 调用某些函数完成对象初始化工作,看上去好像是调用了构造函数一样.不过要记 ...

  3. Linux curl命令参数详解--转载

    linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态. 一.Linux ...

  4. 打造强大的BaseModel(2):让Model实现自动映射,将字典转化成Model

    打造强大的BaseModel(1):让Model自我描述 这篇文章将讲述Model一项更高级也最常用的功能,让Model实现自动映射–将字典转化成Model(所有代码全由Swift实现) 将JSON转 ...

  5. Socket异步通信学习一

    最近在做一个频谱管理项目,负责通信模块,自己也是小白,重头学起,直至今天通信基本框架已经完成,把自己在学习中的心得与大家分享一下,做一个socket系列的博文,顺便加固一下自己对socket通信的认识 ...

  6. hdu2565java

    放大的X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  7. mongo数据管理java简易版

    mongo是搭建在局域网服务器上的,处理起来比较麻烦,于是自己写了个简单的处理工具. 如果有对java操作mongo不太了解的也可以在这里看下简单的示例. 只有增删改查的功能,而且只支持json格式的 ...

  8. 自定义标签 tld

    初学者可能在不借助IDE工具的情况下 编写自定义标签库 tld  程序可能遇到找不到class 的错误,下面讲解一下如何解决该问题 步骤一:新建一个自定义标签类 HelloWorldTag,该类放到s ...

  9. Optimal Logging

    by Anthony Vallone How long does it take to find the root cause of a failure in your system? Five mi ...

  10. Canvas 笔记(持续更新中)

    1.从线条开始 HTML <canvas id="canvas"></canvas> Javascript var canvas=document.getE ...