網頁

2014年2月6日 星期四

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;
}

2014年1月21日 星期二

UVA 10409 - Die Game

 #include <iostream>
 #include <string>
 using namespace std;
 
 class Dice{
  public:
   int up, down, north, east, south, west ;
   void init (){
     up=1, down=6, north=2, west=3, east=4, south=5 ;  
   }
   void rotate (string input){
     int tmp = 0 ; 
     if (input=="north")
       tmp=up, up=south, south=down, down=north, north=tmp;
     if (input=="south")
       tmp=up, up=north, north=down, down=south, south=tmp;
     if (input=="east")
       tmp=up, up=west, west=down, down=east, east=tmp;
     if (input=="west")
       tmp=up, up=east, east=down, down=west, west=tmp;
   }
 }; 
 
 int main(int argc, char *argv[])
 {
   int t ;
   string input;
   Dice dice ;
   
  while (cin >> t && t){
    dice.init() ; 
    while (t--){
      cin >> input ;
      dice.rotate(input) ;  
    }
    cout << dice.up << endl;
  }
  return 0;
}
 #include <iostream>
 #include <string>
 #include <map>
 using namespace std;
 
 class Dice{
  public:
   int tmp, up, down, north, east, south, west ;
   map<string, void (Dice::*)()> m;
 
   void init (){
     tmp=0, up=1, down=6, north=2, west=3, east=4, south=5;
     m["north"] = &Dice::rotateNorth;
     m["south"] = &Dice::rotateSouth;
     m["west"] = &Dice::rotateWest;
     m["east"] = &Dice::rotateEast;
   }
   void rotate (string input){
     (this->*m[input])(); 
   }
 
   void rotateNorth(){
     tmp=up, up=south, south=down, down=north, north=tmp;
   }
   void rotateSouth(){
     tmp=up, up=north, north=down, down=south, south=tmp;
   }
   void rotateEast(){
     tmp=up, up=west, west=down, down=east, east=tmp;
   }
   void rotateWest(){
     tmp=up, up=east, east=down, down=west, west=tmp;
   }
 }; 
 
 int main(int argc, char *argv[])
 {
   int t ;
   string input;
  Dice dice ;
  
  while (cin >> t && t){
    dice.init() ; 
    while (t--){
      cin >> input ;
      dice.rotate(input) ;  
    }
    cout << dice.up << endl;
  }
  return 0;
}

2014年1月20日 星期一

UVA 10188 minesweeper

 #include <iostream>
 #include <cstdio>
 #include <cstring> 
 #define INF 0x7fffffff
 
 using namespace std ;
 
 int main(int argc, char *argv[])
 {
   int map[102][102] ;
   char ch ; 
   int n, m;
   for (int t=1 ; cin >> n >> m && n && m ; ++t){
     if(t>1) puts("") ;
     memset(map, 0, sizeof(map)) ;
     for (int i=1; i<=n ; ++i){
       for(int j=1 ; j<=m ; ++j){
         cin >> ch; 
         if (ch ==  '.'){
           map[i][j] = 0 ;
         }else {
           map[i][j] = -INF ;
         }
       }
     }
     
     for(int i=1 ; i<=n ; ++i){
       for(int j=1 ; j<=m ; ++j){
         if(map[i][j]<0){
           for(int k=-1 ; k<=1 ; ++k)
             for(int l=-1 ; l<=1 ; ++l)
               map[i+k][j+l]++ ; 
         }
      }
    }

    printf("Field #%d:\n", t) ; 
    for(int i=1 ; i<=n ; ++i){
      for(int j=1 ; j<=m ; ++j){
        if (map[i][j] < 0) {
          printf("*") ; 
        } else {
          printf("%d", map[i][j]) ; 
        }
      }
      puts(""); 
    }
  }
  return 0;
}