第四题概率不会,大佬给我发下答案补上 ,要看题目的可以点这个链接
#include <bits/stdc++.h> using namespace std; int main() { //freopen("../in.txt", "r", stdin); string str; cin >> str; int i, len = str.length(), m, number, ans = 0, c1, c2; for (i = 0, m = 0, c1 = 0, c2 = 0; i < len; i++) { number = str[i] - '0'; //遇到3的情况直接加1 if (number % 3 == 0) { ans++; m = 0, c1 = 0, c2 = 0; continue; } m += number; if (number % 3 == 1) c1++; else c2++; //判断能不能有一个组合 if ((m > 0 && m % 3 == 0) || (c1 > 0 && c2 > 0)) { ans++; m = 0, c1 = 0, c2 = 0; } } cout << ans << endl; }
2.这题其实描述是有点不完整的,按照题目的意思,得到的ans可能会爆long long,但是我这么交上去过了
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { //freopen("../in.txt", "r", stdin); ll t,x,k,ans,i,j,a,lenX,lenK,lenA; int posX[160],posK[160],posA[160]; cin>>t; while (t--){ memset(posX,0, sizeof(posX)); cin>>x>>k; i=0,ans=0; while (x>0){ posX[i++]=x&1; x=x>>1; } lenX=i; i=0; while (k>0){ posK[i++]=k&1; k=k>>1; } lenK=i; for(i=0,j=0,a=0;j<lenK;i++){ if(posX[i]==0) posA[a++]=posK[j++]; else posA[a++]=0; } lenA=a; for(i=lenA-1;i>=0;i--){ ans=ans<<1; ans=ans|posA[i]; } cout<<ans<<endl; } }
3.
#include <bits/stdc++.h> using namespace std; #define ll long long int a[10]={0,1,2,3,4,5,6,7,8,9}; int pos[10],i,p[10]; set<string> v; void Print(){ string s=""; for(i=0;i<10;i++){ if(p[i]==1) s+=a[i]+'0'; // cout<<a[i]; } v.insert(s); // cout<<s<<endl; } int main() { // freopen("../in.txt", "r", stdin); for(i=0;i<10;i++) cin>>pos[i]; int add = 0,tmp; while (1){ int flag=0; for(i=9;i>=0;i--){ if(p[i]==0){ flag=1; } } if(flag==0){ break; } //打印 tmp=add++; for(i=9;i>=0;i--){ p[i]=pos[i]; if(pos[i]==0){ p[i]=tmp&1; tmp=tmp/2; } } Print(); } set<string>::iterator it; for(it=v.begin();it!=v.end();it++){ cout<<*it<<endl; } }
4.
5.
#include <bits/stdc++.h> using namespace std; #define ll long long int n, a[1005], Max; int dp[1005];//dp[i]为前i个数的最大升序子段和 const int inf = 99999; int main() { //freopen("../in.txt", "r", stdin); int i = 1, j, temp; while (cin >> a[i++]); n = i; memset(dp, 0, sizeof(dp)); for (i = 1; i <= n; i++) { temp = -inf; for (j = 0; j < i; j++) { if (a[j] < a[i]) temp = max(temp, dp[j]);//如果i前没有小于它的,temp就为0 } //且dp[i]也为它本身 dp[i] = temp + a[i]; } temp = -inf; for (i = 1; i <= n; i++) if (dp[i] > temp) temp = dp[i]; cout << temp << endl; }
6.
#include <bits/stdc++.h> using namespace std; #define ll long long void string_replace(string &strBig, const string &strsrc, const string &strdst) { int pos = 0; int srclen = strsrc.size(); int dstlen = strdst.size(); while ((pos = strBig.find(strsrc, pos)) != -1) { strBig.replace(pos, srclen, strdst); pos += dstlen; } } int main() { // freopen("../in.txt", "r", stdin); string str, s, p; getline(cin, str); cin >> s >> p; string_replace(str, s, p); cout << str << endl; }
全部评论
(14) 回帖