Общий форум#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 100000000 void prime(int n) { int i,j,temp,count=0; for(i=2;i<=MAX;i++) { temp=0; for(j=2;j<=sqrt(i);j++) { if(i%j==0) { temp=1; break; } } if(temp==0) { count++; } if(count==n) { printf("%d\n",i); break; } } } int main() { int n,i; scanf("%d",&n); int arr[n]; for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { prime(arr[i]); } return 0; } this is my code...its working perfectly fine on codeblocks...ive used two more methods 1. where in the function ive run the loop till i/2 not sqrt(i) which is also correct but i presume will take more loops and therefor less efficient code. 2. i use pointer in main functon to avoid using arrays (not sure it makes difference but still) and offcourse this code up...none of them are working below time limit even though they give correct output...any suggestions? the segment you wrote to identify if a number is prime or not is much time killing, rather you may use this function like this: ---------------------------------------------------------------------------- bool is_prime(int n) { if(n < 2) return 0; else if(n == 2) return 1; else if(n%2 == 0) return 0; int root = sqrt(n); for(int i = 3; i <= root; i += 2) { if(n % i == 0) return 0; } return 1; } ---------------------------------------------------------------------------- Hints: 2 is the only even number which is prime. then you may store prime numbers in an array. finally the program seems to be like this... : --------------------------------------------------------------------------- #include <iostream> #include <cmath> using namespace std; bool is_prime(int n) { //if(n < 2) return 0; //else if(n == 2) return 1; //else if(n%2 == 0) return 0; // this lines aren't necessary for this program. int root = sqrt(n); for(int i = 3; i <= root; i += 2) { if(n % i == 0) return 0; } return 1; } int main() { int k, i, n, *num, prime_num[15001] = {0, 2}; // 2 is in 1st position. for(n = 3, i = 2; i <= 15000; n += 2) if(is_prime(n)) prime_num[i++] = n; cin >> k; num = new int [k]; for(i = 0; i < k; i++) cin >> num[i]; for(i = 0; i < k; i++) cout << prime_num[num[i]] << '\n'; delete [] num; return 0; } --------------------------------------------------------------------------------- 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 I want to submit the solution in C language. What language I should choose from the option and what is the error in following coding:- #include <stdio.h> int main() { double a,b; scanf("%lf %lf",&a,&b); printf("%lf",a+b); return 0; } Edited by author 31.03.2015 20:19 Use integral type (int, long, long long) or set precision in output format string printf("%0.0lf",a+b); Try this test: 1 0 0 0 1 1 1 0 Answer: You should rent the apartment #1 alone. Есть ли возможность сдавать задачи на языке Фортран Each crossroads in the input is the binary OR of its branches, so a 5 (binary 101) at some crossroads in the input means there are branches 4 (100) and 1 (001) there. As for the output, for each crossroads where there are no branches (each 0, let's call this crossroads P), repeat this: Find the Manhattan (vertical+horizontal) distance to the closest branch from C. In the description, this closest distance is 2. Next, find all branches that lie on this exact distance from C. In the description, these are branches with number 16, 8, and 4. The output at location P should reflect all types of branches at distance 2 by ORing these numbers together, and in the description we get 28 (binary 10000 | 1000 | 100). import java.util.Scanner; import java.io.*; public class HelloWorld { public static void main(String args[]) {
int a = 0; int total = 0; Scanner input = new Scanner(System.in); a = input.nextInt(); total = convertFunc(a); System.out.println(total); }
public static int convertFunc(int n){
int sum = 0; if(n > 0 && n <= 10000 ) { for(int i = 1 ; i <= n ; i++ ) { sum += i; } } else { for(int i = 1 ; i >= n ; i--) { sum += i; } } return sum; } } import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.ArrayList; import java.util.Scanner; public class Task1001 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Long> list = new ArrayList<Long>(); for (int i = 0; i < 4 ; i++) { long a = scanner.nextLong(); if (a >= 0 && a <= 10E18) list.add(a); } DecimalFormatSymbols symbol = new DecimalFormatSymbols(); symbol.setDecimalSeparator('.'); DecimalFormat f = new DecimalFormat("#,##0.0000", symbol); for (int i = list.size(); i > 0 ; i--) { double c = (Math.sqrt(list.get(i - 1))); System.out.println((f.format(c))); } } } I need help please!!! Edited by author 04.04.2015 17:02 Well, maybe because you are checking it only for 4 Long values, It's working only for 4 exact values, not less, not more. Edited by author 05.06.2015 13:04 Problem was solved. Edited by author 04.06.2015 18:02 Make sure s1 and s2 are within the limits of the problem. ER, thank you! Thanks to author for challenging task. Please give me some tests I don't use any graphs algorithms,but only DFS. If you try to solve this problem like me do: 1) break(remove) all cycles 2)Store elements of graph,witch belong to different component's of graph. 3) Connect one of them with others. (one - to every). Not every to every!!! Test 6 1000000 1000000 010000 100000 000100 001000 000001 000010 Edited by author 16.10.2009 21:29 Is this one of the solutions to the above given test? 2000000 00a0a0 000000 a00000 000000 a00000 000000 var a, b : array[1..1000] of integer; n, i, k : integer; begin readln(n); for i := 1 to n do begin read(a[i]); end; i := 2; k := 1; while i <= n + 2 do begin if a[i] = a[i - 1] then begin k := k + 1; i := i + 1; end else begin write(k,' ',a[i - 1],' '); i := i + 1; k := 1; end; end; end. Please explain, why my cod has WT7 and your not) var a:array[1..1000] of integer; i,n,k:integer; Begin readln(n); for i:=1 to n do begin read(a[i]); end; k:=1; i:=2; while i<=n+2 do begin if a[i]=a[i-1] then begin i:=i+1; k:=k+1; end else begin write(k,' ',a[i-1],' '); i:=i+1; k:=1; end; end; End. Edited by author 26.11.2012 23:44 Edited by author 26.11.2012 23:44 #include <bits/stdc++.h> using namespace std; #define maxx 500 #define pb push_back #define pii pair<int,int> queue<int>qu; vector<pii>vpii[maxx]; #define inf 0x7fffffff int X[]={+2,+2,-2,-2,+1,-1,-1,+1}; int Y[]={-1,+1,-1,+1,+2,+2,-2,-2}; int main() { int n,u; cin>>n; vector<int>v; for(int i=0;i<n;i++) { cin>>u; v.pb(u); } int cnt=1,l; for(int i=0;i<v.size();i++) { if(v[i]==v[i+1]) { cnt++; l=v[i+1]; } else{ l=v[i]; cout<<cnt<<" "<<l<<" "; cnt=1; } } return 0; } you need to write answer with two digits after point, so don't use cout! use printf("%.2f\n", ); and don't forget #include <cstdio> i've been finding this mistake for 2 days!! or u can use cout<<setprecision(2)<<fixed<<; Does someone have any idea what's test 3? i understand problem !!! Edited by author 03.06.2015 12:14 если жарить 3 бифштекса на сковороде, где умещается всего 2, то потребуется не 3, а 4 минуты, поскольку последний бифштекс на 2 части не разделить и не положить на сковороду одновременно обоими сторонами хотя нет, в три минуты тоже можно пожарить и разрезать ничего не надо Edited by author 13.05.2014 01:30 КАК?) Нумеруме бифштексы оригинально 1, 2 ,3. Жарим 1 и 2. Жарим вторую половину 1 и 3. Жарим вторые половины 2 и 3. Вуаля - вы программист! Так вот оказывается как котлеты надо жарить.... java public class MainClass { public static void main(String[] args) { System.out.println("A=\"'\";B='A=\"\";B=;?$(B,0,4)+A+$(B,4,4)+A+B+A+$(B,8,37)';?$(B,0,4)+A+$(B,4,4)+A+B+A+$(B,8,37)"); } } This is my code #include <iostream> #include <stdio.h> #include <math.h> using namespace std; long n,x[201],y[201]; int main() {double xa,xb,xc,ya,yb,yc,l,a,b,c,x,h; cin>>xa>>ya>>xb>>yb>>xc>>yc>>l; b=sqrt((xc-xa)*(xc-xa)+(yc-ya)*(yc-ya)); a=sqrt((xc-xb)*(xc-xb)+(yc-yb)*(yc-yb)); c=sqrt((xa-xb)*(xa-xb)+(ya-yb)*(ya-yb)); if(c!=0){ x=(c*c+b*b-a*a)/(2.0*c); h=sqrt(b*b-x*x); if(b!=0&&a!=0){ if((b*b+c*c-a*a)/(2.0*b*c)<=0) h=b; if((a*a+c*c-b*b)/(2.0*a*c)<=0) h=a;}
if(h<=l) h=0; else h=h-l;} else h=a; printf("%0.2f\n",h);
if(a>=b) {if(a<=l) h=0; else h=a-l;} else {if(b<=l) h=0; else h=b-l;} printf("%0.2f\n",h);
return 0; }
Please give me some test Попробуйте 2 2 2 2 3 3 1 Ответ: 0.41 0.41 My program gives the correct output for the test you gave, but still getting wrong answer on test#18. Any suggestions? Edited by author 11.06.2015 18:48 Edited by author 11.06.2015 18:48 pls give me some test data, dont know what s wrong with code. greetings. PLEASE NOTE what N and K represent;N represents colums and k represents rows. My code was wrong because of this;Hope you can get help. Thanks!!! I made such mistake :( |
|