網頁

2014年2月24日 星期一

UVA 10127 - Ones

 #include <iostream>
 #include <cstdio>
 #include <cmath>
 #include <cstring>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <algorithm>
 #include <map>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 typedef long long int lli;
 
 int main(int argc, char *argv[]){
   int n;
   while (cin >> n){
     int digit=1, number=1;
     for (int number=1; number%n!=0; number=number*10+1, number%=n)
       digit++;
     cout << digit << endl ;
   }
   return 0;
 }

c048: Ant on a Chessboard

 #include <stdio.h>
 #include <math.h> 
 
 int main(int argc, char *argv[])
 {
        int N ; 
        int layer ; 
        int base ; 
        int displace ; 
        int coor[2] ; 
        while(scanf("%d",&N) && N){
                N-- ; 
                layer = floor(sqrt(N))+1; 
                base = layer*(layer-1) ; 
                displace = N-base ; 
                coor[0] = coor[1] = layer ; 
 
                if(layer%2 == 1) 
                        if(displace > 0)
                                coor[0] -= displace ; 
                        else
                                coor[1] += displace ;
                else
                        if(displace > 0) 
                                coor[1] -= displace ; 
                        else
                                coor[0] += displace ; 
                printf("%d %d\n", coor[0], coor[1]) ;  
        }
        return 0;
 }

2014年2月14日 星期五

UVA 10340 - All in All

 #include <iostream>
 #include <cstdio>
 #include <cmath>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <algorithm>
 #include <map>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 typedef long long int lli;
 
 int main(int argc, char *argv[]){
   string s, t;
   int i, j;
   while (cin >> s >> t){
     i=0, j=0;
     while (i!=t.length()){
       if (s[j]==t[i]) ++j;
       ++i;
     }
     cout << (j==s.length() ? "Yes" : "No") << endl;
   }
   return 0;
 }

UVA 10298 - Power Strings

 #include <iostream>
 #include <cstdio>
 #include <cmath>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <algorithm>
 #include <map>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 typedef long long int lli;
 
 int main(int argc, char *argv[]){
   string s;
   bool isPowerString=true;
   while (getline(cin, s) && s!="."){
     for (int i=1; i<=s.length(); ++i){
       if (s.length()%i != 0) continue;
 
       isPowerString = true;
       for (int j=i; j<s.length(); j+=i){
         for (int k=0; k<i; ++k)
           if (s[k] != s[k+j]){
             isPowerString=false;
             break;
           }
         if (isPowerString==false) break;
       }
 
       if (isPowerString==true){
         cout << s.length()/i << endl;
         break;
      }
    }
      
  }
  return 0;
}

2014年2月7日 星期五

uva 11917 - Do Your Own Homework

 #include <iostream>
 #include <cstdio>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <algorithm>
 #include <map>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 
 int main(int argc, char *argv[]){
   int t;
   int n;
   string subject;
   int days;
   int deadline;
   cin >> t;
   for (int i=1; i<=t; ++i){
     cin >> n;
     map<string, int> m;
     map<string, int>::iterator it;
     for (int j=0; j<n; ++j){
       cin >> subject >> days;
       m[subject] = days;
     }
     cin >> deadline >> subject;
 
     it = m.find(subject);
     if (it == m.end()){
       printf("Case %d: Do your own homework!\n", i);
     } else if (it->second <= deadline) {
       printf("Case %d: Yesss\n", i);
     } else if (it->second <= deadline+5) {
       printf("Case %d: Late\n", i);
     } else {
       printf("Case %d: Do your own homework!\n", i);
     }
   }
  return 0;
}

UVA 12602 - Nice Licence Plates

 #include <iostream>
 #include <cstdio>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <algorithm>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 
 int main(int argc, char *argv[]){
   string line, subline;
   int t;
   cin >> t;
   getline(cin, line); // read new line
   while (t--){
       getline(cin, line);
       int sum=0;
       int number=0;
       for (int i=0; i<3; ++i){
         sum = sum*26+(line[i]-'A');
       }
       subline = line.substr(4,4);
       stringstream ss;
       ss << subline;
       ss >> number;
       if (abs(number-sum)<=100){
         puts("nice");
       } else {
         puts("not nice");
       }
   }
   return 0;
}

UVA 10235 - Simply Emirp

 #include <iostream>
 #include <cstdio>
 #include <vector>
 #include <sstream>
 #include <algorithm>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 
 bitset<MAX+1> bs;
 
 void seive(){
   bs.set();
   bs[0]=false, bs[1]=false;
   
   for (long long int i=2; i<=bs.size(); ++i){
     if (bs[i]==true){
       for (long long int j=i*i; j<bs.size(); j+=i) 
         bs[j]=false;
     }
   }
   return; 
 }
 
 int main(int argc, char *argv[]){
   seive();
   
   int num, renum, tmp;
   while(cin>>num){
     tmp = num;
     for (renum=0; tmp; tmp/=10){
       renum = renum*10+(tmp%10);
     }
    if (bs[num]==true){
      if (bs[renum]==true && num!=renum){
        printf("%d is emirp.\n", num);
      } else {
        printf("%d is prime.\n", num);
      }
    } else {
      printf("%d is not prime.\n", num);
    }
  }
  return 0;
}

2014年2月6日 星期四

UVA 11150 - Cola

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(int argc, char *argv[]){
  int n;
  while(cin>>n){
    int total=n;
    int empty=n;
    int remain;

    while (empty>=3){
      remain = empty/3; 
      empty = empty%3;
       total += remain;
       empty += remain;
     }
 
     if (empty==2){
       total++;
     } 
     
     cout << total << endl;
   }
   return 0;
 }

UVA 10057 - A mid-summer night's dream.

 #include <iostream>
 #include <algorithm>
 #include <vector>
 using namespace std;
 
 int main(int argc, char *argv[]){
   int n;
   int num[1000001];
   while(cin>>n){
     int min_median=0;
     int max_median=0;
     int Innum=0;
     int integers=0;
     for (int i=0; i<n; ++i){
       cin >> num[i];
     }
     sort(num, num+n);
     
     if (n%2==1){
       min_median=num[n/2];
       for (int i=0; i<n; ++i)
         if (num[i]==min_median)
           Innum++;
       integers=1;
     } else {
       max_median=num[n/2];
       min_median=num[n/2-1];
       for (int i=0; i<n; ++i)
         if (num[i]==min_median || num[i]==max_median)
           Innum++;
       integers=max_median-min_median+1;
     }
     cout << min_median << " " << Innum << " " << integers << endl;
   }
   return 0;
 }

UVA 299 - Train Swapping

 /* just like bubble sort swapping times */
 #include <iostream>
 #include <algorithm>
 using namespace std;
 
 int main(int argc, char *argv[]){
   int t;
   cin >> t;
   int train[51];
   while(t--){
     int length;
     int count=0;
     cin >> length;
     for (int i=0; i<length; ++i){
       cin >> train[i];
     }
 
     for (int i=0; i<length-1; ++i){
       for (int j=0; j<length-i-1; ++j){
         if (train[j]>train[j+1])
           swap(train[j], train[j+1]), count++;
       }
     }
     cout << "Optimal train swapping takes " << count << " swaps." << endl;
   }
   return 0;
 }

UVA 10062 - Tell me the frequencies!

 #include <iostream>
 #include <string>
 #include <vector>
 #include <map>
 #include <algorithm>
 
 using namespace std;
 
 class Freq{
  public:
   Freq(char ansii, int count){
     this->ansii = ansii;
     this->count = count;
   }
   char ansii;
   int count;
 };
 
 bool FreqCompare (const Freq &a, const Freq &b){
   if (a.count!=b.count){
     return a.count<b.count;
   } else {
     return a.ansii>b.ansii;
   }
 }
 
 int main(int argc, char *argv[]){
   map<char, int> m;
   map<char, int>::iterator it;
   vector<Freq> freq;
   string line;
   bool firstTime=true;
   while (getline(cin, line)){
     m.clear();
     freq.clear();
     
     for (int i=0; i<line.size(); ++i){
       it = m.find(line[i]);
       (it == m.end()) ? m[line[i]]=1 : m[line[i]]++;  
    }
    
    for (it=m.begin(); it!=m.end(); ++it){
      freq.push_back(Freq(it->first, it->second));
    }

    sort(freq.begin(), freq.end(), FreqCompare);
    firstTime ? firstTime=false : cout << endl ;
    for (int i=0; i<freq.size(); ++i){
      cout << (size_t) freq[i].ansii << " " << freq[i].count << endl;
    }
  }
  return 0;
}

UVA 543 - Goldbach's Conjecture

 /* use seive method to generate the table of prime number 
    reference: http://www.cnblogs.com/xiaobaibuhei/p/3329702.html
    http://maplewing.blogspot.tw/2011/02/uva543goldbachs-conjecture.html*/
  
 #include <iostream>
 #include <vector>
 #include <bitset>
 #define MAX 1000000
 using namespace std;
 
 bitset<MAX+1> bs;
 vector<int> primes;
   
 void seive(){
   bs.set(); // set all bit to 1
   bs[0]=false, bs[1]=false;
 
   for (long long int i=2; i<=MAX; ++i){
     if (bs[i]==true){
       for (long long int j=i*i; j<=MAX; j+=i)
         bs[j]=false;
       primes.push_back(i);
     }
   }
   return;
 }
 
 int main(int argc, char *argv[]){
   seive();
   int n;
   while(cin>>n && n){
     for (int i=0; i<primes.size(); ++i){
       if (bs[primes[i]]==true && bs[n-primes[i]]==true){
         cout << n << " = " << primes[i] << " + " << n-primes[i] << endl;
         break;
       }
     }
   }
   return 0;
}

2014年2月5日 星期三

UVA 10226 - Hardwood Species

 #include <iostream>
 #include <cstdio>
 #include <iomanip>
 #include <string> 
 #include <map>
 #include <algorithm>
 
 using namespace std;
 
 int main(int argc, char *argv[]){
   int t;
   string line;
   cin >> t;
   getline(cin, line); // read new line
   getline(cin, line); // read new line
   while (t--){
     map<string, int> m;
     map<string, int>::iterator it;
     int count=0 ;
     while (getline(cin, line)){
       if (line=="") break;
       it = m.find(line);
       if (it == m.end()){
         m[line] = 1;
       } else {
         it->second++;
       }
       count++;
     }
     
     for (it=m.begin(); it!=m.end(); ++it){
       float percent = 100.0*it->second/count;
       cout  << it->first << ' ' << fixed << setprecision(4) << percent << endl;
     }
 
     if (t>0) cout << endl ;
     
   }
   return 0;
}