Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
1 2 3 4 5 6 |
class Solution { public: int getSum(int a, int b) { return b==0?a:getSum(a^b, (a&b)<<1); } }; |
December 17, 2021
Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
1 2 3 4 5 6 |
class Solution { public: int getSum(int a, int b) { return b==0?a:getSum(a^b, (a&b)<<1); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// 3934633 2017-03-13 10:48:25 Accepted 3612 C++0x 900 540 xxfflower #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> #include <queue> #include <set> using namespace std; typedef long long int64; int main() { int t,n,x; char op[20]; cin>>t; while(t--) { scanf("%d", &n); vector<int64> a; while(n--) { scanf("%s %d", op, &x); auto it = lower_bound(a.begin(), a.end(), x); if(op[0] == 'r') { if(it==a.end() || *it != x) { puts("Wrong!"); continue; } else { a.erase(it); if(a.empty()) { puts("Empty!"); continue; } } } else if(op[0] == 'a') { a.insert(it, x); } if(a.size()%2==1) { printf("%lld\n", a[a.size()/2]); } else { int64 ans = a[a.size()/2-1] + a[a.size()/2]; if(ans%2==0) printf("%lld\n", ans/2); else printf("%.1lf\n", ans/2.0); } } } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// 3934609 2017-03-13 08:38:54 Accepted 3713 C++0x 290 6428 xxfflower #include <iostream> #include <string> #include <iomanip> using namespace std; void put_hex(int byte) { cout << hex << uppercase << setfill('0') << setw(2) << byte; } void put_str(const string& s) { for(char c : s) put_hex(c); } void put_len(int length) { while(length>=128) { put_hex(length % 128 + 128); length >>= 7; } put_hex(length); } int main(int argc, char const *argv[]) { int n; cin>>n; string s; getline(cin, s); while(n--) { getline(cin, s); put_len(s.length()); put_str(s); cout<<endl; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public: int countBattleships(vector<vector<char>>& board) { int count = 0; for(int i=0;i<board.size();++i) for(int j=0;j<board[i].size();++j) { if(board[i][j] == 'X' && (i==0 || board[i-1][j] == '.') && (j==0 || board[i][j-1] == '.')) ++count; } return count; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Solution { public: bool check(const string& word, const string& row) { for(char c : word) { if(row.find(tolower(c)) == string::npos) return false; } return true; } vector<string> findWords(vector<string>& words) { const vector<string> rows = { "qwertyuiop", "asdfghjkl", "zxcvbnm" }; vector<string> ans; for(const auto& word : words) { for(const auto& row : rows) { if(check(word, row)) { ans.push_back(word); break; } } } return ans; } }; |