3-19 博乐科技C++笔试部分题解
全是暴力的题也没啥可说的。
就是第一题一直只过60%。给我恶心吐了
第四题手写个堆,话说过了就行谁要自己写堆呀。
第一题求税率
第二题求拦截水最大
第三题求字符串S能拆除几个T
第四题排序
第二题: class Solution { public: int maxArea(vector<int>& height) { // write code here int ans=0; int n = height.size(); for(int i =0;i<n;i++){ for(int j =i+1;j<n;j++){ ans = max(ans,min(height[i],height[j])*(j-i)); } } return ans; } }; 第三题 class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 输入s为一个长度为n的字符串,t是一个长度为m的字符串, 1<= s.length <= 10**9, 1 <= t.length <= 10**9。 * @param s string字符串 * @param t string字符串 * @return int整型 */ int MatchString(string s, string t) { // write code here unordered_map<char, int>mpS; unordered_map<char, int>mpT; unordered_map<char, int>mp; for(int i = 0;t[i];i++){ mpT[t[i]] ++; } for(int i =0;s[i];i++) mpS[s[i]]++; int ans = 10000000; for(int i = 0;t[i];i++){ mp[t[i]]++; if(mp[t[i]] == 1){ ans = min(ans,mpS[t[i]] / mpT[t[i]]); } } return ans; } }; 4 class Solution { public: vector<int> heap_top_k(vector<int>& nums, int k) { // write code here vector<int>ans; sort(nums.begin(),nums.end()); for(int i =nums.size()-1;i > nums.size() - k-1;i--) ans.push_back(nums[i]); return ans; } };
第一题恶心心,只能过60% ,我直接改了一个多小时,改上头了
double nowNum = 0.0; class Solution { public: string toString(double num){ string ans = ""; num *= 100; num += 0.5; long long p = (long long)num; long long x = p; int cnt = 0; while(p){ ans.push_back(p % 10 + '0'); cnt++; if(cnt == 2) ans.push_back('.'); p /= 10; } if(x <= 9){ ans.push_back('0'); ans.push_back('.'); ans.push_back('0'); } else if(x <= 99){ ans.push_back('0'); } reverse(ans.begin(), ans.end()); return ans; } double getNum(double needCost){ double ans = 0.0; if(nowNum + needCost > 960000.0){ double p = min(needCost,(nowNum + needCost - 960000)); ans += p * 0.45; needCost -= p; } if(needCost < 0.01) return ans; if(nowNum + needCost > 660000.0){ double p = min(needCost,(nowNum + needCost - 660000)); ans += p * 0.35; needCost -= p; } if(needCost < 0.01) return ans; if(nowNum + needCost > 420000.0){ double p = min(needCost,(nowNum + needCost - 420000)); ans += p * 0.30; needCost -= p; } if(needCost < 0.01) return ans; if(nowNum + needCost > 300000){ double p = min(needCost,(nowNum + needCost - 300000)); ans += p * 0.25; needCost -= p; } if(needCost < 0.01) return ans; if(nowNum + needCost > 144000.0){ double p = min( (nowNum + needCost - 144000),needCost ); ans += p * 0.20; needCost -= p; } if(needCost <= 0.01) return ans; if(nowNum + needCost > 36000){ double p = min ( needCost, (nowNum + needCost - 36000) ); ans += p * 0.1; needCost -= p; } if(needCost <= 0.01) return ans; if(nowNum + needCost > 0){ ans += ( needCost) * 0.03; } return ans; } vector<string> calcTax(vector<double>& income) { nowNum = 0.0; vector<string>ans; for(int i=0; i < income.size(); i++){ if(income[i] <= 5000){ ans.push_back("0.00"); } else { double realNum = getNum( income[i] - 5000 ); string x = toString(realNum); if(x == "" || realNum < 0.01) x = "0.00"; nowNum += (income[i] - 5000); ans.push_back(x); } } return ans; } };