ACM试题 - 另一种阶乘问题
生活随笔
收集整理的這篇文章主要介紹了
ACM试题 - 另一种阶乘问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. ACM試題題源:http://acm.nyist.net/JudgeOnline/problem.php?pid=65
描述
大家都知道階乘這個概念,舉個簡單的例子:5!=1*2*3*4*5.現(xiàn)在我們引入一種新的階乘概念,將原來的每個數(shù)相乘變?yōu)閕不大于n的所有奇數(shù)相乘例如:5!!=1*3*5.現(xiàn)在明白現(xiàn)在這種階乘的意思了吧!
現(xiàn)在你的任務(wù)是求出1!!+2!!......+n!!的正確值(n<=20)
輸入2. 代碼
我的代碼
1 //運(yùn)行時間:25ms 內(nèi)存:184 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args){ 7 8 Scanner cin = new Scanner(System.in); 9 int a = cin.nextInt(); 10 int[] re = new int[a]; 11 int[] tenNum = new int[10]; 12 for(int i = 0; i < 10; i++){ 13 if(i == 0){ 14 tenNum[i] = 1; 15 }else if(i == 1){ 16 tenNum[i] = 3; 17 }else{ 18 tenNum[i] = tenNum[i-1] * (i*2+1); 19 } 20 } 21 for(int i = 0; i < a; i++){ 22 int n = cin.nextInt(); 23 re[i] = 0; 24 if(n%2==0){ 25 for(int j = 0; j < n/2; j++){ 26 re[i] = re[i] + tenNum[j]; 27 } 28 re[i] = re[i] * 2; 29 }else{ 30 for(int j = 0; j < (n+1)/2; j++){ 31 re[i] = re[i] + tenNum[j]; 32 } 33 re[i] = re[i] * 2 - tenNum[(n+1)/2-1]; 34 } 35 } 36 37 for(int i = 0; i < a; i++){ 38 System.out.println(re[i]); 39 } 40 cin.close(); 41 } 42 } View Code參考代碼1
1 //運(yùn)行時間:1ms 內(nèi)存:122 2 3 public class Main{ 4 5 public static int parseInt(String s){ 6 int i, value, multi=10; 7 8 value = s.charAt(s.length()-1) - 48; 9 for(i=s.length()-2; i>=0; i--){ 10 value += (s.charAt(i) - 48) * multi; 11 multi*=10; 12 } 13 14 return value; 15 } 16 17 public static void main(String args[])throws java.io.IOException{ 18 java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); 19 java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.out)); 20 StringBuilder result = new StringBuilder(""); 21 String s; 22 int i, j, a, n; 23 long sum=0, tmp=0; 24 25 while((s=br.readLine())!=null){ 26 a = parseInt(s); 27 28 while(a-->0){ 29 n = parseInt(br.readLine()); 30 sum=0; 31 tmp=1; 32 33 for(i=1; i<=n; i++){ 34 for(j=1; j<=i; j+=2){ 35 tmp*=j; 36 } 37 sum+=tmp; 38 tmp=1; 39 } 40 41 result.append(sum).append("\n"); 42 } 43 44 bw.write(result.toString()); 45 bw.flush(); 46 result.setLength(0); 47 } 48 } 49 } View Code參考代碼2
1 //運(yùn)行時間:2 內(nèi)存:61 2 3 import java.util.Scanner; 4 5 public class Main { 6 7 private static Scanner input = new Scanner(System.in); 8 public static void main(String[] args) { 9 int t = input.nextInt(); 10 while (t-- != 0) { 11 int n = input.nextInt(); 12 int sum = 0; 13 for (int i = 1; i < n+1; i++) { 14 sum += jiechen(i); 15 } 16 System.out.println(sum); 17 } 18 } 19 private static int jiechen(int i) { 20 int sum = 1; 21 for (int j = 1; j < i+1; j += 2) { 22 sum *= j; 23 } 24 return sum; 25 } 26 } View Code3. 總結(jié)
不難看出別人的代碼不論是在時空復(fù)雜度還是在代碼的易閱讀性上面,都很健壯。而我的就很一般化了,還有要學(xué)習(xí)別人引用包后的使用方式,變量定義的方式,要學(xué)會把問題子問題化。
轉(zhuǎn)載于:https://www.cnblogs.com/louislee92/p/3949270.html
總結(jié)
以上是生活随笔為你收集整理的ACM试题 - 另一种阶乘问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#利用反射+特性实现简单的实体映射数据
- 下一篇: SQL模糊查询 LIKE