标签:汉诺塔问题 val sample pid 运用 system.in mis ESS oid
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2044
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95054 Accepted Submission(s):
33882
import java.math.BigInteger; import java.util.Scanner; public class Main { public static BigInteger func(int num) { BigInteger[] bigIntegers = new BigInteger[51]; bigIntegers[1] = BigInteger.valueOf(1); bigIntegers[2] = BigInteger.valueOf(2); for(int i = 3;i <=num;i++) { bigIntegers[i] = bigIntegers[i-1].add(bigIntegers[i-2]); } return bigIntegers[num]; } public static void main(String[] args) { @SuppressWarnings("resource") Scanner inScanner = new Scanner(System.in); int number = inScanner.nextInt(); while(number-->0) { int a = inScanner.nextInt(); int b = inScanner.nextInt(); System.out.println(func(b-a)); } } }
C++代码:
#include <iostream> using namespace std; long long fun(int n) { long long a[51]; a[1]=1; a[2]=2; for(int i=3;i<=n;i++) //可以用这个递推方法,减少计算量。注意运用数组。 a[i]=a[i-1]+a[i-2]; return a[n]; } int main() { int n; cin>>n; while(n--) { int a,b; cin>>a>>b; printf("%lld\n",fun(b-a)); } return 0; }
标签:汉诺塔问题 val sample pid 运用 system.in mis ESS oid
原文地址:https://www.cnblogs.com/Weixu-Liu/p/9651562.html