5 rows of code to solve this My Endlish is not that good, could anyone translate it into Russian, pls? Так с гугл-переводчиком понятно заходит решение берем пары соседних чисел и выводим минимум из их нодов( #include <iostream> #include <vector> using namespace std; int gcd(int a, int b) { if(a == b) return a; while( a != 0 && b !=0) if(a > b) a%=b; else b%=a; return a+b; } int main() { int N; cin >> N; int array[1000]; for(int i = 0; i < N; i++) cin >> array[i]; if(N == 1) cout << array[0]; else{ vector <int> mins; for(int i = 0; i < N-1; i++) mins.push_back(gcd(array[i], array[i+1])); int min = mins[0]; for(int i = 0 ;i < mins.size(); i++) if(min > mins[i]) min = mins[i]; cout << min; } return 0; } это вроде не камильфо! I haven't used word "impossible" at all in my AC program :) It's impossible to get "impossible" =))) Confusing question: HOW ARE THE BLANKS CHOSEN? Let's see example 2 3 4 - initial blanks I choose blanks 2 and 4 - they are different so I cut off piece of length 2 from the long blank (4). After that we have blanks: 2 3 2 - two blanks coincide so one is eliminated: 2 3 they are different so we cut of blank with length 3. We have: 2 1. Cut again we have 1 1. Eliminate one blank and we get a result - 1. You may try other ways to choose blank - but you will get same answer. So, NO MATTER HOW TO CHOOSE 2 blanks. Moral: What is not said does not matters ;) By the way algorithm is very very easy... try to think Captain Obvious Edited by author 04.10.2010 16:51 does anybody knows what is this fucking test 5? if you know write the answer please:) sorry i've mistaked i've written knows instead of know i've got tl on tets 7 cannot you say why? i just calculate gcd of all numbers Edited by author 26.04.2010 21:02 Edited by author 26.04.2010 21:03 #include <cstdlib> #include <iostream> //----------Funkcje---------------------------- long long nwd(long long a, long long b) { if (b>=1) nwd(b, a%b); if (b=0) return a; } using namespace std; //---------------Program------------------------------------ int main(int argc, char *argv[]) { int n; long long tab[1002]={0}; //printf("Dla ilu liczb chcesz policzyc NWD?: \n"); scanf("%d", &n); //printf("Podaj liczby: \n"); for (int i=0;i<n;i++) scanf("%I64d", &tab[i]); long long wynik=tab[0]; for (int i=0;i<n-1;i++) { wynik=nwd(tab[i],tab[i+1]); tab[i+1]=wynik; }
//printf("NWD wynosi %I64d \n", wynik); printf("%I64d", wynik);
system("PAUSE"); return EXIT_SUCCESS; } use __int64 instead long long #include <cstdlib> #include <iostream> //----------Funkcje---------------------------- long long nwd(long long a, long long b) { if (b>=1) nwd(b, a%b); if (b=0) return a; } using namespace std; //---------------Program------------------------------------ int main(int argc, char *argv[]) { int n; long long tab[1002]={0}; //printf("Dla ilu liczb chcesz policzyc NWD?: \n"); scanf("%d", &n); //printf("Podaj liczby: \n"); for (int i=0;i<n;i++) scanf("%I64d", &tab[i]); long long wynik=tab[0]; for (int i=0;i<n-1;i++) { wynik=nwd(tab[i],tab[i+1]); tab[i+1]=wynik; }
//printf("NWD wynosi %I64d \n", wynik); printf("%I64d", wynik);
system("PAUSE"); return EXIT_SUCCESS; } Can anyone give me a test for which the answer is "impossible"? > Can anyone give me a test for which the answer is "impossible"? it impossible that "IMPOSSIBLE" #include int gcd( int a, int b ){ return b ? gcd( b, a%b ) : a; } int main () { int n, L, g=0; cin >> n; while( cin >> L ) g=gcd( L, g ); cout << g << endl; } include what? gcd is wrong too! #include <iostream> using namespace std; int gcd( int a, int b ) { return b ? gcd( b, a%b ) : a; } int main () { int n, L, g=0; cin >> n; while( cin >> L ) g=gcd( L, g ); cout << g << endl; } Change your code to this and it runs perfectly! Not complited first line, i suppose: #include (what??) #include <stdio.h> #include <stdlib.h> int compare(const int *, const int *); int gcd(int, int); int main(void) { int size = 0,counter = 0; scanf("%d",&size); int ls[size]; int * pointer = ls; while(counter < size) { scanf("%d", pointer++); counter++; } qsort(ls, size, sizeof(int), compare); int x = ls[0]; while(--counter > 0) { x = gcd(x, ls[counter]); } printf("%d", x); return 0; } int gcd(int small, int big) { if (small == 0) return big; return gcd(big % small, small); } int compare(const int *p1, const int *p2) { int x = *p1; int y = *p2; if(x < y) { return -1; } else { return 1; } } ===================== 2904ea07-b44d-48c6-ba40-3b3f94228782 2904ea07-b44d-48c6-ba40-3b3f94228782(11): error: declaration may not appear after executable statement in block int ls[size]; ^ 2904ea07-b44d-48c6-ba40-3b3f94228782(11): error: expression must have a constant value int ls[size]; ^ 2904ea07-b44d-48c6-ba40-3b3f94228782(12): error: declaration may not appear after executable statement in block int * pointer = ls; ^ 2904ea07-b44d-48c6-ba40-3b3f94228782(18): warning #167: argument of type "int (*)(const int *, const int *)" is incompatible with parameter of type "int (__cdecl *)(const void *, const void *)" qsort(ls, size, sizeof(int), compare); ^ 2904ea07-b44d-48c6-ba40-3b3f94228782(19): error: declaration may not appear after executable statement in block int x = ls[0]; ^ 2904ea07-b44d-48c6-ba40-3b3f94228782(47): warning #1: last line of file ends without a newline } ^ compilation aborted for 2904ea07-b44d-48c6-ba40-3b3f94228782 (code 2) ============= junmin@thinking ~/workspace/otros/ACM $ gcc --version gcc (Gentoo 4.3.1-r1 p1.1) 4.3.1 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. junmin@thinking ~/workspace/otros/ACM $ icc --version icc (ICC) 10.1 20080801 Copyright (C) 1985-2008 Intel Corporation. All rights reserved. thanks!!!! Edited by author 02.10.2008 23:11 Your code: int compare(const int *, const int *); Right code: int compare(const void *, const void *); Your code: int ls[size]; Right code: int *ls = new int[size]; I wonder, where have you found C/C++ compiler which can successful compile it. =) My MinGW-gcc 3.4.2, Visual 2003, Visual 2005, Mandriva-gcc 3.4.2 say "Compilation error" Can anyone explain me the problem. Minimal numbers????????????????? What on earth is that. I have gone throuh he webboard but found nothing answering my question. I just can't understand where the mistake is. In my program? I don't know. But either the example or the algorithm explained is absolutely wrong. Tell me please :) heh... I didn't got the clue when I have been solving it, my program of course solved it OK, but it worked 500ms while other's programs worked 20-30 ms :) >Only find NOD of all pairs of numbers >2 and 4 has NOD 2 e.t.c. What the hell is a NOD on earth!!! Can someone explain? NOD = GCD (Greatest Common Divisor) (I think) I guess it is from russian (for example in bulgarian it is NOD too, and the languages are close) If lengths of the blanks are different, then the piece of the most long blank is sawed off and its length must be equal to the length of the other blank. Then the algorithm is repeated from point 2.??? what does it mean Edited by author 15.05.2005 19:59 yes. whose length must be equal to the length of the other? The sawed off part or the remaining part??? Don't they mean planks ? :) This is my programme,please tell me why I got WA.Thank you! var n,m,m0,ys:longint; procedure pd(a,b:longint); var t:longint; begin if a<b then begin t:=b; b:=a; a:=t; end; while a mod b<>0 do begin a:=a mod b; t:=a;a:=b;b:=t; end; ys:=b; end; procedure run; var i:integer; begin for i:=1 to n-1 do begin readln(m); pd(m0,m); m0:=ys; end; writeln(ys); end; begin readln(n); read(m0); run; end. Maybe replace readln to read who know please tell me ? give me a sample programme is the best! var a:array[1..1000]of longint; n,i,j:longint; function nod(k,l:longint):longint; begin repeat if k<l then l:=l mod k else k:=k mod l; until (k=0)or(l=0); nod:=k+l; end; begin read(n); for i:=1 to n do read(a[i]); for i:=n-1 downto 1 do a[i]:=nod(a[i],a[i+1]); write(a[1]); end. I'm quite sure that either the example or the problem statement is wrong....Is it possible to correct them? I've replaced "Let's choose 2 blanks with minimal numbers" with "Let's choose some 2 blanks" Is problem ok now? Edited by author 27.12.2005 14:42 |
|