程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> URAL 1083. Factorials!!! (閱讀理解)

URAL 1083. Factorials!!! (閱讀理解)

編輯:關於C++

 

1083. Factorials!!!

Time limit: 1.0 second
Memory limit: 64 MB
Definition 1. n!!…! = n(n?k)(n?2k)…(n mod k), if k doesn’t divide n; n!!…! = n(n?k)(n?2k)…k, if k divides n (There are k marks ! in the both cases). Definition 2.X mod Y — a remainder after division of X by Y. For example, 10 mod 3 = 1; 3! = 3·2·1; 10!!! = 10·7·4·1. Given numbers n and k we have calculated a value of the expression in the first definition. Can you do it as well?

Input

contains the only line: one integer n, 1 ≤ n ≤ 10, then exactly one space, then k exclamation marks, 1 ≤ k ≤ 20.

Output

contains one number — n!!…! (there are k marks ! here).

Sample

input output
9 !!
945
Problem Author: Oleg Katz
Problem Source: The 3rd high school children programming contest, USU, Yekaterinburg, Russia, March 4, 2001

 

 

 

 

 

解析:直接按題目中的定義計算即可。

 

 

 

 

AC代碼:

 

#include 
using namespace std;

int main(){
    int n;
    string s;
    while(~scanf("%d", &n)){
        cin>>s;
        int k = s.size();
        int ans = 1;
        if(n % k){
            int t = n / k;
            for(int i=0; i<=t; i++){
                ans *= (n - i * k);
            }
        }
        else{
            int t = n / k - 1;
            for(int i=0; i<=t; i++){
                ans *= (n - i * k);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved