標籤:codeforces string 演算法 刷題
Codeforces印象
這兩天抽時間去codeforces體驗了一把。
首先,果然有眾多大牛存在,很多名人一直參加每周一次的比賽,積分2000+,並參與出題。
另外,上面題目很多,估計至少一千題。比賽結束後,題目將轉為練習題,可以持續嘗試。每道題目都有標籤,如greedy, math, matrices等等,可以點擊相應的標籤只做相關的題目。可惜我做了好幾道之後才發現。
這次解決的題目
首次嘗試,這次做的幾個都是選的完成人數最多的,比較簡單,但還是有些東西是從中新學習到的。以後最好分類練習。
1A Theatre Square
- 用a x a的磚塊去鋪滿面積m x n的廣場,不準破壞磚塊,允許超出廣場,求磚塊數目
- 分別計算長寬至少多少塊即可
- 小技巧:(m-1)/a+1來計算長度方向的磚塊數目
#include<iostream>#include<limits>using namespace std;int main(){ long long m,n,a; //cout << numeric_limits<long>::max() << endl; cin >> m >> n >> a; cout << ((m-1)/a + 1)*((n-1)/a + 1) << endl; return 0;}
4A Watermelon
158A Next Round
71A Way Too Long Words
- 給定一個單詞,如果長度超過10,改成縮寫形式:首字母+中間的字母個數+尾字母
- 直接輸出結果即可
118A String Task
- 給定一個單詞,刪掉所有母音字母,其餘字母轉換為小寫並每個字母前加.符號
- 使用ostringstream和tolower函數
#include<iostream>#include<sstream>using namespace std;int main(){ string str; cin >> str; ostringstream ostr; for(auto i:str){ char t=tolower(i); if(t == 'a' || t=='o' || t=='y'||t=='e' ||t=='u' || t=='i') ; else ostr<< '.' << t; } cout << ostr.str() << endl; return 0;}
158B Taxi
- n個小組,每組不超過4人,出租車每車不能超過4人,同組人不能分開,求最少要多少輛車
- 貪心,盡量坐滿每一輛車,剩下的進行組合
#include<iostream>#include<vector>using namespace std;int main(){ int n; cin >> n; // can't initialize a vector with its element; //vector<int> test(1,2,3,4); vector<int> stat(4,0); for(int i = 0;i < n;i++){ int tmp; cin >> tmp; stat[tmp-1] += 1; } int num_taxi = 0; num_taxi += stat[3]; if(stat[2]>= stat[0]){ num_taxi += stat[2]; num_taxi += (stat[1]+1)/2; } else{ num_taxi += stat[2]; stat[0] -= stat[2]; num_taxi += (stat[0] + stat[1]*2 -1)/4 + 1; } cout << num_taxi << endl; return 0;}
50A Domino piling
- mxn的廣場用2x1的磚塊鋪滿,至少多少塊?
- 分析m,n在為奇偶的情況下的鋪法即可
231A Team
116A Tram
131A cAPS lOCK
- 根據特定條件更改字串中的字母大小寫
- 關鍵是如何遍曆字串中的字元並更改大小寫
- for(char &c:s) 可以方便的遍曆字串
- cctype標頭檔中包含了islower,tolower,isupper,toupper等char字元處理函數
#include<iostream>#include<cctype>using namespace std;bool meet_rule(string s){ for(char &c:s.substr(1)) { if(islower(c)) return false; } return true;}int main(){ string s; cin >> s; if(meet_rule(s)) { for(char &c:s) { if(true ) { if(islower(c)) c = toupper(c); else c = tolower(c); } } } cout << s << endl; return 0;}
282A Bit++
轉載請註明Focustc,部落格地址為http://blog.csdn.net/caozhk,原文連結為點我