最小差值
class Solution { public: /** * 求最小差值 * @param a int整型vector 数组a * @return int整型 */ int minDifference(vector<int>& a) { int n=a.size(); sort(a.begin(),a.end()); int ans=2147483647; for(int i=0;i<n-1;i++) ans=min((long long)ans,(long long)abs(a[i]-a[i+1])); return ans;// write code here } };
Tree IV
class Solution { public: typedef long long ll; const ll mod = 998244353; inline ll qpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod, b >>= 1; } return ret % mod; } long long tree4(long long n) { ll ans = 0, inv2 = qpow(2, mod - 2), l, r, dep; for (l = 1, r = 1, dep = 1; r <= n; ++dep) { ans = (ans + dep * ((l + r) % mod * (r - l + 1) % mod * inv2 % mod) % mod) % mod; l = r + 1, r = (r + 1) * 2 - 1; } ans = (ans + dep * ((l + n) % mod * (n - l + 1) % mod * inv2 % mod) % mod) % mod; return ans; } };
牛牛组数
bool cmp(char a,char b) { return a>b; } class Solution { public: /** * 返回最大和的字符串 * @param x string字符串 即题目描述中所给字符串 * @param k int整型 即题目描述中所给的k * @return string字符串 */ string intToString(int num) { stringstream ss; ss<<num; return ss.str(); } string add(string s1, string s2){ string ans; int len1 = s1.length() - 1; int len2 = s2.length() - 1; int s = 0; int re = 0; while (len1 >= 0 || len2 >= 0) { s = re; if (len1 >= 0) s += s1[len1] - '0'; if (len2 >= 0) s += s2[len2] - '0'; re = s / 10; ans.push_back(s % 10 + '0'); len1--; len2--; } if (re) ans.push_back(re + '0'); reverse(ans.begin(), ans.end()); return ans; } string Maxsumforknumers(string x, int k) { // write code here int n=x.size(); sort(x.begin(),x.end(),cmp); int sum=0; int m=n-k+1; string str=x.substr(0,m); for(int i=m;i<n;i++)sum+=x[i]-'0'; string str2=intToString(sum); str=add(str,str2); return str; } };
全部评论
(0) 回帖