下一个斐波拉契数
This commit is contained in:
parent
04978083ec
commit
f5a896f1ae
|
@ -0,0 +1,68 @@
|
|||
package com.ossez.codebank.interview;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* https://www.cwiki.us/display/ITCLASSIFICATION/Next+Fibonacci+Number
|
||||
*
|
||||
* @author YuCheng
|
||||
*
|
||||
*/
|
||||
public class ManNextFibonacciNumber {
|
||||
private final static Logger logger = LoggerFactory.getLogger(ManNextFibonacciNumber.class);
|
||||
|
||||
public static void main(String[] args) throws java.lang.Exception {
|
||||
int fArray[] = new int[60];
|
||||
|
||||
for (int i = 0; i < 60; i++) {
|
||||
fArray[i] = getFib(i);
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
String input = br.readLine();
|
||||
// System.out.println(fib(Integer.valueOf(input)));
|
||||
|
||||
for (int i = 0; i < Integer.valueOf(input); i++) {
|
||||
Integer inputInt = Integer.valueOf(br.readLine());
|
||||
// System.out.println(inputInt);
|
||||
for (int j = 0; j < fArray.length; j++) {
|
||||
if (fArray[j] > inputInt) {
|
||||
// System.out.println(fArray[j]);
|
||||
logger.debug("{} Next Fibonacci [{}]", inputInt, fArray[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Fibonacci Number
|
||||
*
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
private static int getFib(int n) {
|
||||
if (n < 0) {
|
||||
return -1;
|
||||
} else if (n == 0) {
|
||||
return 0;
|
||||
} else if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
} else {
|
||||
int[] fibAry = new int[n + 1];
|
||||
fibAry[0] = 0;
|
||||
fibAry[1] = fibAry[2] = 1;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
fibAry[i] = fibAry[i - 1] + fibAry[i - 2];
|
||||
}
|
||||
return fibAry[n];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE RelativeLayout>
|
||||
<Configuration status="WARN">
|
||||
|
||||
<Properties>
|
||||
<Property name="baseDir">/home/logs/reoc/services/</Property>
|
||||
</Properties>
|
||||
|
||||
<Appenders>
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<!-- C O N S O L E - A P P E N D E R -->
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<Console name="CONSOLE" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n" />
|
||||
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
</Console>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<!-- D E B U G _ F I L E - A P P E N D E R -->
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<RollingFile name="FILE_DEBUG" append="true" fileName="${baseDir}/services_debug.log" filePattern="${baseDir}/$${date:yyyy-MM}/services-debug-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
<PatternLayout>
|
||||
<pattern>%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy />
|
||||
<SizeBasedTriggeringPolicy size="250 MB"></SizeBasedTriggeringPolicy>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="12" />
|
||||
</RollingFile>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<!-- I N F O _ F I L E - A P P E N D E R -->
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<RollingFile name="FILE_INFO" append="true" fileName="${baseDir}/services_info.log" filePattern="${baseDir}/$${date:yyyy-MM}/services-info-%d{yyyy-MM-dd}.log.gz">
|
||||
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
<PatternLayout>
|
||||
<pattern>%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy />
|
||||
<SizeBasedTriggeringPolicy size="250 MB"></SizeBasedTriggeringPolicy>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="12" />
|
||||
</RollingFile>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<!-- F I L E _ W A R N - A P P E N D E R -->
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<RollingFile name="FILE_WARN" append="true" fileName="${baseDir}/services_info.log" filePattern="${baseDir}/$${date:yyyy-MM}/services-warn-%d{yyyy-MM-dd}.log.gz">
|
||||
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
<PatternLayout>
|
||||
<pattern>%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy />
|
||||
<SizeBasedTriggeringPolicy size="250 MB"></SizeBasedTriggeringPolicy>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="12" />
|
||||
</RollingFile>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<!-- F I L E _ E R R O R - A P P E N D E R -->
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<RollingFile name="FILE_ERROR" append="true" fileName="${baseDir}/services_error.log" filePattern="${baseDir}/$${date:yyyy-MM}/services-error-%d{yyyy-MM-dd}.log.gz">
|
||||
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
|
||||
<PatternLayout>
|
||||
<pattern>%-d{yyyy/MM/dd HH:mm:ss} %-5p [%c] - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy />
|
||||
<SizeBasedTriggeringPolicy size="250 MB"></SizeBasedTriggeringPolicy>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="12" />
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<!-- LOGGERS -->
|
||||
<Loggers>
|
||||
|
||||
<Logger name="com.ossez" level="TRACE" additivity="false">
|
||||
<AppenderRef ref="CONSOLE" level="DEBUG" />
|
||||
<AppenderRef ref="FILE_DEBUG" level="DEBUG" />
|
||||
<AppenderRef ref="FILE_INFO" level="INFO" />
|
||||
<AppenderRef ref="FILE_WARN" level="WARN" />
|
||||
<AppenderRef ref="FILE_ERROR" level="ERROR" />
|
||||
</Logger>
|
||||
|
||||
|
||||
<root level="TRACE">
|
||||
|
||||
</root>
|
||||
|
||||
</Loggers>
|
||||
|
||||
</Configuration>
|
Loading…
Reference in New Issue