Page 3 #include <stdio.h> #include <time.h> //#include <windows.h> // GetTickCount() int fun(int n); int main() { int n,max,t,x; long te=0, ts=0; printf("\n Enter the Number..\n"); scanf("%d",&n); x=n; max=0; // ts = GetTickCount(); if(n%2==0) n=n-1; while(n>=0) { if((t=fun(n))>max) max=t; n=n-2;
}
printf("\n %d %d \n",x,max); // te = GetTickCount(); // printf("\n ts=%ld \n",te); //printf("%d ms\n", te - ts); // getch();
} int fun(int n) { if(n==0)return 0; else if(n==1)return 1; else if(n%2==0)return(fun(n/2)); else if(n%2==1)return(fun((n-1)/2)+fun((n-1)/2+1));
return 0; } a_10=a_5=a_2+a_3=a_1+a_1+a_2=1+1+1=3 Why it's 4? 3 is the 10th element. So you should find a maximum of 10 elements. It's 4. Edited by author 22.03.2011 01:05 what`s wrong? var n,i,res,c,max:longint; a:array[0..100000] of integer; begin repeat max:=0; Readln(n); a[0]:=0; a[1]:=1; for i:=1 to n do begin a[2*i]:=a[i]; a[2*i+1]:=a[i]+a[i+1] end; for i:=1 to n do if a[i]>max then max:=a[i]; if n<>0 then Writeln(max); until n=0; end. i find mistake: a:array[0..200001] of integer; this is accepted) #include<iostream> using namespace std; int x=1; int recursive(int n) { if(n==0) return 0; if(n==1) return n; if((n%2)==0) recursive(n/2); else if(n>2 ) { if(n%2) { recursive(n/2); return 1+recursive((n/2)+1); }
} return x; } int maxi(int input) { int temp,result=0; for(int i=0;i<=input;i++) { temp=recursive(i); if(result<temp) { result=temp; } } return result; } int main(void) { int result,input=-1; while(1) { cin>>input; if(input==0) break; result=maxi(input); cout<<result<<endl; } return 0; } you can't do brute thing ... i have written algorithm that uses log2() which is in c++(<cmath> library) and when i used it copmpilation error occured "error C3861: 'log2': identifier not found" when i compile it in devc++ it works perfectly.. can everyone helm me? :( Timus use the compiler visual c++ compiler not gcc. hint: log2(n) = log(n)/log(2) (but this sum at the complexity) Edited by author 21.01.2011 22:32 Edited by author 21.01.2011 22:32 Timus use the compiler visual c++ compiler not gcc. hint: log2(n) = log(n)/log(2), (but this sum at the complexity) 5 - оутпут 3 10 - оутпут 4 0 (return) Got to do only one thing find the maximum in this array If the array size is 5 then maximum = 3 and if 10 then 4 is max so, I solved the problem in the givven way and in given way the max of 5(sized array) isn't 3 here is the input and output cin>> 5 0 1 1 2 1 max = 2 _____________________________ cin >> 10 0 1 1 2 1 3 2 3 1 4 max = 4 _______________________________________ input>>6 0 1 1 2 1 3 max = 3 ______________________________________ please help the Solution of Problem is Actually thiS, In c++ But dOesn't work maybe you can notice something?? for(int i = 0; i < a; i++){ if(2*i < a){ arr[2*i] = arr[i]; } if(2*i+1 < a){ arr[2*i+1] = arr[i] + arr[i+1]; } } What is the number 1 stand for? and test # 2 ? 3 ? 4 ? i think you should read all input before starting traitement....good luck #include<iostream> using namespace std; void K(int); void main() { int b[10],j=0,k=0; for(;j<10;j++) { cin>>b[j]; if(b[j]==0) break; } for(;k<j;k++) K(b[k]); }
void K(int n) { if(n!=1) { n++; int *a = new int [n]; a[0]=0; a[1]=1; int i=2; for(;i<n;i++) { if(i%2==0){a[i]=a[i/2];} else {a[i]=a[(i-1)/2]+a[(i-1)/2+1];} } if(n%2==0){cout<<a[n-1];} else {cout<<a[n-2];} cout<<endl; } else { cout<<0<<endl; } } Hello everyone! My program was accepted only with 'newline' output for each test case. Before that I`ve got I/A. You have been warned :) See you! I have submitted solution and have got AC (ID 3108352) but it doesn't pass some tests. For example, for input 146 0 its output is 21, not 23. And there are a lot of other cases with wrong output. I think, something should be changed in test data. New tests were added. 208 authors lost AC after rejudge. Wow, that was your gift for Christmas?? I'm kidding of course; it takes no more than 5-6 mins to write a new correct solution:) Happy holidays! what is test #5? why did I have wa var k,n,s,j,max:integer; a:array [0..100000] of integer; b:array [1..10] of integer; begin a[1]:=1; a[0]:=0; for k:=2 to 10000 do begin if k mod 2 =0 then a[k]:=a[k div 2] else a[k]:=a[k div 2]+a[(k div 2)+1]; end; k:=0; while not eof do begin k:=k+1; readln(b[k]); end; n:=k-1; for k:=1 to n do begin max:=0; s:=b[k]; for j:=1 to s do begin if a[j]>max then max:=a[j]; end; writeln(max); end; end. Same problem here. It gives me crash on test 5. Maybe it's something to do with the case when the input consists of only one line - 0 as it's not specified what the program should do then. Yep, same here.... crash using java on test 5. GOT AC... error was when input was repeated e.g: 10000 10000 0 Answer: 521 521 My program was crashing because of using a set. GL Edited by author 03.10.2010 06:24 where you find test cases? i think it will work out fine. But just got WA1. Can you help me ? thx! #include <cstdlib> #include <iostream> using namespace std;
int shu(int); int main(int argc, char *argv[]) { int a[10],b=0,max=0,d=0; while (cin>>a[b]) {if (a[b]==0) break; b++;} while (1) {if (a[d]==0) break; while (a[d]>=0) {if (shu(a[d])>max) max=shu(a[d]); a[d]--;} cout<<max<<endl; d++;}
system("PAUSE"); return EXIT_SUCCESS; }
int shu(int n) {int c; if (n==0) c=0; else if (n==1) c=1; else if (n%2==0) c=shu(n/2); else c=shu((n-1)/2)+shu((n-1)/2+1);} I have the same trouble whit you,WA1!!! But I test it myself whit my computer but find nothing wrong! my code: #include "stdio.h" #include "iostream.h" #include "math.h" int main() { long a[100001],i; a[0]=0; a[1]=1; for(i=2;i<100001;i++) { if(i%2==0) { a[i]=a[i/2]; } else { a[i]=a[i/2]+a[i/2+1]; } } int t[10]; i=0; while(true) { cin>>t[i]; if(t[i]==0) break; i++; } i=0; long max; while(t[i]!=0) { for(int j=0;j<t[i]+1;j++) { if(a[j]>max) { max=a[j]; } } cout<<max<<"\n"; max=-1; i++; } return 0; } What for N=10 answer 4??? a1 = 1 a2 = 1 a3 = 2 a4 = 1 a5 = 3 a6 = 2 a7 = 3 a8 = 1 a9 = 4 a10 = 3 Maximal numbler among a1,...,a10 is a9, which is equal to 4. I understand!Thank you!=) What will be the output for N=10? For N=10 output is 4. For N=100000 output is 2584. For N=77777 output is 2207. For N=12345 output is 610. For N=5000 output is 322. For N=99999 output is 2584. For N=100 output is 21. For N=50 output is 13. program p1079; var a:array[0..100000] of longint; m:array[1..100000] of longint; n,max,i:longint; begin max:=1; a[1]:=1; m[1]:=1; readln(n); while n<>0 do begin if n<=max then writeln(m[n]) else begin for i:=max+1 to n do begin if odd(i) then a[i]:=a[i div 2]+a[i div 2+1] else a[i]:=a[i div 2]; if a[i]>=m[i-1] then m[i]:=a[i] else m[i]:=m[i-1]; end; end; writeln(m[n]); readln(n); end; end. test: 1 2 3 0 right answer is: 1 1 2 Good luck)) Urraaa... Accepted ...!!! Good test.Thank you very much (apple_worm)...!!! #include <stdio.h> int n,x; int a[10]; int i,t; int _max(int s1,int s2,int x) { if (x==n) return s1+s2; else { int t1,t2; if (x*2-1<=n) t1 = _max(s1,s2+s1,x*2-1); else t1 = 0; if (x*2+1<=n) t2 = _max(s1+s2,s2,x*2+1); else t2 = 0; if ((t1==t2)&&(t2==0)) return s1+s2; else return t1>t2?t1:t2; } } int main() { scanf("%d",&n); i = 0; while (n){ if (n==2) a[i] = 1; else if (n==1) a[i] = 1; else if (n==0) a[i] = 0; else a[i] = _max(1,1,3); scanf("%d",&n); i++; } for (n = 0;n<i;n++) printf("%d\n",a[n]); return 0; } Worked without homesave for me. Just bruteforce is enough #include<iostream.h> void main() { int a[10000],max[10]; int i,j,num,n; for(i=0;i<10;i++) { cin>>n; if(n==0) break; max[i]=0; for(j=0;j<=n;j++) { if(j==0) a[j]=0; else if(j==1) a[j]=1; else if(j%2==0) a[j]=a[j/2]; else a[j]=a[(j-1)/2]+a[(j-1)/2+1]; if(a[j]>max[i]) max[i]=a[j]; } } num=i; for(i=0;i<num;i++) cout<<max[i]<<endl; } N goes up to 100000, not merely 10000. |
|