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. ThinkPHP3.1快速入门(2)数据CURD

    上一篇中,我们了解了ThinkPHP的基础部分,以及如何创建一个控制器和模板,并知道了M方法的用法,本篇将会讲解下数据的CURD操作,探索下更多的数据操作. CURD CURD是一个数据库技术中的缩写 ...

  2. Linux 内核进程管理之进程ID 。图解

    http://www.cnblogs.com/hazir/tag/kernel/ Linux 内核进程管理之进程ID   Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数 ...

  3. 第一篇:GPU 编程技术的发展历程及现状

    前言 本文通过介绍 GPU 编程技术的发展历程,让大家初步地了解 GPU 编程,走进 GPU 编程的世界. 冯诺依曼计算机架构的瓶颈 曾经,几乎所有的处理器都是以冯诺依曼计算机架构为基础的.该系统架构 ...

  4. [转]使用Beaglebone Black的SPI

    分类: Beaglebone Black2013-11-24 18:21 678人阅读 评论(6) 收藏 举报 beaglebone blackbeagleboneSPIdevice tree   目 ...

  5. 在含有null值的复杂类的集合(Collection)中取最大值

    在日常编程中,经常遇到要在一组复杂类的集合(Collection)中做比较.取最大值或最小值. 举个最简单的例子,我们要在一个如下结构的集合中选取包含最大值的元素: public class Clas ...

  6. 一个卡片式的ViewPager,带你玩转ViewPager的PageTransformer属性!

    ViewPager的基本用法不必多说,这都很简单,我们可以在ViewPager中加载一个ImageView,也可以加载一个Fragment,这都是目前非常常见的用法.那么我今天说的是ViewPager ...

  7. redhat 6.4 双网卡绑定

    linux系统配置 1.redhat 6.4 双网卡绑定 1)#ethtool eth* //在服务器网口接网线至笔记本,确定各网口的配置文件: 2)切换目录 #cd /etc/sysconfig/n ...

  8. JS调用ashx文件传递中文参数取不到值的解决方案

    引自:http://www.cnblogs.com/yinpeng186/archive/2011/09/30/2196726.html

  9. MySQL之DML语句(insert update delete)

    DML主要针对数据库表对象的数据而言的,一般DML完成: 插入新数据 修改已添加的数据 删除不需要的数据 1.insert into插入语句 //主键自增可以不插入,所以用null代替 ); //指定 ...

  10. xml、xhtml、html、dhtml的区别

    1.XML 可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. 可扩展标记语言可以对文档和数据进行结构化处理,从而能够在部门.客户和供应商之间进行交换,实现动态内 ...