n, m = map(int, input().split()) a = [] b = [1] c = [] for i in range(m): f = int(input()) a.append(f) a = sorted(a) f = 0 for i in range(1, len(a)): if a[i] == a[i - 1]: b[f] += 1 elif a[i] != a[i - 1]: b.append(1) f += 1 for i in range(len(b)): b[i] = float(b[i] / m * 100) for i in range(len(b)): c.append('%.2f' % b[i] + '%') for i in range(n): try: print(c[i]) except: print('0.00%') #include <stdio.h> int main () { int n, m; scanf("%d %d", &n, &m); int ara[n+1]{0}, temp; double result{0}; for (int i = 1; i <= m; i++) { scanf("%d", &temp); ara[temp]++; temp = 0; } for (int i = 1; i <= n; i++) { if(m == 0) { printf("0.00%\n"); continue; } result = (100.0 * ara[i] / m); printf("%2.2lf", result); printf("%\n"); } return 0; } My code: n,m = map(int, input().split()) dct = dict() for i in range(m): a = input() if a not in dct: dct[a] = 1 else: dct[a] += 1 for i in dct: print("{0:.2f}%".format((dct[i]/m)*100)) Where is mistake? Edited by author 13.09.2021 21:32 #include<iostream> #include<iomanip> #include<math.h> using namespace std; double a[10001]; int main() { int l=0,i,n,k; double x,m; cin>>n>>m; for(i=0;i<m;i++) { cin>>k; a[k]++; } for(i=1;i<=n;i++) { x=a[i]/m; x*=100; if(ceil(x)==floor(x))cout<<x<<".00%"<<endl; else cout<<setprecision(4)<<x<<'%'<<endl; } return 0; } Edited by author 09.04.2008 22:50 I have this Problem too. plz help, my program is been written in JAVA. Edited by author 13.10.2011 16:36 So, I have this problem too. What is the rigth solution? --- I found the answer, use Console.WriteLine("{0:f2}%", t); Edited by author 25.10.2012 19:37 Edited by author 25.10.2012 19:37 Try these inputs: 2 11 1 2 2 2 2 2 2 2 2 2 2 I change this >> first multiply 100 and then divide m . and it works. object Election1263 extends App { val Array(n, m) = scala.io.StdIn.readLine().split(" ").map(_.toInt) val list = (0 to m - 1).foldLeft(List.empty[Int]){case (acc, _) => scala.io.StdIn.readLine().split("\n").map(_.toInt).toList match { case head :: Nil => acc :+ head case _ => acc } } val electionList = list.groupBy(identity).foldLeft(List.empty[Int]) {(s, e) => s :+ e._2.size } def getPersent(list: List[Int], list1: List[Double]) : List[Double] = list match { case head :: tail => getPersent(tail, list1 :+ head * 100.0 / m) case head :: Nil => getPersent(Nil, list1 :+ head * 100.0 / m) case _ => list1 } val result = getPersent(electionList, List()) result.foreach(e => println("%.2f".format(e) + "%") ) } import java.util.*; public class j { public static void main(String[] args){ Scanner input = new Scanner(System.in); int bin,sum = 0; int n=input.nextInt(); int m=input.nextInt(); int [] mas = new int [n]; for(int i = 0;i<m;i++){ bin = input.nextInt(); sum += 1; mas[bin-1] = mas[bin-1] + 1; } for(int i = 0;i<n;i++){ System.out.println(String.format("%.2f",mas[i]/(sum*0.01))+"%"); } } } I don’t understand what is the mistake here. Help pls string[] val = Console.ReadLine().Split(' '); double[] k = new double[10000]; for(int i = 0; i < Convert.ToInt32(val[1]); i++) { string s = Console.ReadLine(); k[Convert.ToInt32(s) - 1]++; } double swap = 0; for(int i = 0; i < k.Length-1; i++) { for(int j = i + 1; j < k.Length - i - 1; j++) { if (k[j+1] > k[j]) { swap = k[j+1]; k[j+1] = k[j]; k[j] = swap; } } } for (int i = 0; i < Convert.ToInt32(val[0]); i++) {
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:N2}", (100 * k[i]) / Convert.ToDouble(val[1]))+"%"); }
import java.io.PrintWriter; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; public class Task_1263 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String str = in.nextLine(); int candidateCount = Integer.valueOf(str.substring(0, 1)); int votersCount = Integer.valueOf(str.substring(2)); int[] votersForCandidates = new int[candidateCount]; double[] votersPercents = new double[candidateCount]; int voter; for (int i = 0; i < votersCount; i++) { voter = Integer.valueOf(in.nextLine()); votersForCandidates[voter - 1]++; } for (int i = 0; i < candidateCount; i++) { votersPercents[i] = new BigDecimal((double) 100 * votersForCandidates[i] / votersCount).setScale(2, RoundingMode.HALF_UP).doubleValue(); } for (int i = 0; i < candidateCount; i++) { out.printf("%.2f%%%n", votersPercents[i]); } out.flush(); } } Because of the Memory limit: which no more than 64 MB. votersPercents array is useless and should be removed. But the main error is you reading candidateCount in the wrong way. str.substring(0, 1) - how many digits can you read? What max candidateCount is? You use Scanner class. It has method nextInt() - you can just use it, you don't need to read strings and split them into numbers manually. Could anybody give me some tricky tests? This problem seems to be trivial but I got WA. I guess it's all about format of result. try this: 3 6 1 2 1 2 1 1 Thank you! We should pay attention to the case when there is any candidates that no one votes for him or her Thanks! Most helpful answer here! using System; namespace Training { class Program { static void Main(string[] args) { string[] n = Console.ReadLine().Split(' '); int N = Convert.ToInt32(n[0]); int M = Convert.ToInt32(n[1]); double cnt = 1; int arr = 0; int[] ans = new int[M]; double[] vs = new double[N]; for (int i = 0; i < M; i++) ans[i] = Convert.ToInt32(Console.ReadLine()); Array.Sort(ans); Array.Resize(ref ans, ans.Length+1); Console.WriteLine(); for (int j = 0; j < M; j++) { if (ans[j] == ans[j + 1]) cnt++; else { double x = Math.Round(cnt / M * 100, 2); if ((int)x == x) Console.WriteLine(x + ".00%"); else Console.WriteLine(x + "%"); arr++; cnt = 1; } } if (arr < N) for (int j = 0; j < N - arr; j++) Console.WriteLine("0.00%"); } } } int main() { int n , m; cin >> n >> m; int *counter = new int [n]; for ( int i = 0 ; i < m ; i++ ) { int temp; cin >> temp; counter[temp-1]++; } for ( int i = 0 ; i < n ; i++ ) { float result = (float)counter[i] * (float)100 / (float)m; printf ("%.2f%%\n" , result); } delete[] counter; return 0; } (WA On Test 1) Where is "cout"? printf() also does the same work as cout<< I think the problem is from using int*counter.. Try using vector<int>counter(n); instead.. It worked fine when i tested it with the rest of your code m = [] g = [] d, b = map(int, input().split(' ')) for i in range(1, b+1): n = int(input()) m.append(n) for i in range(1, d+1): g.append(i) for i in range(d): f = 0 for y in range(b): if m[y] == g[i]: f += 1/b*100 f = round(f, 2) a = str(f) if len(a) == 4: print(a+'0%') else: print(a+'%') при 100% проголосовавших выводится 100.0%. Edited by author 30.12.2018 18:16 Попробуй вариант где ответ - 100% n, m = map(int,input().split(' ')) voices = [] p = ['p'] h = 0 for i in range(m): voices.append(int(input()))
for i in range(n): p.append(0) for i in voices: for c in range(n+1): if i == c: p[c] += 1
for i in range(1, len(p)): h = p[i] / m * 100 p[i] = str(round(h, 2)) if len(p[i]) == 4: p[i] += '0' print(p[i] + '%') Help. what's wrong n, m = map(int,input().split(' ')) voices = [] p = ['p'] h = 0 for i in range(m): voices.append(int(input())) for i in range(n): p.append(0) for i in voices: for c in range(n+1): if i == c: p[c] += 1 for i in range(1, len(p)): h = p[i] / m * 100 print(f'{h:.2f}%') This is your code that prints the right answer. Python 3 uses BANKER'S rounding while the problem asks you to the MATHEMATICAL rounding. I used 'f-string' to perform MATHEMATICAL rounding. Anyways, the solution is not fast enough - try to come up with some speed-ups. import math nm = input().split() n = int(nm[0]) m = int(nm[1]) res = set() result = [] for i in range(m): x = int(input()) if x not in res: res.add(x) result.append(x) answer = [] for i in res: answer.append(result.count(i)) while n - len(answer) > 0: answer.append(0) for i in answer: v = str(round(i*100/m, 2)) if v[-2] + v[-1] == '.0': print(str(round(i*100/m, 2)) + '0%') else: print(str(round(i*100/m, 2)) + '%')
Edited by author 24.02.2019 01:39 Edited by author 09.03.2019 03:19 n, m = [int(x) for x in input().split()] x = 0 k = [0]*(n) for i in range(m): x = int(input()) k[x-1] = k[x-1]+1 for u in range(n): print( "%.2f" % (k[u]*100/ m), "%", sep="" ) import numpy n,m=input().split() a=[] for i in range(int(m)): a.append(int(input())) b=numpy.zeros(int(n)) for i in range(int(n)): b[i]=a.count(i+1)/int(m)*100 print('%.2f' % b[i]+'%') Answer from system:Restricted function Help! What's wrong with my code? #include <bits/stdc++.h> #define forn( i, n ) for( int i = 0; i < n; i ++ ) using namespace std; int main() { int qq[10000]; int k, l; double m , n; l = 0; cin >> k >> n; forn( i , n) cin >> qq[i]; forn( i, k+1 ){ forn( j , n) if (qq[j] == i) l = l+1; if ( i != 0 ) { m = (l / n * 100); l = (int) round(m*100)/100; if ( l - m == 0) cout << round(m*100)/100 << ".00%" << endl; else cout << round(m*100)/100 << "%" << endl; l = 0; } } return 0; } a=input().split() t=0 for i in range(len(a)): c=a[t] v=int(c) a[t]=v t=t+1 e=[0] s=[0] z=[0] q=[0] y=1 for i in range(a[1]): d=int(input()) s.append(d) s.remove(s[0]) for i in range(0,a[0]): w=s.count(y) z.append(w) w=0 y=y+1 z.remove(z[0]) t=0 for i in range(len(z)): c=z[t] v=int(c) z[t]=v t=t+1 t=0 c=0 for i in range(len(z)): c=z[t]/a[1] c=c*100 q.append(c) t=t+1 q.remove(q[0]) t=0 c=0 for i in range (len(q)): c=round(q[t],2) e.append(c) t=t+1 e.remove(e[0]) t=0 for i in range (len(e)): print(e[t],"%") t=t+1 Hi, folks! Can anyone give me the source code in C# of this task (mine is already AC) when the timing is less than 0.1 sec? Or can anyone give me the key notes how it can be decreased in almost ten times? Thanks in advance! I presume your sol is O(NM), right? Think a bit. There is O(N+M) solution which definitely has time better than 0.1 sec. 1) Be sure that your algorithm is O(N+M) 2) When calculating percents you should not do: value * 100 / number inside cycle, it is better to calculate "100.0 / number" one time before cycle and then just perfom multiplying. 3) I used float for keeping values, not int. I think it saves time of casting from int to float. When I used 2 and 3 my time for c# decreased from 0.109 to 0.046. #include <iostream> using namespace std; int main() { int cand, ch; cin >> cand >> ch; int* Ch = new int[ch]; double* Cand = new double[cand]; for (int i = 0; i < cand; i++) { Cand[i] = 0; } for (int i = 0; i < ch; i++) cin >> Ch[i]; for (int i = 0; i < ch; i++) { Cand[Ch[i]-1] += 1; } for (int i = 0; i < cand; i++) { printf("%.2f%%\n", (Cand[i]/6.0)*100.0); } } +++++++++++ Why this isn't work? What is the hardcoded value "6" in your code? |
|