
UVA Problem 1124 Solution
#include <bits/stdc++.h> int main() { char s[105]; while(gets(s)) { printf("%s",s); } return 0; }
#include <bits/stdc++.h> int main() { char s[105]; while(gets(s)) { printf("%s",s); } return 0; }
#include <stdio.h> #define N 10000000 typedef long long LL; using namespace std ; LL primes[N]; //bool flag[N]; void sieve(LL n) { for(LL i=4; i<=n; i+=2) { primes[i]=1; } //LL p=(LL)sqrt(n); for(LL i=3; i*i<=n; i+=2) { if(primes[i]) continue; //primes[cnt++]=i; // if(i <= n/i) // { for(LL j=i*i; j<=n; j+=i*2) { primes[j] = 1; } // } } return ; } /*bool isprimes(LL x) { for(LL i=0;;i++) { if(primes[i]*primes[i] >x ) break; if(x%primes[i]==0) return false; } return true; }*/ void res(LL x) { for(LL i=2;;i++) { //LL p1=primes[i]; // LL p2= primes[x-i]; if( !primes[i] && !primes[x-i] ) { printf("%lld %lld",i,x-i ); break; } } } int main() { LL n; sieve(N); while(scanf("%lld",&n)!=EOF){ if(n<=7){ printf("Impossible."); continue; } if(n&1){ printf("2 3 "); res(n-5); } else{ printf("2 2 "); res(n-4); } puts(""); } return 0 ; }
#include <bits/stdc++.h> using namespace std; char s[100000],t[100000]; int main() { int i,j; bool flag; while(scanf("%s %s",s,t)!=EOF) { flag=false; for(j=0,i=0;t[i]!='\0';i++) { if (s[j] == t[i]) j++; if (s[j] == '\0') { flag = true; break; } } if(flag==true) printf("Yes\n"); else printf("No\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d,res; while(cin>>a>>b>>c>>d) { if(a==0 and b==0 and c==0 and d==0) break; res=1080; res += (a > b) ? (a - b) * 9 : (a - b + 40) * 9; res += (c > b) ? (c - b) * 9 : (c - b + 40) * 9; res += (c > d) ? (c - d) * 9 : (c - d + 40) * 9; //res+=a>b? a-b*9 :((a-b)+40)*9; cout<<res<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x,y,k,n,i,KK,X[10005],Y[10005]; bool f; KK=0; while(cin>>k) { KK=0; //X[105]=Y[105]={0}; memset(X, 0, sizeof(X[0]) * 10005); memset(Y, 0, sizeof(Y[0]) * 10005); for(y= 2*k ; y!=k; y--) { // x=(k*y)/(y-k); if((k*y)%(y-k)==0) { x=(k*y)/(y-k); X[KK] = x; Y[KK] =y; KK++; } } printf("%d\n",KK); for (int i = (KK - 1); i >= 0; --i) { printf("%d/%d = %d/%d + %d/%d\n", 1, k, 1, X[i], 1, Y[i]); //if (f==true) //printf("%d = %d + %d\n",1/k,1/x,1/y); } } return 0; }
#include <bits/stdc++.h> #include <algorithm> #include <vector> #include <map> #include <iostream> using namespace std; #define SIZE(a) ((int)a.size()) #define FOREACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) int main() { for (int n; cin >> n && n;) { map<vector<int>, int> m; vector<int> v(5); vector<vector<int> > vs; for (int i = 0; i < n; ++i) { for (int j = 0; j < 5; ++j) cin >> v[j]; sort(v.begin(), v.end()); vs.push_back(v); m[v]++; } int res = 0; FOREACH(data, m) res = max(res, data->second); int cnt = 0; FOREACH(vv, vs) cnt += m[*vv] == res; printf("%d\n", cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[150],i,j,tc,n,sum; cin>>tc; while(tc--) { sum=0; cin>>n; for(i=0;i<n;i++) cin>>a[i]; sort(a,a+n); for(i=0;i<n-1;i++) sum+=abs(a[i]-a[i+1]); sum*=2; cout<<sum<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main ( ) { char arr[101],a[101], b[101],c[101],d[101], e[101],f[101],g[101], h[101],in[101], jn[101]; int val,i, tc,cases = 1 ; cin>>tc; while (tc--) { scanf ( "%s %d" , a ,&arr[0]); scanf ( "%s %d" , b ,&arr[1]); scanf ( "%s %d" , c ,&arr[2]); scanf ( "%s %d" , d ,&arr[3]); scanf ( "%s %d" , e ,&arr[4]); scanf ( "%s %d" , f ,&arr[5]); scanf ( "%s %d" , g ,&arr[6]); scanf ( "%s %d" , h ,&arr[7]); scanf ( "%s %d" , in ,&arr[8]); scanf ( "%s %d" , jn ,&arr[9]); val = 0 ; for ( i=0;i<10;i++ ) { if ( arr[i] > val ) { val= arr[i] ; } } cout<<"Case #"<<cases++<<":"<<endl; for(int j=0;j<10;j++) { if(arr[j]==val) { if(j==0) printf("%s\n",a); if(j==1) printf("%s\n",b); if(j==2) printf("%s\n",c); if(j==3) printf("%s\n",d); if(j==4) printf("%s\n",e); if(j==5) printf("%s\n",f); if(j==6) printf("%s\n",g); if(j==7) printf("%s\n",h); if(j==8) printf("%s\n",in); if(j==9) printf("%s\n",jn); } } } return 0 ; }
#include <bits/stdc++.h> using namespace std; int main() { char s[2001]; int cases=1,l; while(scanf("%s",s)) { if(strcmp(s,"#")==0) break; if(strcmp(s,"HELLO")==0) printf("Case %d: ENGLISH\n",cases++); else if(strcmp(s,"HOLA")==0) printf("Case %d: SPANISH\n",cases++); else if(strcmp(s,"HALLO")==0) printf("Case %d: GERMAN\n",cases++); else if(strcmp(s,"BONJOUR")==0) printf("Case %d: FRENCH\n",cases++); else if(strcmp(s,"CIAO")==0) printf("Case %d: ITALIAN\n",cases++); else if(strcmp(s,"ZDRAVSTVUJTE")==0) printf("Case %d: RUSSIAN\n",cases++); else printf("Case %d: UNKNOWN\n",cases++); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int tc,cases=1,n,a[1001],give,given,egl; while(cin>>n) { give=0,given=0; if (n==0) break; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) if(a[i]==0) give++; else given++; egl=given-give; cout<<"Case "<<cases++<<":"<<" "<<egl<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[101],l; int i,tc,ans; cin>>tc; while(tc--) { scanf("%s",s); l=strlen(s); if(l<=3) { if(s[0] =='o'&&s[1]=='n'&& s[2] == 'e' || s[0] == 'o' && s[1] == 'n' || s[0] == 'o' && s[2] == 'e' ||s[1] == 'n' && s[2] == 'e') printf("1\n"); else if(s[0] == 't' && s[1] == 'w' && s[2] == 'o'||s[0] == 't' && s[1] == 'w' ||s[0] == 't' && s[2] == 'o'||s[1] == 'w' && s[2] == 'o' ) printf("2\n"); } else cout<<"3"<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[150]; int tc,a,sum=0,amount; cin>>tc; while(tc--) { scanf("\r"); scanf("%s",s); if(strcmp(s,"donate")== 0) { cin>>amount; sum+=amount; //printf("%d\n",sum); } else printf("%d\n",sum); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,ans; while(cin >> a >> b) { if(a==-1 and b==-1) break; ans=abs(a-b); if(ans>50) ans=100-ans; cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[10]; int cases=1; while(scanf("%s",s)) { if(strcmp(s,"*")==0) break; else if( strcmp(s,"Hajj")== 0) printf("Case %d: Hajj-e-Akbar\n",cases++); else printf("Case %d: Hajj-e-Asghar\n",cases++); } return 0; }
#include<bits/stdc++.h> using namespace std; int tc=1,position=0; int bfs(int src, int TTL,map <int, vector<int> > edges) { map<int,int> visited,level; int unreach=0; queue<int>Q; Q.push(src); position=1; visited[src]=1; level[src]=0; while(!Q.empty()) { int u=Q.front(); for(int i=0;i<edges[u].size();i++) { int v=edges[u][i]; if(!visited[v]) { level[v]=level[u]+1; if(level[v]>TTL) unreach++; //printf("unreach = %d\n",unreach); j node gula unreach visited[v]=1; Q.push(v); position++;//printf("%d unreach\n",unreach); koto num node teke jay na } } Q.pop(); } return unreach; } int main() { vector<pair< int,int > >check(100); //pairakare chack ex: 35 2 , 35 3; int edge,x,y,node,source,ans; while(1) { map <int,vector<int> > edges; //a[0]=0,1,2;a[1]=2,3,4,5; scanf("%d",&edge); if(edge==0) return 0; for(int i=0;i<edge;i++) { cin>>x>>y; edges[x].push_back(y); edges[y].push_back(x); } int index=0,totalunreach; while(1) { cin>>check[index].first>>check[index].second; // 35 2 if(check[index].first ==0 and check[index].second==0) break; index++; } for(int i=0;i<index;i++) { ans=bfs(check[i].first,check[i].second,edges);// printf("val= %d" ,ans); totalunreach = ans + edges.size()-position; printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",tc++,totalunreach,check[i].first,check[i].second); position=0; } } return 0; }
#include <cstdio> #include <vector> #include <queue> using namespace std; typedef vector<int> vi; int main() { int V, E, u, v; vector<vi> AdjList; while (scanf("%d", &V), V) { AdjList.assign(V, vi()); scanf("%d", &E); for (int i = 0; i < E; i++) { scanf("%d %d", &u, &v); AdjList[u].push_back(v); AdjList[v].push_back(u); } queue<int> q; q.push(0); vi color(V, 1e9); color[0] = 0; bool isBipartite = true; while (!q.empty() && isBipartite) { int u = q.front(); q.pop(); for (int j = 0; j < (int) AdjList[u].size(); j++) { int v = AdjList[u][j]; if (color[v] == 1e9) { color[v] = 1 - color[u]; q.push(v); } else if (color[v] == color[u]) { isBipartite = false; break; } } } if (isBipartite) printf("BICOLORABLE.\n"); else printf("NOT BICOLORABLE.\n"); } return 0; }
#include <bits/stdc++.h> #include <string> #include <vector> #include <map> using namespace std; int main() { string s; int frequency=0; vector<string>vec; while(getline(cin,s)) { v.push_back(s); frequency=max(frequency,(int)s.length()); } for(int i=0;i<frequency;i++) { for(int j=vec.size()-1;j>=0;j--) { if(i < vec[j].length()) { printf("%c",vec[j][i] ); } else printf("%c",' '); } cout<<endl; } return 0; }
#include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; int main() { stringstream ss; string line1,line2; vector<int>A,B; int i,j,ch; while(getline(cin,line1) && getline(cin,line2)) { A.clear(); B.clear(); ss.clear(); ss<<line1; while(ss>>ch) { A.push_back(ch); } ss.clear(); ss<<line2; while(ss>>ch) { B.push_back(ch); } int Acnt=0; for(int m=0;m<A.size();m++) { for(int n=0;n<B.size();n++) { if(A[m]==B[n]) { Acnt++; } } } if ((Acnt == A.size()) && (Acnt < B.size())) { cout << "A is a proper subset of B" << endl; } else if ((Acnt == B.size()) && (Acnt < A.size())) { cout << "B is a proper subset of A" << endl; } else if ((Acnt == A.size()) && (Acnt == B.size())) { cout << "A equals B" << endl; } else if ((Acnt == 0) ) { cout << "A and B are disjoint" << endl; } else { cout << "I'm confused!" << endl; } } return 0; }
#include <bits/stdc++.h> #include <string.h> #include <map> using namespace std; int main() { string s; //map<char,int>m; // map<char,int>::iterator it; while(getline(cin,s)) { map<char,int>m; int mxfrq=0; int len=s.size(); for(int i=0;i<len;i++) { if((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { m[s[i]]++; mxfrq=max(mxfrq,m[s[i]]); } } map<char,int>::iterator it; for(it = m.begin(); it != m.end(); it++){ if(it->second == mxfrq) cout<<it->first; //printf(" value == %c %c\n",it->first,it->second); } printf(" %d\n",mxfrq); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[140],charcter,l; int i,j; while(gets(s)) { for(i=0; s[i] != '\0'; ) { int freq = 0; while( (s[i] >='0' && s[i] <='9') ) { charcter = ( s[i++]-'0' ); freq += charcter; //return 0*printf("ch = %c fre = %d\n",charcter,freq); } for(j=0;j<freq;j++) { if(s[i]=='b') { cout<<" "; } else { printf("%c",s[i]); } } if ( s [i] == '!' ) cout<<endl; i++; } cout<<endl; } return 0; }