2010年7月14日 星期三

[Timus Online Judge] 1083. Factorials!!!

Try first time and got AC.
The point is how to get string.
Count how many '!' do user input.







Time Limit: 1.0 second
Memory Limit: 16 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).



#include
#include


int Factorials(int n,int k){

if (n<=k){
if(n%k == 0)
return k;
else
return n%k;

}
else
return n*Factorials(n-k,k);

}

int main(void){

size_t nbytes = 2;
int n_num,k_num;
char *line;
line = (char *) malloc (nbytes+1);

scanf("%d ",&n_num);

k_num = 0;
while ( fgets(line, nbytes , stdin) != NULL ){
k_num++;
}
k_num--;

printf("%d",Factorials(n_num,k_num));

return 0;
}

Related Posts:

0 意見:

張貼留言