# Count Up Down（上下计数） 原

## 英文描述

### Part 1

Write a program that counts in sequential order when given a start and end value - without using any iterative programing loops, i.e. while, for, do, for-each, etc.

You can assume that both the start and end values will always be positive and that the start value will always be less then the end value. There should only be one method with the following signature:

void countUp(int start, int end) { // All code exercise code should go here }

Here is example output with start=0 and end=5:

[ 0,1,2,3,4,5]

### Part 2

Continuing with part 1 change the output of the test, so that it now prints out in sequential order to the end value (only once), but then also counts down to the start value.

Again, using no iterative loops, and assuming that both the start and end values will always be positive and that start value will always be less then the end value. There should only be one method with the following signature:

void countUpAndDown(int start, int end) { // All code exercise code should go here }

Here is example output with start=0 and end=5:

[0,1,2,3,4,5,4,3,2,1,0]

## 源代码

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/test/java/com/ossez/codebank/interview/tests/KayakTest.java

```package com.ossez.codebank.interview;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down
*
* @author YuCheng
*
*/
public class KayakCountUpDown {
private final static Logger logger = LoggerFactory.getLogger(KayakCountUpDown.class);

static int minNumber = 0;
static int maxNumber = 0;
int tmpN = 0;
List<Integer> retList = new ArrayList<Integer>();

/**
*
* @param start
* @param end
* @return
*/
public List<Integer> countUp(int start, int end) {
logger.debug("BEGIN");
maxNumber = end;
tmpN = start;
moveUp(0);
return retList;

}

/**
*
* @param start
* @param end
* @return
*/
public List<Integer> countUpDown(int start, int end) {
logger.debug("BEGIN");
minNumber = start;
maxNumber = end;
tmpN = start;

moveUp(0);

moveDown(1);
return retList;

}

/**
*
* @param n
*/
private void moveUp(int n) {
tmpN++;
if (tmpN != maxNumber) {
moveUp(tmpN + 1);
}

}

/**
*
* @param n
*/
private void moveDown(int n) {
tmpN = (maxNumber - n);

if (tmpN != minNumber) {
moveDown(n + 1);
}
}

}```

## 测试结果

`2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - TEST Count Up and Down ` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [2 -> 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [2, 3, 4, 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [2, 3, 4, 5, 4, 3, 2]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [0 -> 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [0, 1, 2, 3, 4, 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [-1 -> 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [-1, 0, 1, 2, 3, 4, 5]` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN` `2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [-1, 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1]`

https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down

571 篇文章30 人订阅

0 条评论

## 相关文章

### 5G时代来了，你要换手机就吗？

12月8日中移动董事长尚冰在全球合作伙伴大会上透露，将会增加5G设备及网络建设的资金投放，计划于2019年开始5G预商用，并于明年上半年推出5G智能手机和首批中...

9520

### 企业遭到DDoS攻击可以通过哪些手段进行防御？

DDoS攻击是现在最常见的一种网络攻击方式，主要通过大规模流量攻击目标服务器，导致服务器性能耗尽而崩溃。随着5G时代的到来，DDoS攻击的数量和规模都越来越大。...

25100

20920

36120

37720

16630

### 胖子哥的大数据之路（12）-三张图告诉你大数据安全方案设计

最近一直很忙，在做一个全国性项目的IT架构，所以一直没有更新，好在算是告一段落，继续努力吧。项目沟通中过程客户反复在强调，大数据的安全性，言下之意，用了大数...

13930

19220

46520

10940