Page 3 #include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; void bubbleSort(int array[], int col){ int temp=0; for (int i=1; i<col ; i++){ for (int j=0; j<col-i; j++){ if (array [j]>array [j+1]){ temp=array[j]; array [j]=array [j+1]; array [j+1]=temp; } } } } int _tmain(int argc, _TCHAR* argv[]) { int k; int max; int sum; sum = 0; max = 0; cin >> k; int *p = new int[k-1]; int i; for (i = 0; i < k; i++) { cin >> p[i]; }
bubbleSort(p, k-1); k=k/2+1; for (i=0; i<k; i++) { sum = sum + p[i]/2+1; } cout << sum; _getch(); return 0; } kgxvh8-ov2fjo kgxvh8-ov2fjo(1) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory What's wrong? There is no problem in Visual Studio 12 :( [code deleted] Edited by moderator 20.11.2019 00:02 I understood the solution. But I didn't understand the logic, why 1 is added before dividing the number? Thank you. Because to get vote from i group you need to have more than a half votes. For example, If 7 you have 7 guys in a group, you need to have 4 votes. 7/2 = 3 3+1 = 4 Why you sorted the array?. Is there any need of it?. What is wrong in my code? Please, give me a hint. var k,i,min,max,voices,sum:integer; ka:array [1..101] of integer; begin readln(k); for i:= 1 to k do begin read(ka[i]); if i=1 then begin min:=ka[i]; max:=ka[i]; end else begin if ka[i]<min then min:=ka[i]; if ka[i]>max then max:=ka[i]; end; end;
if min<>max then begin for i:=1 to k do begin if ka[i]=min then sum:=sum+ka[i]; end; voices:=sum div 2 +1; end else begin for i:=1 to k do sum:=sum+ka[i]; voices:=sum div 2 +1; end;
writeln(voices); end. [code deleted] Edited by moderator 20.11.2019 00:12 I have the same problem for example a write: 5 5 5 5 5 5 my program gives me 9 is it true? Edited by author 06.11.2012 21:15 Edited by author 06.11.2012 21:15 Yes, it's true. and if I write: 4 4 4 4 4 I get 9 too and if I write 5 3 7 6 6 4 I get 9 too i solved problem. i changed from real to word and all OK wht do u mean by real to word nd did ur ques get accept? this is my code and its wrking fine on my pc import java.io.*; import java.util.Scanner; public class Democracy { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
Scanner sc = new Scanner (System.in); System.out.println("enter no of groups"); int n = sc.nextInt();
System.out.println("enter group members"); //String s = sc.next(); //String[] num = s.split(","); String[] num = sc.next().split(",");
int[] num1 = new int[num.length]; int x =0; for(String a: num){ num1[x] = Integer.parseInt(a); //System.out.println(a); x++; }
int len = n/2+1; int[] min = new int[len];
for(int i = 0; i <len; i++ ){ min[i] = num1[i]; for(int j= i+1; j<num1.length; j++ ){ if(min[i] > num1[j]){ min[i] = num1[j]; } } }
int vote = 0; for(int i =0; i<len; i++){ vote = vote+ min[i]/2+1; }
System.out.println(vote);
} } var t: real; n, k, i,j: integer; a:array [1..101] of integer; begin readln(n); for i := 1 to n do begin read(a[i]); end; for i:=1 to n do begin for j:=1 to n do begin if a[i]<a[j] then begin swap(a[i],a[j]); end; end; end;
for i:=1 to (n div 2)+1 do begin t:=t+((a[i] div 2)+1); end;
write(t); end. Pascal hasn't function swap. You should to write it yourself [code deleted] Edited by author 10.09.2012 19:45 Edited by moderator 20.11.2019 00:11 OMG,how it was possible to come up with that? you should cout the answer only, without any "The minmum no of voters needed is" import java.util.Scanner; public class Main { public static void main(String args[]) { try { Scanner in = new Scanner(System.in);
int[] group = new int[in.nextInt()]; for(int i=0; i<group.length; i++) { group[i] = in.nextInt(); } int per; for(int i=0; i<group.length - 1; i++) { for(int j=1; j<group.length; j++) { if (group[i] >= group[j]) { per = group[i]; group[i] = group[j]; group[j] = per; } } } int sum = 0; for(int i=0; i<group.length/2 + 1; i++) { sum += group[i]/2 + 1; } System.out.println(sum); in.close(); } catch(Exception e) { System.out.println("Error"); } } } [code deleted] Edited by moderator 20.11.2019 00:14 Because you need print only answer, without other text like "Number of groups: " using System; class Program { static void Main() { int t = int.Parse(Console.ReadLine()); string[] s = Console.ReadLine().Split(' '); Array.Sort(s); int r = 0; for (int i = 0; i < (t / 2)+1; i++) { r += ((int.Parse(s[i]) / 2) +1); } Console.WriteLine(r); } } Edited by author 16.06.2012 19:10 Edited by author 16.06.2012 19:11 You must input all the array first, and only then work with it Edited by author 10.12.2016 03:26 program demokratiya; const max=9999; var k,i,j,N:integer; p: array [1..max] of integer; x,sum:integer; begin readln(k); N:=0; if (k<=101) and (k>=1) then begin for i:=1 to k do begin readln(p[i]); if (p[i] mod 2 = 0) or (p[i]>9999) then exit; N:=N+p[i]; end; if N>9999 then exit; for i:=1 to k-1 do for j:=i downto 1 do begin if p[j]>p[j+1] then begin x:=p[j]; p[j]:=p[j+1]; p[j+1]:=x; end; end; k:=trunc(k/2)+1; for i:=1 to k do begin p[i]:=trunc(p[i]/2)+1; writeln(p[i],' '); sum:=sum+p[i]; end; writeln(sum,' '); end; end. Can anybody explain, what dates are insertrd in test N3, i made my own test everything is working, but test N3 said what wrong answer #include "stdafx.h" #include<stdio.h> #include<conio.h> //#include<math.h> void main() { int k, A[102], t, B[52],i,l,minp,min; scanf("%d", &k); for (i=0; i<k; i++) scanf("%d",&A[i]); t=k/2+1; for (l=0; l<t; l++) { min=9999; for (i=0; i<k; i++) { if (A[i]<min) { min=A[i]; minp=i; B[l]=min; } if (i==(k-1)) A[minp]=9999; } } k=0; for (l=0; l<t; l++) k+=((B[l]/2)+1); printf("%d",k); getch(); } try test 1 9999 in this test array B[52] in your program will contain random numbers and so is answer. Your algo is true but not effective ,use qsort for example program f; var x,m,i,amount,s,j,g,k:integer; a: array [1..10000] of integer; begin read (m); for i:=1 to m do read (a[i]); for i:=1 to m do begin for j:=1 to m-1 do for g:=1 to m-j do if a[i]>a[i+1] then begin k:=a[i]; a[i]:=a[i+1]; a[i+1]:=k; end; end; amount:=(m div 2) +1; for i:=1 to amount do s:=s+(a[i]div 2)+1; write (s); end. Incorrect sort algorithm. Variable 's' not initialized. It work in dev c++: #include <iostream> using namespace std; int main(){ int a; cin>>a; int b[a]; int tmp; int q=0; for(int i=0; i<a; i++){ cin>>b[i]; } for(int i = 0; i < a - 1; ++i) // i - íîìåð ïðîõîäà { for(int j = 0; j < a - 1; ++j) // âíóòðåííèé öèêë ïðîõîäà { if (b[j + 1] < b[j]) { tmp = b[j + 1]; b[j + 1] = b[j]; b[j] = tmp; } } } if (a%2==0){
for (int z=0; z<(a/2)+1;z++){ if(b[z]%2==0) q+=(b[z]/2)+1; else q+=(b[z]+1)/2;
} } else { int r=(a+1)/2; for (int z=0; z<r;z++){ if(b[z]%2==0) q+=(b[z]/2)+1; else q+=(b[z]+1)/2;
} }
cout<<q; return 0; } 1st variant: [code deleted] Result: WA My 2nd variant: [code deleted] Result: accepted. The difference is in "For" cycle which I separated with two blank lines. The rest part of code is the same in both variants. I can't understand why my 1st variant wasn't accepted. In my opinion there is no difference between these examples: a = b/2 + 1; and a = (b + 1)/2; if b > 0, is odd and has int type. Where is a mistake? Edited by author 01.02.2012 01:27 Edited by author 02.02.2012 00:24 Edited by moderator 20.11.2019 00:17 The first code gets AC too. Try to submit it. There was another code (with bug) in your submit № 4100318. You are right. Thanks. )) I hurried and was inattentive. But how can you see my submits? )) I tried to open someone's solutions but I couldn't. Why? Sandro is an administrator of this site. #include <iostream> #include <algorithm> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cstring> #define fi "Democracy in Danger.in" #define fo "Democracy in Danger.out" using namespace std; int m; int k; long long sum; int a[102]; void init_file(){ freopen(fi, "r", stdin); freopen(fo, "w", stdout); }
void readdata(){ scanf("%d", &k); for (int i = 1; i <= k; i ++){ scanf("%d", a + i); } // printf("%d\n", a[1]); } int half(int k){ return ((k >> 1) + 1); } void work(){ sum = 0; sort(a, a + k + 1); m = half(k); for (int i = 1; i <= m; i++){ sum += half(a[m]); } printf("%d", sum); }
int main(){ //init_file(); readdata(); work(); return 0; } My code is above. Could u Please tell me why I had a WA in #5. 3q very much Send me the answer or I'll kick you! import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class DemocracyInDanger { private static int sum = 0; private static int i=0; public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int amount = Integer.parseInt(in.readLine()); if(amount>=1 && amount<=101 && amount%2!=0){ int[] party = new int[amount]; Scanner scanner = new Scanner(System.in); while(i<amount){ party[i]=scanner.nextInt(); if(party[i]>0 && party[i]%2!=0){ sum+=party[i]; }else throw new IOException(); i++; } if (sum<=9999){ sum=0; Arrays.sort(party); for (int i = 0; i <=amount/2; i++) { sum +=party[i]/2+1; } System.out.println(sum); } }
} catch (Exception e) { } } } Tell me please how you have get timing 0.001 ? It mustn't be a code, may be just method or algorithm. Thanks I think it's impossible to get time less than 0,015 sec at new testing system (since approx. end of 2008). Look at problem 1000 - simple operation, but you can't now get time less than 0.015 neither on C++ nor on Pascal. Furthermore, step between time measurment is about 0,016 (so you can get 0,015 or 0,031 but nothing between) And furthermore, when you run you some hard solution sequental some times, it will get significant different time result. For example, my Pascal AC solution for 1001 sended a few of times takes 0.078, 0.093 or 0.109 sec I think it because mistake in time-measurment method. I think they using a system-clock (RTC) that renew after significant time. So, if testing system is hard loaded (for example testing many authors in parallel) the testing will take more time relatively ro Real-Time Clock, than when system is idle (at early morning, for example). So, testing system is incorrect and you can't reach time less than 0.015 until realisation of system will be changed Excuse my English I understood, thank you ) Your English is OK My code runtime is 0.001. If you use bitwise operation your code will get AC in 0.001. Here is my code. If you want you can learn. [code deleted] Edited by moderator 20.11.2019 00:08 program Project2; {$APPTYPE CONSOLE} uses SysUtils; var k,i,j,min:byte; t:Word; nas:array of Word; begin read(k); if k = 1 then begin read(t); write((t+1)div 2); exit; end; SetLength(nas,k+1); for i:=1 to k do read(nas[i]); for i:=1 to k-1 do begin min:=i; for j:=i+1 to k do if nas[j] < nas[min] then min:=j; t:=nas[i]; nas[i]:=nas[min]; nas[min]:=t; end; write((nas[1]+1) div 2 + (nas[2]+1)div 2); end. |
|