个人能力有限,仅有abcefg六题题解
A门牌制作
暴力
#include<bits/stdc++.h>
using namespace std;
int count(int num){
int ans=0;
if(num==0)return 0;
else{
int temp=num%10;
if(temp==2)ans=1+count(num/10);
else ans=0+count(num/10);
}
return ans;
}
int main(){
int ans=0;
for(int i=1;i<=2020;i++){
ans+=count(i);
}
cout<<ans;
return 0;
}
答案:624
B既约分数
暴力
#include<bits/stdc++.h>
using namespace std;
int ans=0;
int gcd(int i,int j){
if(j==0)return i;
else
return gcd(j,i%j);
}
int main(){
for(int i=1;i<=2020;i++){
for(int j=1;j<=2020;j++){
if(gcd(i,j)==1)ans++;
}
}
cout<<ans;
return 0;
}
c蛇形填数
找规律
第二十行第二十列就在第39层上
而odd层是斜着从下往上数数的
算前38层的数再加上第39层往上数20就是答案
#include<bits/stdc++.h>
using namespace std;
int main(){
int ans=0;
for(int i=1;i<=38;i++){
ans+=i;
}
cout<<ans+20;
return 0;
}
D七段码
用二进制表示发光状态
然后枚举每个状态并结合并查集判断是否联通即可
E平面切割
用f(n,m)表示n个圆m条直线最多能将平面分成的份数。
根据数学推导可得公式:f(n,m)= m^2+n(n+1)/2+2mn-m+1
#include <bits/stdc++.h>
using namespace std;
int n,m,ans;
int main()
{
n = m = 20;
ans = m * m + (n + 1) * n / 2 + 2 * m * n - m + 1;
cout<<ans;
return 0;
}
F成绩分析
照做就行
#include<bits/stdc++.h>
using namespace std;
int ma=0,mi=100;
double avera=0,sum=0;
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n;i++){
int temp;
cin>>temp;
ma=max(ma,temp);
mi=min(mi,temp);
sum+=(double)temp;
}
avera=sum/n;
cout<<ma<<endl<<mi<<endl;
printf("%.2f\n",avera);
return 0;
}
G回文日期
已知10000101<=N<=89991231,则最大答案不会超过90900909(AB回文),最小不会小于10000001,因此可以枚举年份1010-9000.
写一个函数根据回文性质推出月和日,判断该日期是否合法,并记录合法回文日期.
最后二分查找即可。
#include<bits/stdc++.h>
using namespace std;
int N;
vector<int >dates;
vector<int >ABdates;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void check(int year){
int a = year/ 1000;
int b = year / 100 % 10;
int c = year % 100 / 10;
int d = year % 10;
int month = d * 10 + c;
int day = b * 10 + a;
if (month == 0 || month > 12) return;
if (day > days[month]) return;
int t = year * 10000 + d * 1000 + c * 100 + b * 10 + a;
dates .push_back(t);
if (a == c && b == d) ABdates.push_back(t);
return;
}
int findAB(vector<int >ABdates,int x){
int l = 0;
int r=ABdates.size()-1;
int ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(ABdates[mid]<=x){
l=mid+1;
}else{
ans=ABdates[mid];
r=mid-1;
}
}
return ans;
}
int find(vector<int >dates,int x){
int l = 0;
int r=dates.size()-1;
int ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(dates[mid]<=x){
l=mid+1;
}else{
ans=dates[mid];
r=mid-1;
}
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
for(int i=1000;i<=9090;i++){
check(i);
}
cin>>N;
cout<<find(dates,N)<<endl<<findAB(ABdates,N)<<endl;
return 0;
}