蓝桥杯准备
A.门牌制作
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=100010; int a[N]; int fcnt(int n){ int cnt=0; while(n){ if(n%10==2) cnt++; n/=10; } return cnt; } int main(){ int sum=0; for(int i=1;i<=2020;i++){ sum+=fcnt(i); } cout<<sum<<endl; }//答案:624
B.即约分数
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=100010; bool st[N]; int primes[N],cnt; int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } void get_primes(int n){ cnt=0; memset(st,true,sizeof(st)); st[1]=st[0]=false; for(int i=2;i<=n;i++){ if(st[i]) primes[cnt++]=i; for(int j=0;j<cnt;j++){ if(i*primes[j]>n) break; st[i*primes[j]]=false; if(i%primes[j]==0) break; } } } int main(){ int ans=0; for(int i=1;i<=2020;i++){ for(int j=1;j<=2020;j++){ if(gcd(i,j)==1) ans++; } } cout<<ans<<endl; }//答案:2481215
C.蛇形矩阵
#include <bits/stdc++.h> using namespace std; int a[50][50]; int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n = 45, num = 45 * 45; for(int i = 1, cnt = 1; i <= n && cnt <= num; i++) { if(i & 1) { for(int x = i, y = 1; x >= 1 && y <= i; x--, y++) { a[x][y] = cnt++; } } else { for(int x = 1, y = i; x <= i && y >= 1; x++, y--) { a[x][y] = cnt++; } } } printf("%d\n", a[20][20]); return 0; } /* 761 */
D:跑步锻炼
#include <bits/stdc++.h> using namespace std; int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int a = 2000, b = 1, c = 1, num = 1, ans = 0; while(a != 2020 || b != 10 || c != 2) { if(c == 1 || num % 7 == 3) ans += 2; else ans += 1; int nowday = day[b]; if((a % 400 == 0 || (a % 4 == 0 && a % 100 != 0)) && b == 2) nowday++; c++, num++; if(c > nowday) { c = 1; b += 1; } if(b == 13) { a += 1; b = 1; } } printf("%d\n", ans); return 0; } /* 8879 */
完全二叉树的权值
求二叉树每一层的权值和,问:哪个深度的权值和最大,输出深度更小的那个最大值。
我的代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=100010; int ans[N],a[N]; ll qpow(ll a,int b){ ll ans=1; while(b){ if(b&1) ans=ans*a; a*=a; b>>=1; } return ans; } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } int l=0; while(qpow(2,l)<=n){ l++; } int maxn=-1,ans=0; for(int i=0;i<l;i++){ int sum=0; for(int j=qpow(2,i);j<qpow(2,i+1);j++){ sum+=a[j]; if(sum>maxn) { maxn=sum; ans=i+1; } } } cout<<ans<<endl; }
江大佬代码:
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; typedef long long ll; const int N=1e5+7; ll a[N]; int main() { ll n; while(~scanf("%lld",&n)) { for(int i=1; i<=n; ++i)scanf("%lld",&a[i]); ll ans=1,maxx=a[1],p=2,lv=1; while(p<=n) { ++lv; ll now=0; for(int i=0; i<(1<<(lv-1))&&p<=n; ++i)now+=a[p++];//1<<lv表示lv个2相乘,左移一位表示乘以一个2 . if(now>maxx)ans=lv,maxx=now; } printf("%lld\n",ans); } return 0; }
等差数列
第十届蓝桥杯
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define sc(n) scanf("%d",&n) #define print(n) printf("%d\n",n) #define endl "\n"; #define ios ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) const int N=100010; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll INF64 = 0x3f3f3f3f3f3f3f3f; ll qpow(ll a,int b){ll ans=1;while(b){if(b&1) ans=ans*a;a*=a;b>>=1;}return ans;} int ans[N],a[N]; int main(){ int n; cin>>n; for(int i=0;i<n;i++) cin>>a[i]; for(int i=1;i<n;i++){ ans[i]=a[i]-a[-1]; } sort(a,a+n); sort(ans,ans+n-1); int d=__gcd(ans[1],ans[2]);//求等差数列的公差,这题的关键 for(int i=3;i<n;i++){ d=__gcd(ans[i],d); } int res=(a[n-1]-a[0])/d+1; cout<<res<<endl; }