int main() { long int n,res; while(1){ scanf("%ld",&n); if(n!=0){ if(n%2==0){ res=function(n-1); printf("%ld\n",res); } else{ res=function(n); printf("%ld\n",res); } } else{ return 0; } } //gives right ans in codeblocks but here gives WA....Why?? return 0; } long int function(long int n){ if(n==1){ return 1; } if(n%2==0){ return 1; } else if(n%2==1){ return function(n/2)+function(n/2+1); } } you have to print the maximux number of n range.but in recursive formula it alawys give the n th value . n th value and maximum value of n range is not same #include <bits/stdc++.h> using namespace std; int main(){ int n; int a[100000]; a[0] = 0; a[1] = 1; int max = 1; while(cin >> n, n != 0){ if(n > max){ for(int i = max + 1; i <= n; ++i){ if(i % 2 == 0){ a[i] = a[i / 2]; } else{ a[i] = a[(i - 1) / 2] + a[(i - 1) / 2 + 1]; } max = n; } } cout << a[n - (n % 2 == 0)] << "\n"; } } Edited by author 10.01.2021 03:55 print the maximum value in the range 1 to n a13 is greater than a15 1.declare an array.size 1000000 2.genarate all number according the problem description. 3.input the value n. 4.declare a container and take the value of the array .vector<int>v(a,a+n); 5.sort the container. 5.at last print v[n]; Edited by author 26.07.2020 04:57 Edited by author 26.07.2020 04:58 import java.util.*; public class Maximums { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int N = -564646878; // random value hehehehe while (N != 0) { N = scn.nextInt(); if (N == 0) { break; // As per the problems requirement it should terminate at 0 } else if (N == 1) { System.out.println(1); } else { N = N + 1; // problems indexing are upto N int[] arr = new int[N]; // creating an array to create desired sequence arr[0] = 0; arr[1] = 1; // initialising the array with the given values in question int max = Integer.MIN_VALUE; // to start with for (int i = 2; i < N; i++) { if (i % 2 == 0) { arr[i] = arr[i / 2]; // for even indexes if (arr[i] > max) { max = arr[i]; // comparing }} else { arr[i] = arr[(i - 1) / 2] + arr[((i - 1) / 2) + 1]; // for odd indexes if (arr[i] > max) { max = arr[i];// comparing }}} System.out.print(max); // finally printing the max value upto the given index 'N'. System.out.println(); // for new line ( P.S. I know i could have made the upper line to println but that's boring }}}} /// Happy Coding Dudes n Dudettes why is the 10th element 4? isn't it 3? Ummm, I have got my answer accepted and I must tell you that you're right about the value is 3. Well, it's the answer value which is 4. Because 4 is the maximum value among all values up to the 10th index. i.e. 0 1 1 2 1 3 2 3 1 {4} 3 <----- Respected Values 0 1 2 3 4 5 6 7 8 9 10 <----- INDEX I hope that helped. If not ping me. I'll send you my solution. ping me at my ain't account - id (aquarius_gaurav). Peace. Edited by author 21.04.2020 00:36 You can solve the question very fast with appropriate precomputed values. The sequence is known as the Stern-Brocot sequence, I was not aware of it before! what is test 4. plz help me. my code is below #include<bits/stdc++.h> using namespace std; int main() { long n=1000,i,k; long long a[100000]; a[0]=0,a[1]=1; for(i=2;i<n;i=i+2){ k=i/2; a[i]=a[k],a[i+1]=a[k]+a[k+1]; } cin>>n; while(n){ cout<<*max_element(a,a+n+1)<<endl; cin>>n; } } Edited by author 22.06.2019 13:46 #include <iostream> 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() { std::cin >> 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); std::cin >> n; i++; } for (n = 0;n<i;n++) std::cout << a[n] << "\n";
return 0; } #include <iostream> #include <algorithm> using namespace std; int main() { unsigned int i, v[100000]; v[0] = 0; v[1] = 1; for(i=2;i<100000;i++){ if(i%2==0) v[i] = v[i/2]; else v[i] = v[(i-1)/2] + v[(i-1)/2+1]; } unsigned int n; while(cin >> n){ if(n!=0) cout << *max_element(v, v+n+1) << endl; } return 0; } /* its not accepted. whats wrong with it?? WA on test 4 */ #include<bits/stdc++.h> using namespace std; int main() { long n=1000,i,k; long long a[100000]; a[0]=0,a[1]=1; for(i=2;i<n;i=i+2){ k=i/2; a[i]=a[k],a[i+1]=a[k]+a[k+1]; } cin>>n; while(n){ cout<<*max_element(a,a+n+1)<<endl; cin>>n; } } Edited by author 22.06.2019 13:37 Edited by author 22.06.2019 13:37 Edited by author 22.06.2019 13:39 PS. У меня тесты проходит PSS. базовые) #include<iostream> #include<vector> using namespace std; int test(int a); vector<int> list; //vector<int> answers; int main() { list.push_back(0); list.push_back(1); int N; cin >> N; while (N != 0) { cout << test(N); // answers.push_back(test(N)); cin >> N; } //for (int i = 0; i < answers.size(); i++) // cout << answers[i] << endl; return 0; } int test(int a) { int i = 2; if (a < list.size() && a % 2 == 0) return list[a - 1]; else if (a < list.size() && a % 2 != 0) return list[a]; else if (a >= list.size()) i = list.size(); for (i; i <= a; i++) { if (i % 2 == 0) list.push_back(list[i / 2]); else list.push_back(list[i / 2] + list[i / 2 + 1]); } if (a % 2 == 0) return list[a - 1]; else return list[a]; } #include<stdio.h> #include<string.h> int main() { int n,i=0,j,temp=0; int a[99999]; int b[20]; scanf("%d",&b[0]); while(b[i]!=0) { i++; scanf("%d",&b[i]); } n=i; a[0]=0; a[1]=1; for(i=2;i<99999;i++) { if(i%2==0) a[i]=a[i/2]; else a[i]=a[i/2]+a[i/2+1]; } for(i=0;i<n;i++) { for(j=0;j<=b[i];j++) { if(a[j]>temp) { temp=a[j]; } } printf("%d\n",temp); } fflush(stdin); getchar(); return 0; } N can be as high as 99999; however you do int a[99999]; which only creates an array [0..99998]. Sigh, this counterintuitive syntax always creates errors like this. thank you very much but i change 99999 to 100000,it is still wrong ....sad...why... Did you change it from 99999 to 100000 in BOTH places? Also, you should do temp=0 before your for(j=0;j<=b[i];j++) or otherwise the maximum will remain the same as on previous request (you can try test 10, 5, 0 and it should give you 4 4 with your current code) thank you!!! it is temp that cause the wrong answer. thanks! I know that my algorithm exactly correct but only mathematically but not in program var n1:longword; function max(n:longword):word; begin max:=max+(n mod 2); if (n<>1) then if n mod 2=0 then max(n div 2) else begin max(n div 2); max((n div 2)+1); end; end; begin readln(n1); writeln(max(n1)); readln; end. Maybe I do not fully understand how do recursive functions operate. Edited by author 02.04.2016 15:07 If you change word to longint, it returns even more~ Even if you leave just function max(n:longword):word; begin max:=max+(n mod 2); end; , it will return an enormous number. Because, you see, in this very first row, you try to assign the function its own value (plus something). But it might be initialized in its own mysterious ways. So it's not how you do it. So uh... i solved this one long ago without any recursion, and i'm not quite sure what you're trying to do, but i modified it a bit, so at least it's trying to make some sense (even though it returns the wrong result still; but it's not that bad anymore, and maybe you'll be able to fix it into a proper version). So there you go. function max(n:longint):longint; var funcRes: longint; begin funcRes:=(n mod 2); if (n <> 1) then if n mod 2 = 0 then inc(funcRes, max(n div 2)) else begin inc(funcRes, max(n div 2)); inc(funcRes, max((n div 2)+1)); end; max:=funcRes; end; Thank you very much. I understood No problem. If you want to use recursion for calculations, maybe i'd suggest something like this: var i, n1, r1, maximum: longint; function x(p: longint): longint; begin if p <= 1 then x:=p else if p mod 2 = 0 then x:=x(p div 2) else x:=x(p div 2) + x(p div 2 + 1); end; begin readln(n1); maximum:=0; for i:=0 to n1 do begin r1:=x(i); if maximum < r1 then maximum:=r1; end; writeln(maximum); readln; end. But i think it's kinda slower than if you'd just precalculated the array, and then precalculated another array for maximums. It was quite likely harder back in the days, with 64kb turbo pascal limitations and all, so maybe you'd have to use smarter ways, rather than those blunt ones nowadays~ I meant I understood that I've used wrong way to get maximum. By the way I accidentally started to write a program that prints out the number from this sequence. And namely this way I think would be good to solve it. I was interested to write such a program and that's what I got: var n:longword; r:word; procedure max(n1,n2:word; var res:word); begin if n1 and (n1-1)=0 then inc(res) else if n1=3 then inc(res,2) else if n1 mod 2=0 then max(n1 div 2,0,res) else max(n1 div 2, (n1 div 2)+1,res); if n2<>0 then if n2 and (n2-1)=0 then inc(res) else if n2=3 then inc(res,2) else if n2 mod 2=0 then max(n2 div 2,0,res) else max(n2 div 2, (n2 div 2)+1,res); end; begin readln(n); if n=1 then writeln(1) else if n and (n-1)=0 then writeln(1) else if n mod 2=0 then begin max(n div 2,0,r); writeln(r); end else begin max(n div 2, (n div 2)+1,r); writeln(r); end; readln; end. Also div 2 can be shl 1 Edited by author 02.04.2016 20:42 Edited by author 02.04.2016 20:44 n=1 x=[] while(n<=10): j=int(input()) if j==0: break else: n+=1 x.append(j) def fun(k): if k==0: return 0 if k==1: return 1 if (k%2==0): return fun(k//2) if not(k%2==0): return (fun(k//2)+fun(k//2+1)) for l in range(0,len(x)): if x[l]%2==0: print(fun(x[l]-1)) if not(x[l]%2)==0: print(fun(x[l])) Edited by author 23.07.2015 23:40 I decided to write post my solution and comment it, may be it will be useful for someone. So, you have recursively defined sequence. Of course first idea is to write recursive function. But, we need to find max element from 1 to n and if you will call function for every n in order to find maximum element, such algorithm will cost O(n^2). Another way is to store results gotten before in special array and compare gotten result with max of them. I hope I helped you at least as it. And here is accepted solution in C++: #include <iostream> #define max(a, b) ((a) < (b) ? (b): (a)) int last = 1; int mem[100000]; int max[100000]; int F(int n) { for (int i = last+1; i <= n; ++i) { if (i%2) mem[i] = mem[i/2]+mem[i/2+1]; else mem[i] = mem[i/2]; max[i] = max(mem[i], max[i-1]); } return max[n]; } int main() { mem[0] = 0; mem[1] = 1; max[0] = 0; max[1] = 1; int n; while (1) { std::cin >> n; if (!n) break;
std::cout << F(n) << std::endl; } return 0; } well done, bro.... :) here's another way... >> -------------------------------------------------------------------------------------- #include <iostream> using namespace std; int arr[100000]; int find_max(int n) { int maximum = 0; for(int i = 0; i <= n; i++) if(maximum < arr[i]) maximum = arr[i]; return maximum; } int main() { int n[11], i, idx; arr[0] = 0; arr[1] = 1; for(idx = 2; idx < 100000; idx++) { if(idx & 1) { // when idx is odd. i = (idx-1) / 2; arr[idx] = arr[i] + arr[i+1]; } else { // when idx is even. i = idx / 2; arr[idx] = arr[i]; } } i = 0; while(cin >> n[i], n[i++] != 0); i--; idx = i; for(i = 0; i < idx; i++) { cout << find_max(n[i]) << '\n'; } return 0; } ------------------------------------------------------------------------------------- Edited by author 07.06.2015 08:42 #include <stdio.h> __int64 n, i, a[200002], b[200002], mx; void solve(){ a[1] = b[1] = mx = 1; for(i = 1 ; i < 100001 ; i ++){ a[i << 1] = a[i]; a[i << 1 | 1] = a[i] + a[i + 1]; if(mx < a[i]) mx = a[i]; b[i] = mx; } while(~scanf("%I64d", &n)){ if(!n) break; printf("%I64d ", b[n]); } } int main(){ solve(); } 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)...!!! 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 Edited by author 01.06.2014 00:29 |
|