1.小乐乐该数字
遇到按位处理的情况可以考虑用字符串去读
代码:
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include<string> using namespace std; int main() { string str; cin >> str; int ans = 0; for (int i = 0; i < str.size(); i++) { int u = str[i] - '0'; if (u % 2)ans = ans * 10 + 1; else ans *= 10; } cout << ans << endl; return 0; }
2.十字爆破
思路:
用俩数组分别维护每行和每列的和就好了。注意long long
代码:
#include <iostream> #include<vector> using namespace std; typedef long long LL; const int N=1e6+10; LL col[N]; LL row[N]; int main() { int n,m; cin>>n>>m; vector<vector<int>> a(n+1,vector<int>(m+1)); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; row[i]+=a[i][j]; col[j]+=a[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cout<<(LL)row[i]+col[j]-a[i][j]<<" "; } cout<<endl; } }
3.比那名居的桃子
思路:
前缀和维护快乐值和羞耻值,枚举每一天,假设在当前天吃掉桃子,看能不能更新答案。
代码:
#include <iostream> using namespace std; typedef long long LL; const int N=1e5+10; LL sa[N]; LL sb[N]; int main() { int n,k; cin>>n>>k; for(int i=1;i<=n;i++){ int x; cin>>x; sa[i]=sa[i-1]+x; } for(int i=1;i<=n;i++){ int x; cin>>x; sb[i]=sb[i-1]+x; } LL res1=0;//快乐值 LL res2=1e9;//羞耻 int ans=0; for(int i=1;i+k-1<=n;i++){ LL a=sa[i+k-1]-sa[i-1]; LL b=sb[i+k-1]-sb[i-1]; if(res1<=a){ if(res1==a&&res2>=b){ if(res2==b){ ans=min(ans,i); }else{ res2=b; ans=i; } }else{ res1=a; res2=b; ans=i; } } // cout<<res1<<" "<<res2<<endl; } cout<<ans<<endl; return 0; }