填空题:
1 (2022)9 输出 2 * 9^3 + 2 * 9^1 + 2 * 9^0 = 2*729+18+2=1478
2 顺子日期:输出14
依次遍历即可
注意str[]和string的传递
注意sprintf的用法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//注意最前面是0!!!
int is_leap(int year){
if(year%4==0&&year&100||year%400==0) return 1;//是闰年
else return 0;
}
int get_days(int year,int month){
if(month==2) return 28+is_leap(year);
else return months[month];
}
bool check(int year,int month,int day){
if(day<=get_days(year,month)) return 1;
return 0;
}
bool ch(string str){
for(int i=0;i+2<str.size();i++){
if(str[i]+1==str[i+1]&&str[i]+2==str[i+2]) return true;
}
return false;
}
int main(){
char str[8];
int res=0;
for(int m=1;m<=12;m++){
for(int d=1;d<=31;d++){
if(check(2022,m,d)) {
sprintf(str,"%04d%02d%02d",2022,m,d);
if(ch(str)) res++;
}
}
}
cout<<res<<endl;
return 0;
}
编程题
1 刷题统计
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int main(){
LL a,b,n;
cin>>a>>b>>n;
LL sum=0;
LL day=0;
LL k=5*a+2*b;
// cout<<"k="<<k<<endl;
LL cnt=n/k;
day+=7*cnt;
sum+=k*cnt;
// cout<<"day="<<day<<endl;
// cout<<"sum="<<sum<<endl;
LL i=1,j=1;//注意i j 从1开始
while(sum<n&&i<=5){
sum+=a;
i++;
day+=1;
}
// cout<<"day="<<day<<endl;
while(sum<n&&j<=2){
sum+=b;
j++;
day+=1;
}
cout<<day<<endl;
return 0;
}
2 修建灌木(要动脑子想想 不要只傻的一直敲代码)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int m=max(n-i-1,i);
cout<<2*m<<endl;
}
return 0;
}
4 统计子矩阵
二维前缀和 过6/10个数据
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N=10010;
int g[N][N],s[N][N];//s为二维前缀和
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>g[i][j];
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+g[i][j];
}
}
int res=0;
for(int x1=1;x1<=n;x1++){
for(int y1=1;y1<=m;y1++){
for(int x2=x1;x2<=n;x2++){
for(int y2=y1;y2<=m;y2++){
int sum=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
if(sum<=k) res++;
}
}
}
}
cout<<res<<endl;
return 0;
}