import java.util.Scanner; public class Factorials { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String k = in.next(); char a[] = k.toCharArray(); int length = a.length; function(n, length); in.close(); } private static void function(int n, int length) { int t = n % length; int c = (t == 0 ? length : t); int sum = n; for (int i = 1;; i++) { sum = sum * (n - i * length); if ((n - i * length) == c) { System.out.println(sum); break; } } } } If k > n then answer is n. Try "5 !!!!!!" Тест №8. Какой должен быть ответ? Ни у кого нет тестов (кроме тех, кто создавал задачи и соответственно тесты). Мы можем только догадываться какой может быть тест № а , если смотреть на каком тесте валится наш код. В общем методом подбора public class Factorials { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); StringTokenizer st = new StringTokenizer(str, " ");
int n = Integer.parseInt(st.nextToken()); int k = st.nextToken().length(); if (n >= 1 && n <= 10) { if (k >= 1 && k <= 20) { int limite= n % k, total = n, res= 0, i = 1; do { res= (n - (k * i)); if (res== 0) { System.out.print(total); return; } total *= res; i++; } while (res!= limite); System.out.print(total); } } } } Edited by author 19.09.2015 06:20 According to input data 9!! = 9(9-1*2)(9-2*2)(9mod2) = 9*7*5*1=315 Edited by author 04.06.2015 18:13 Edited by author 04.06.2015 18:13 You forgot the multiplication with 3. 9 !! = 9(9-1*2)(9-2*2)(9-3*2)(9-4*2) = 9 * 7 * 5 * 3 * 1 = 945 The formula presented in the text has some "...", meaning that the product continues until the last member is either n mod k (in case n doesn't divide k) or k (in the other case). it will minus(-) what your input in string.... like you input (!!!) and enter 9,,,so there 9-3=6 then 6-3=3 then 3-3=1 so 9*6*3*1=162 so that's why u can solve this by 9 !!==> 9-2=7,7-2=5,5-2=3,3-2=1; so 9*7*5*3*1=945. that's it why wrong ans test case # ?? Plzzz help my code: #include<bits/stdc++.h> using namespace std; int main() { int a[12],n,i; //int n,i; float res,sum=0; cin>>n; for(i=0; i<n; i++) { cin>>a[i]; sum+=a[i]; } res=(float)sum/n; if(res==5) printf("Named\n"); else if(res>=4.5&&res<5) printf("High\n"); else if(res>3&&res<4.5) printf("Common\n"); else printf("None\n"); return 0; } Sorry for my english. First of all check the value of k: 0 < k < n. If False, then the result is n, otherwise -- use formulas. I saw your recent topic, so maybe someone will answer MY Q here (haha). Guys, help me to understand. I see 'n!!…! = n(n−k)(n−2k)…(n mod k)', between '-' sign and 'k' IS value here(which changes from 0...2...X . I used X, 'cause I dont know, TO WHICH number should this value change (of course 'k' or 'n'), tell me to which and why ? Actually, I think this task isn't formalized as shoud be(tell me if i'm wrong please). Thanks, you're right about that ... so stupid. Edited by author 06.05.2015 13:09 Can k be gretter than n if possible what should we do in this case? Если изменить правило 2(если n делится на k) и вместо k умножить на 1, то все заработает. Определение 1. n!!…! = n(n−k)(n−2k)…(n mod k), если n не делится на k; n!!…! = n(n−k)(n−2k)…1, если n делится на k (знаков ! в обоих случаях k штук). it's very simple,especially on java. Если k>n что будет выводить? Why "Time limit exceeded" ??? Edited by author 28.09.2014 09:03 #include<iostream> #include<string> int res; int Fr(short int nm, short int q) { if((nm-q) == 0) return res; if((nm-q)>0) { res *= nm-q; Fr(nm-q,q); } else return res; } int main() { short int n=0; char c; int k=0; std::string s; std::string ss=""; std::getline(std::cin,s); for(int i=0;i<s.length();i++) { if(isdigit(s[i])) ss += s[i]; if(s[i] == '!') k++; } int w=1; int temp; for(int i=0;i<ss.length();i++) { for(int j=0;j<(ss.length()-i-1);j++) w *= 10; if (ss[i] >= '0' && ss[i] <= '9') temp = ss[i] - '0'; n += temp*w; w=1; }
res = n; printf("%d",Fr(res,k)); } Что не так ?? #include <stdio.h> #include <string.h> int main() { int a, fact = 1, i, count; char str[256]; scanf("%i %s", &a,str); for (count = 0; str[count] != '\0';) if (str[count] == '!') count++; if (a%count != 0) for (i = a%count; i <= a; i += count) fact *= i; else { fact *= count; for (i = count; i <= a; i += count) fact *= i; } printf("%i", fact); return 0; } else { //fact *= count; for (i = count; i <= a; i += count) fact *= i; } Here is problem. what's wrong with my code? i tried a few test cases and the result is good. #include<stdio.h> int main() { int num,n=0; int res=1; char a; scanf("%d ",&num); while(scanf("%c",&a)==1) n++; for(int i=num%n;i<=num;i+=n) { if(i==0) i=n; res=res*i; } printf("%d",res); } Edited by author 23.05.2013 23:39 try adding '\n' to the output package practice; import java.util.Scanner; public class Factorial { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner (System.in); System.out.println("enter the no"); int num = sc.nextInt(); String s = sc.next();
int k = s.length(); int fact = 1;
if(num %k ==0){ for(int i = num; i>=k; i = i-k){ fact = fact*i; } } else { for(int i =num; i >0; i = i-k){ fact = fact*i; } }
System.out.println(fact); } } 1) множитель не может быть отрицательным 2) умножать надо не по количеству восклицательных знаков а пока i*k < n 3) если ( n - i*k ) равно n % k или ( n - i*k ) равно k значит мы посчитали факториал ( то есть надо больше ничего с числом не делать ) 4) n и k от 1 до 20 5) составители задачи тупые мудаки I would say the same ("tupoi mudak" (*)) about the author of the post. Problem statement is more than clear: "n!!…! = n(n−k)(n−2k)…(n mod k)", "X mod Y — a remainder after division of X by Y", then even example is given: "10 mod 3 = 1". If after all this you couldn't derive that "1) множитель не может быть отрицательным" - then you're really (*) "3) если ( n - i*k ) равно n % k или ( n - i*k ) равно k значит мы посчитали факториал ( то есть надо больше ничего с числом не делать )" - just copy from the problem statement "4) n и k от 1 до 20" - just wrong (n <= 10, as, again, given in the statement) What is test #1?? maybe you divide a number by zero to get that runtime error the product is : n*(n-k)*(n-2*k)... till---> k if divisible by k, i.e. the last non zero value of n OR n(mod)k which is again the last non zero value of n. You don't have to multiply it with k or n mod k again. |
|