日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ACM试题 - 另一种阶乘问题

發(fā)布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)在你的任務是求出1!!+2!!......+n!!的正確值(n<=20)

輸入
第一行輸入一個a(a<=20),代表共有a組測試數(shù)據(jù) 接下來a行各行輸入一個n.
輸出
各行輸出結果一個整數(shù)R表示1!!+2!!......+n!!的正確值
樣例輸入
2 3 5
樣例輸出
5 23

2. 代碼

我的代碼

1 //運行時間:25ms 內存: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 //運行時間:1ms 內存: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 //運行時間:2 內存: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 Code

3. 總結

不難看出別人的代碼不論是在時空復雜度還是在代碼的易閱讀性上面,都很健壯。而我的就很一般化了,還有要學習別人引用包后的使用方式,變量定義的方式,要學會把問題子問題化。

轉載于:https://www.cnblogs.com/louislee92/p/3949270.html

總結

以上是生活随笔為你收集整理的ACM试题 - 另一种阶乘问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。