Moderator: Board moderators
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct S
{
char cand[100];
char party[100];
int vote;
} list[20000];
int cmpf(const void *a, const void *b)
{
S *s1 = (S*)a;
S *s2 = (S*)b;
return strcmp(s1->cand,s2->cand);
}
int main()
{
char s[500];
int i,j,n,c,case_no,m,l,h,r,max,t,M;
/* freopen("C.txt","r",stdin); */
scanf("%d\n",&case_no);
for(c=0; c<case_no; c++)
{
scanf("%d\n",&n);
for(i=0; i<n; i++)
{
gets(list[i].cand);
gets(list[i].party);
}
for(i=0; i<n; i++)
{
list[i].vote = 0;
}
qsort((void*)list,n,sizeof(list[0]),cmpf);
scanf("%d\n",&M);
for(i=0; i<M; i++)
{
gets(s);
l = 0;
h = n;
while(l<=h)
{
m = (l+h)/2;
r = strcmp(s,list[m].cand);
if(r>0)
l = m + 1;
else if(r<0)
h = m - 1;
else
{
list[m].vote++;
break;
}
}
}
max = -1;
for(i=0; i<n; i++)
if(list[i].vote>max)
{
j = i;
max = list[i].vote;
}
t = 0;
for(i=0; i<n; i++)
{
if(i!=j && max==list[i].vote)
{
t = 1;
printf("tie\n");
break;
}
}
if(!t)
{
printf("%s\n",list[j].party);
}
if(c<case_no-1)
{
printf("\n");
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
int i,j,k,l,m,n,o,p;
char part[30][100];
char cand[30][100];
bool val[30];
int capa[30];
int voto[30];
int cand_c;
char s[100];
scanf("%d\n",&p);
while(p--) {
scanf("%d\n",&cand_c);
for(i=0;i!=cand_c;i++) {
gets(cand[i]);
gets(part[i]);
}
// candidates
for(i=0;i!=cand_c;i++) val[i]=0;
for(i=0;i!=cand_c;i++) {
for(j=0;j!=cand_c;j++) {
if(strcmp(part[j],part[i])==0) {
capa[i]=j;
val[j]=1;
break;
}
}
}
scanf("%d\n",&o);
for(i=0;i!=cand_c;i++) voto[i]=0;
// votes, please
while(o--) {
gets(s);
for(i=0;i!=cand_c;i++) {
if(strcmp(cand[i],s)==0) {
voto[capa[i]]++;
break;
}
}
}
j=0;
// finds the maximum voted
for(i=1;i<cand_c;i++) if(val[i] && voto[i]>voto[j]) j = i;
// is it a tie?
for(i=0;i<cand_c;i++) {
if(val[i] && i!=j && voto[i]==voto[j]) {
printf("tie\n");
goto nex;
}
}
// not a tie, go ahead
printf("%s\n",part[j]);
nex:
if(p) printf("\n");
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int dep, par;
char d[30][100];
int dp[30];
int vot[30];
char p[30][100];
char *getl(char *s) {
gets(s);
int len = strlen(s);
while(len && (s[len-1]==13 || s[len-1]==10)) { s[--len]='\0'; }
return s;
}
void st() {
int i,j,k;
dep = par = 0;
scanf("%d\n",&dep);
for(i=0;i<dep;i++) {
getl(d[i]);
getl(p[par]);
// cout << d[i] << " - " << p[par] << endl;
for(j=0;j<par;j++) {
if(strcmp(p[j],p[par])==0) { dp[i] = j; goto nex; }
}
dp[i] = par++;
nex:;
}
for(i=0;i!=par;i++) vot[i]=0;
scanf("%d\n",&k);
char s[200];
while(k--) {
getl(s);
for(i=0;i<dep;i++) if(!strcmp(d[i],s)) { vot[dp[i]]++; break; }
}
int maxi = *max_element(vot,&vot[par]);
int n = -1;
for(i=0;i!=par;i++){
if(vot[i]==maxi) {
if(n!=-1) goto tie;
else n = i;
}
}
cout << p[n] << endl;
return;
tie:cout << "tie" << endl;
}
int main() {
int t;
scanf("%d\n",&t);
while(t--) {
st();
if(t) cout << endl;}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct combo
{
char name[500];
char party[500];
int count;
};
combo list[100];
int cmp(const void *a,const void *b)
{
combo *p=(combo*)a;
combo *q=(combo*)b;
return strcmp(p->name,q->name);
}
int cmp2(const void *a,const void *b)
{
combo *p=(combo*)a;
combo *q=(combo*)b;
return q->count-p->count;
}
int main()
{
combo *ptr;
int cases,i,n,m;
char vote[1000000];
// freopen("in.in","r",stdin);
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
gets(list[i].name);
gets(list[i].party);
list[i].count=0;
}
qsort(list,n,sizeof(list[0]),cmp);
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
gets(vote);
ptr=(combo*)bsearch(vote,list,n,sizeof(list[0]),cmp);
if(ptr!=NULL)
{
list[ptr-list].count++;
}
}
qsort(list,n,sizeof(list[0]),cmp2);
if(list[0].count>list[1].count)
puts(list[0].party);
else
puts("tie");
if(cases)
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct combo
{
char name[500];
char party[500];
int count;
};
combo list[100];
int cmp2(const void *a,const void *b)
{
combo *p=(combo*)a;
combo *q=(combo*)b;
return q->count-p->count;
}
int main()
{
int cases,i,j,n,m;
char vote[1000000];
// freopen("in.in","r",stdin);
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
gets(list[i].name);
gets(list[i].party);
list[i].count=0;
}
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
gets(vote);
for(j=0;j<n;j++)
{
if(strcmp(vote,list[j].name)==0)
{
list[j].count++;
break;
}
}
}
qsort(list,n,sizeof(list[0]),cmp2);
if(list[0].count>list[1].count)
printf("%s\n",list[0].party);
else
printf("tie\n");
if(cases)
printf("\n");
}
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
struct Candid
{
string name, party;
int number;
};
int main ()
{
int i, j, k, l, x, n, m;
Candid stuff [20];
string temp;
bool first = true, tie;
ofstream fout;
cin >> x;
cin.get();
for (m = 0; m < x; m++) {
if (!first) {
fout << endl;
}
else {
first = false;
}
while (isspace(cin.peek())) {
cin.get();
}
cin >> n;
while (isspace(cin.peek())) {
cin.get();
}
for (i = 0; i < n; i++) {
stuff[i].number = 0;
stuff[i].name = "";
stuff[i].party = "";
while (cin.peek() != '\n') {
stuff[i].name += tolower(cin.peek());
cin.get();
}
cin.get();
while (cin.peek() != '\n') {
stuff[i].party += cin.peek();
cin.get();
}
cin.get();
}
while (isspace(cin.peek())) {
cin.get();
}
cin >> l;
while (isspace(cin.peek())) {
cin.get();
}
for (i = 0; i < l; i++) {
temp = "";
while (cin.peek() != '\n') {
temp += tolower(cin.peek());
cin.get();
}
cin.get();
for (j = 0; j < n; j++) {
if (temp == stuff[j].name) {
stuff[j].number++;
}
}
}
tie = true;
j = stuff[0].number;
k = 0;
for (i = 0; i < n; i++) {
if (stuff[i].number != j) {
tie = false;
}
}
if (tie) {
cout << "tie\n";
}
else {
for (i = 0; i < n; i++) {
if (j <= stuff[i].number) {
j = stuff[i].number;
k = i;
}
}
cout << stuff[k].party << endl;
}
}
return 0;
}
Users browsing this forum: No registered users and 0 guests