Instead of using getline(cin, str), use two different string such as string s1, s2. Then compare s1! I noticed a lot of people complain about WA2. Just wanted to share my experience, since I got WA2 several times. Don't forget that after you read the number, you haven't read the newline after it. So cin.getchar + cin.ignore and getline(cin, ...) get that exact newline. #include <iostream> #include <string> #include <vector> #include <set> const std::string &solution(const std::vector<std::string> &penguins); int main() { int n; std::cin >> n; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::vector<std::string> penguins; penguins.reserve(n); for (int i = 0; i < n; ++i) { std::string penguin; std::getline(std::cin, penguin); penguins.emplace_back(std::move(penguin)); } std::cout << solution(penguins) << std::endl; return 0; } const std::string &solution(const std::vector<std::string> &penguins) { std::set<std::string> penguinSet(penguins.begin(), penguins.end()); auto setIterator = penguinSet.begin(); std::set<std::string>::iterator mostNumerous; int maxCount = 0; while (setIterator != penguinSet.end()) { int count = 0; for (const auto &penguin : penguins) { if (*setIterator == penguin) { count++; } } if (count > maxCount) { maxCount = count; mostNumerous = setIterator; } setIterator++; } return *mostNumerous; } I tested locally and it works... n = int(input()) d = {} for i in range(n): s = input() if s not in d: d[s] = 0 d[s] += 1 ans = sorted(d.items(), key=lambda kv: kv[1])[-1][0] print(ans) #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> int main() { int N; int i; int emp = 0, mac = 0, lit = 0; scanf("%i",&N); char **str = (char**)malloc(N * sizeof(char)); for(i = 0; i < N; i++) str[i] = (char*)malloc(20); fflush(stdin); for(i = 0; i < N; i++) gets(str[i]); for(i = 0; i < N; i++) { if(strcmp(str[i], "Emperor Penguin") == 0) emp++; if(strcmp(str[i], "Macaroni Penguin") == 0) mac++; if(strcmp(str[i], "Little Penguin") == 0) lit++; } if(emp > mac && emp > lit) printf("%s\n", "Emperor Penguin"); if(mac > emp && mac > lit) printf("%s\n", "Macaroni Penguin"); if(lit > mac && lit > emp) printf("%s\n", "Little Penguin"); for(i = 0; i < N; i++) free(str[i]); free (str); return 0; } What is the WA#5?Какой WA#5? my code #include <bits/stdc++.h> using namespace std; int main() { int n, c = 0, s = 0, v = 0, maxn = 0; cin >> n; string a, b; for(int i = 1; i <= n; i++){ cin >> a ; for(int i = 0; i <= a.size(); i++){ if(a[0] == 'E' && a[1] == 'm' && a[2] == 'p' && a[3] == 'e' ){ c++; break; } else if( a[0] == 'M' && a[1] == 'a' && a[2] == 'c' && a[3] == 'a'){ s++; break; } else if(a[0] == 'L' && a[1] == 'i ' && a[2] == 't' && a[3] == 't' ){ v++; break; } } cin >> b; } if(c > s && c > v){ cout << "Emperor Penguin"; } else if(s > c && s > v){ cout << "Macaroni Penguin"; } else if(v > c && v > s){ cout << "Little Penguin"; } return 0; } why incorrect #include <string> #include <iostream> #include <map> using namespace std; int main() { int n; scanf("%d\n",&n); map<char,int> peng; peng['E']=0; peng['M']=0; peng['L']=0;
string tmp="";
for(int i=0;i<n;i++) { getline(cin,tmp); if(tmp[0]=='E' || tmp[0]=='M' || tmp[0]=='L') peng[tmp[0]]++; } map<char,int>::reverse_iterator rit = peng.rbegin(); switch(rit->first) { case 'E': cout<<"Emperor Penguin"<<endl;break; case 'M': cout<<"Macaroni Penguin"<<endl;break; case 'L': cout<<"Little Penguin"<<endl; } return 0; } Enjoy: var n:1..1000; i,e,l,m:1..1000; s:string[1]; begin readln(n); for i:=1 to n do begin readln(s); if s[1]='E' then inc(e); if s[1]='L' then inc(l); if s[1]='M' then inc(m); end; if (e>=l) and (e>=m) then writeln('Emperor Penguin'); if (l>=e) and (l>=m) then writeln('Little Penguin'); if (m>=e) and (m>=l) then writeln('Macaroni Penguin'); end. Good luck! My Solution: #include <iostream> #include <string> #include <vector> #include <set> #include <queue> #include <map> #include <stack> #include <algorithm> #include <bitset> #include <cstring> #include <cmath> #include <cstdlib> #include <cstdio> #include <iomanip> #define F first #define S second #define ll long long #define len length() #define sqr(x) x*x #define pb push_back #define mp make_pair #define sz(x) ((int) (x).size()) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define bp(x) __builtin_popcount(x) #define INF numeric_limits<long long int>::max() #define frp freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++) const int maxn = (int)1e6; const int mod = (int)1e9 + 7; using namespace std;
int n; string s,ss; int mx=-1; int e,l,m; main(){ scanf("%d",&n); while(n--){ cin>>s>>ss; for(int i=0; i < s.len; i++){ switch(s[i]){ case 'E':e++;break; case 'L':l++;break; case 'M':m++;break; } } } mx=max(mx,max(e,max(l,m))); if(mx==e) puts("Emperor Penguin"); else if(mx==l) puts("Little Penguin"); else if(mx==m) puts("Macaroni Penguin");
return 0; } #include<stdio.h> #include <string.h> // n is not required at all, i am leaving it. int main(){ int n, ce, cm, cl, max = 0; char c; scanf ("%d", &n); while ((c = getchar())!= EOF){ if (c == 'E') ce++; else if (c == 'M') cm++; else if (c == 'L') cl++; } /* Emperor Penguin Macaroni Penguin Little Penguin */ max = (ce > cm) ? ((ce > cl) ? ce : cl) : ((cm > cl) ? cm : cl); if (max == ce) printf("Emperor Penguin"); else if (max == cm) printf ("Macaroni Penguin"); else printf ("Little Penguin"); return 0; } Hey, I'm using Python and Counter: from collections import Counter print(max(Counter([input() for i in range(int(input()))]))) Works perfectly, but Test 5... Edited by author 12.08.2014 17:55 It's not difficult to get AC with analyzing of first symbol in input string. I did make it. But how do I can do this task with Counter? #include <iostream> using namespace std; int main () { short int i, n1, n2, n3; cin >> i; cin.sync(); char *name1, *name2, *name3, *name = new char[20]; n1 = n2 = n3 = 0; name1 = "Emperor Penguin"; name2 = "Macaroni Penguin"; name3 = "Little Penguin"; while (i > 0) { cin.getline(name,19); cin.sync(); switch(name[0]) { case 'E': n1++; break; case 'M': n2++; break; case 'L': n3++; break; } i--; } if (n1 > n2 && n1 > n3) printf("%s",name1); else if (n2 > n1 && n2 > n3) printf("%s",name2); else if(n3 > n1 && n3 > n2) printf("%s",name3); system("pause"); return 0; } It works on my laptop. I have Pascal ABC. var A:array[1..1000]of string; B:array[1..3]of integer; c,d:integer; begin readln(c); for d:=1 to c do begin readln(A[d]); if A[d]='Emperor Penguin'then B[1]+=1; if A[d]='Little Penguin'then B[2]+=1; if A[d]='Macaroni Penguin'then B[3]+=1; end; d:=max(max(B[1],B[2]),B[3]); if d=B[1]then write('Emperor Penguin'); if d=B[2]then write('Little Penguin'); if d=B[3]then write('Macaroni Penguin'); end. function max(x,y:integer):integer; begin if x>=y then max:=x else max:=y; end; Thanks. I didn't know that FreePascal doesn't know this function max(). Edited by author 23.08.2013 10:58 #include<stdio.h> #include<string.h> int comp(char *pen,char *peng) { int i; for(i=0;i<strlen(pen);i++) if(pen[i]!=peng[i]) return 0; return 1; } int main() { int i,a=0,b=0,c=0,n; char pen[3][10]={"Emperor","Little","Macaroni"},peng[20]; scanf("%d",&n); for(i=0;i<n;i++) { fflush(stdin); gets(peng); if(comp(pen[0],peng)) ++a; if(comp(pen[1],peng)) ++b; if(comp(pen[2],peng)) ++c; } if(a>b&&a>c) printf("Emperor Penguin"); if(b>a&&b>c) printf("Little Penguin"); if(c>b&&c>a) printf("Macaroni Penguin"); return 0; } figured out, you need to clear the buffer perverted by scanf ("% * [^ \ n] "); and used to enter the scanf ("% s ", peng); import java.util.*; public class Penguins1585 { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); int a=0,b=0,c=0; for(int i=0; i<n; i++){ String s = in.nextLine(); String s1=s.substring(0,1); if(s1.equals("E"))a++; else if(s1.equals("M"))b++; else c++; }
if(a>=b && a>=c)System.out.print("Emperor Penguin"); else if(b>=a && b>=c) System.out.print("Macaroni Penguin"); else System.out.print("Little Penguin");
} } var n,i,e,l,m,max:integer; s,maxw:string; begin readln(n); e:=0; l:=0; m:=0; for i:=1 to n do begin readln(s); if s[1]='E' then e:=e+1; if s[1]='L' then l:=l+1; if s[1]='M' then m:=m+1; end; if e>l then begin max:=e; maxw:='Emperor Penguin'; end else begin max:=l; maxw:='Little Penguin'; end; if m>max then begin max:=m; maxw:='Macaroni Penguin'; end; writeln(maxw); end. I see that many people have got execution time as 0.001 dunno how to achieve such a short execution time...any insights please... PLOBLEM 1: I have seen the topic "c WA why?",and I have a problem:Why the for cycle's range should be 1 to 2*n but not 1 to n?Help me,I don't know. PLOBLEM 2: my program: #include<stdio.h> #include<string.h> #include<stdlib.h> int n,max=0,count[3],a; char s[30],ans,max1; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d",&n); count[1]=0;count[2]=0;count[3]=0; for(int i=1;i<=2*n;i++) { scanf("%s",s); if(s[0]=='E') { count[1]++; if(count[1]>max) {max=count[1];max1='E';} } else if(s[0]=='L') { count[2]++; if(count[2]>max) {max=count[2];max1='L';} } else if(s[0]=='M') { count[3]++; if(count[3]>max) {max=count[3];max1='M';} } } if(max1=='E') printf("Emperor Penguin"); else if(max1=='L') printf("Little Penguin"); else if(max1=='M') printf("Macaroni Penguin"); return 0; } It can be right in my conputer(printf"Macaroni Penguin"),but it's WRONG#1 in the test. And if I change for cycle's range into 1 to n,it's WRONG,and printf"Emperor Penguin" I don't know. SO,please help me,thank you! now,this problem can pass #6 point,but W7 #include<stdio.h> #include<string.h> #include<stdlib.h> int n,max=0,count[3],a; char s[30],ans,max1,e=0,l=0,m=0; int main() { scanf("%d\n",&n); count[1]=1;count[2]=1;count[3]=1; for(int i=1;i<=2*n;i++) { scanf("%s",s); if(s[0]=='E') e++; else if(s[0]=='L') l++; else if(s[0]=='M') m++; } if(e>l&&e>m) printf("Emperor Penguin"); if(l>e&&l>m) printf("Little Penguin"); if(m>e&&m>l) printf("Macaroni Penguin");
//system("pause"); return 0; } Edited by author 24.02.2013 07:27 Edited by author 24.02.2013 07:42 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace timusPin { class Program { static void Main(string[] args) { int max; int e=0, m=0, l=0; max = int.Parse(Console.ReadLine()); string[] arr = new string[max]; for (int v = 0;v <= max -1 ;v++) { arr[Convert.ToInt32(v)] = (Console.ReadLine()); } for (int v = 0;v <= max -1 ;v++) { if (arr[v] == "Emperor Penguin") { e++; } if (arr[v] == "Macaroni Penguin") { m++; } if (arr[v] == "Little Penguin") { l++; } } if (e > m && e > l) { Console.WriteLine("Emperor Penguin"); } if (m > e && m > l) { Console.WriteLine("Macaroni Penguin"); } if (l > e && l > m) { Console.WriteLine("Macaroni Penguin"); } // Console.ReadKey(); } } } WHY Wrong Answer???? if (l > e && l > m) { Console.WriteLine("Little Penguin"); } |
|