第一题
喝咖啡
#include<iostream> #include<vector> using namespace std; int main() { int N,A,X; while(cin>>N>>A>>X) { vector<int> times; times.resize(N); int sum = 0; for(int i = 0;i<N;i++) { cin>>times[i]; sum = sum+times[i]; } if(X > 8) { X = 8; } int speed_up_times = 60*A*X; int no_speed_times = (8-X)*60; if(speed_up_times + no_speed_times < sum) { cout<<0<<endl; } else { if(sum >= speed_up_times) { int left = sum-speed_up_times; cout<<X*60+left<<endl; } else { int hours = sum/(60*A); int left = sum-hours*60*A; int minute = left/A; if(left%A != 0) { minute++; } cout<<hours*60+minute<<endl; } } } }
第二题
挑零食
#include<iostream> #include<vector> using namespace std; int main() { int N,T; while(cin>>N>>T) { vector<int> prices; vector<int> happys; for(int i = 0;i<N;i++) { int price; int happy; cin>>price>>happy; int count; cin>>count; while(count--) { prices.push_back(price); happys.push_back(happy); } } vector<int> dp; dp.resize(T+1,0); int result = 0; for(int i = 0;i<prices.size();i++) { for(int j = T;j>=0;j--) { if(j-prices[i] == 0) { dp[j] = max(dp[j],happys[i]); } else if(j-prices[i] > 0 && dp[j-prices[i]] != 0) { dp[j] = max(dp[j-prices[i]]+happys[i],dp[j]); } if(dp[j] > result) { result = dp[j]; } } } cout<<result<<endl; } }
第三题
搜索字符串
#include<iostream> #include<vector> #include<set> using namespace std; bool search(vector<vector<char>> &matrix,int i,int j,string &str,int index) { if(i<0 || j<0 || i>=matrix.size() || j>= matrix[i].size()) { return false; } else { if(matrix[i][j] == '#') { return false; } else { if(index == str.length()-1 && matrix[i][j] == str[index]) { return true; } if(matrix[i][j] != str[index]) { return false; } else { char temp = matrix[i][j]; matrix[i][j] = '#'; bool result = search(matrix,i+1,j,str,index+1) || search(matrix,i,j+1,str,index+1) || search(matrix,i-1,j,str,index+1) || search(matrix,i,j-1,str,index+1); matrix[i][j] = temp; return result; } } } } int main() { int N; while(cin>>N) { vector<vector<char>> matrix; matrix.resize(N); set<string> Mset; for(int i = 0;i<N;i++) { string temp; cin>>temp; for(auto &j:temp) { matrix[i].push_back(j); } } int M; cin>>M; while(M--) { string str; cin>>str; for(int i = 0;i<matrix.size();i++) { for(int j = 0;j<matrix.size();j++) { if(search(matrix,i,j,str,0)) { Mset.insert(str); goto next; } } } next: continue; } for(auto &i:Mset) { cout<<i<<endl; } } }
全部评论
(2) 回帖