赞
踩
// 转载博客链接
https://blog.csdn.net/weixin_46266058/article/details/123469287
#include<iostream> #define ll long long using namespace std; int ans; void getsi(int x){ while(x){ int t=x%10; if(t==2)ans++; x/=10; } } int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); for(int i=1;i<=2020;i++){ getsi(i); } cout<<ans; return 0; }
读懂题意,不然写完了才发现是错的。
#include<iostream> #include<set> #define ll long long using namespace std; int gcd(int a,int b){ if(a%b==0)return b; return gcd(b,a%b); } int ans; int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); for(int i=1;i<=2020;i++) for(int j=1;j<=2020;j++){ //if(i==j)continue; if(gcd(i,j)==1)ans++; } cout<<ans; return 0; }
#include<iostream> using namespace std; const int N = 40; int arr[N][N]; int main(){ int r = 1; for(int i=1;i<=N;i++){ if(i&1){ for(int x=i,y=1;x>=1&&y<=i;x--,y++){ arr[x][y]=r++; } }else{ for(int y=i,x=1;y>=1&&x<=i;y--,x++){ arr[x][y]=r++; } } } cout<<arr[20][20]; return 0; }
#include<iostream> using namespace std; int ans; int main(){ int month[] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; int num = 0;//总共过了多少天,%7求周一 for(int i=2000;i<=2020;i++){ if(i%100==0||(i%4==0&&i%100!=0))month[2]=29; else month[2]=28; for(int j=1;j<=12;j++){ for(int k=1;k<=month[j];k++){ // num%7 对应 0~6所以等于2的时候就是周一,k==1 表示初一 if(k==1||num%7==2){ ans+=2; }else{ ans+=1; } num++; if(i==2020&&j==10&&k==1){ cout<<ans; return 0; } } } } return 0; }
图的建立,并查集,搜索。
#include<bits/stdc++.h> using namespace std; const int N = 10; int use[N], ans, e[N][N], fa[N]; void init(){ /* 连边建图,e[i][j] == 1表示第i段和第j段灯管相邻 a b c d e f g 1 2 3 4 5 6 7 */ e[1][2]=e[1][6]=1; e[2][1]=e[2][3]=e[2][7]=1; e[3][2]=e[3][7]=e[3][4]=1; e[4][3]=e[4][5]=1; e[5][4]=e[5][7]=e[5][6]=1; e[6][1]=e[6][5]=e[6][7]=1; e[7][2]=e[7][3]=e[7][5]=e[7][6]=1; } int find(int x){ return x==fa[x] ? x : (fa[x]=find(fa[x])); } void dfs(int d){ if(d > 7){ /* 并查集判是否在同一集合 */ for(int i = 1;i <= 7;i++)fa[i] = i;//初始化父亲集合 for(int i = 1;i <= 7;i++) for(int j = 1;j <= 7;j++) if(e[i][j] && use[i] && use[j]){//i和j相邻并且都亮着 int fx = find(i),fy = find(j); if(fx != fy)fa[fx] = fy;//如果不在同一集合,合并 } int k = 0; for(int i = 1;i <= 7;i++) if(use[i] && fa[i] == i)k++; if(k == 1)ans++;//如果所有亮灯都属于同一个集合 return; } use[d] = 1;//打开d这个灯,继续开关下一个灯 dfs(d + 1); use[d] = 0;//关闭d这个灯,继续开关下一个灯 dfs(d + 1); } int main(){ init(); dfs(1); cout << ans; }
#include<iostream> #include<stdio.h> #define ll long long using namespace std; const int N = 1e5+10; int n,a[N]; int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; int x=0,b=0; for(int i=1;i<=n;i++){ if(a[i]>=60)x++; if(a[i]>=85)b++; } int a1=x*1000/n; int t1=a1%10; if(t1>=5)a1=a1/10+1; else a1=a1/10; int a2=b*1000/n; int t2=a2%10; if(t2>=5)a2=a2/10+1; else a2=a2/10; printf("%d%\n%d%\n",a1,a2); return 0; }
题这么长吓我一跳,没想到挺简单的。。。。
#include<iostream> #define ll long long using namespace std; int t1,t2,y1,y2,m1,m2,d1,d2,ans; int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool isy(int x){ return x%400==0||(x%4==0&&x%100!=0); } bool isRve(int y,int m,int d){ int a[]={y/1000,y/100%10,y/10%10,y%10,m/10,m%10,d/10,d%10}; for(int i=0;i<4;i++){ if(a[i]!=a[7-i])return false; } return true; } int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>t1>>t2; y1=t1/10000,y2=t2/10000; m1=t1%10000/100,m2=t2%10000/100; d1=t1-y1*10000-m1*100,d2=t2-y2*10000-m2*100; // cout<<y1<<" "<<m1<<" "<<d1<<endl; // cout<<y2<<" "<<m2<<" "<<d2; for(int year=y1;year<=y2;year++){ for(int mon=(year==y1?m1:1);mon<=(year==y2?m2:12);mon++){ int maxday=(mon==2&&isy(year))?29:d[mon]; for(int day=(year==y1&&mon==m1?d1:1);day<=(year==y2&&mon==m2?d2:maxday);day++){ if(isRve(year,mon,day)){ ans++; } } } } cout<<ans; return 0; }
直接暴力枚举,只能过40%。。。。
#include<iostream> #include<set> #define ll long long using namespace std; string s; int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>s; int n=s.size(); set<char> st; int ans=0; for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ for(int k=i;k<=j;k++){ st.insert(s[k]); } ans+=st.size(); st.clear(); } } cout<<ans; return 0; }
骗了10%。。。。
#include<iostream> using namespace std; int main(){ int n;cin>>n; int a,b; for(int i=0;i<n;i++)cin>>a>>b; int ans=0; switch(n){ case 0:ans=1;break; case 1:ans=2;break; case 2:ans=4;break; case 3:ans=7;break; case 4:ans=11;break; } cout<<ans; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。