目录
试题 A: 九进制转十进制
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
cout<<2+2*9+2*9*9*9<<endl;
//1478
return 0;
}
试题 B: 顺子日期
挺有争议的一道题目
我写的14
试题 C: 刷题统计
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
ll a,b,n;
cin>>a>>b>>n;
ll day=1;
ll i=0;
while(i<n){
if(day%7<=5&&day%7!=0){
i+=a;
day++;
}
else{
i+=b;
day++;
}
}
cout<<day-1<<endl;
return 0;
}
差点写错了,最后检查才发现
while(i<n)不是小于等于
还有最后day要-1
试题 D: 修剪灌木
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
if(n==1){
cout<<1<<endl;
}
else{
for(int i=0;i<n;i++){
if(i<n/2){
arr[i]=(n-i-1)*2;
}
else{
arr[i]=i*2;
}
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
}
return 0;
}
不用模拟,是有规律的一道题目,可以直接算的(应该是的)
试题 E: X 进制减法
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod= 1000000007;
int main(){
int N,Ma,Mb;
cin>>N>>Ma;
int arra[Ma];
for(int i=0;i<Ma;i++){
cin>>arra[i];
}
cin>>Mb;
int arrb[Mb];
for(int i=0;i<Mb;i++){
cin>>arrb[i];
}
int len=Ma;
int x[len];
for(int i=0;i<len;i++){
if(i<Mb){
int temp=max(arra[Ma-i-1],arrb[Mb-i-1]);
if(temp==N-1){
x[Ma-i-1]=N;
}
else if(temp<2){
x[Ma-1-i]=2;
}
else{
x[Ma-1-i]=temp+1;
}
}
else{
int temp=arra[Ma-i-1];
if(temp==N-1){
x[Ma-i-1]=N;
}
else if(temp<2){
x[Ma-1-i]=2;
}
else{
x[Ma-1-i]=temp+1;
}
}
}
int X1[len];
X1[len-1]=1;
for(int i=1;i<Ma;i++){
X1[Ma-i-1]=x[Ma-i]*X1[Ma-i];
}
int A=0,B=0;
for(int i=0;i<Ma;i++){
A+=(X1[Ma-i-1]*arra[Ma-i-1])%mod;
}
for(int i=0;i<Mb;i++){
B+=(X1[Ma-i-1]*arrb[Mb-i-1])%mod;
}
cout<<A-B<<endl;
return 0;
}
试题 F: 统计子矩阵
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
int N,M,K;
cin>>N>>M>>K;
int arr[N][M];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
scanf("%d",&arr[i][j]);
}
}
ll ans=0;
int dp[N][M][N][M];
for(int i1=0;i1<N;i1++){
for(int j1=0;j1<M;j1++){
for(int i2=i1;i2<N;i2++){
for(int j2=j1;j2<M;j2++){
if(i1==i2&&j1==j2){
dp[i1][j1][i2][j2]=arr[i1][j1];
if(dp[i1][j1][i2][j2]<=K){
ans++;
}
continue;
}
if(i2!=0&&j2!=0){
ll temp=0;
temp+=dp[i1][j1][i2-1][j2-1];
for(int i=i1;i<i2;i++){
temp+=arr[i][j2];
}
for(int j=j1;j<j2;j++){
temp+=arr[i2][j];
}
temp+=arr[i2][j2];
dp[i1][j1][i2][j2]=temp;
}
else if(i2==0){
dp[i1][j1][i2][j2]=dp[i1][j1][i2][j2-1]+arr[i2][j2];
}
else {
dp[i1][j1][i2][j2]=dp[i1][j1][i2-1][j2]+arr[i2][j2];
}
if(dp[i1][j1][i2][j2]<=K){
ans++;
}
}
}
}
}
cout<<ans<<endl;
return 0;
}
差不多是动态规划吧,感觉只能过部分样例
试题 G: 积木画
不会写啊,我觉得可能是dp吧
试题 H: 扫雷
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
ll arr1[50000+50][3];
ll arr2[50000+50][3];
ll ans;
ll vis[50000+50];
void dfs(ll x,ll y,ll r){
for(ll j=0;j<n;j++){
if(vis[j]==0){
ll x1=arr1[j][0];
ll y1=arr1[j][1];
ll r1=arr1[j][2];
if((x1-x)*(x1-x)+(y1-y)*(y1-y)<=r*r){
vis[j]=1;
dfs(x1,y1,r1);
ans++;
}
}
}
}
int main(){
cin>>n>>m;
for(ll i=0;i<n;i++){
for(ll j=0;j<3;j++){
scanf("%d",&arr1[i][j]);
}
}
for(ll i=0;i<m;i++){
for(ll j=0;j<3;j++){
scanf("%d",&arr2[i][j]);
}
}
for(ll i=0;i<m;i++){
dfs(arr2[i][0],arr2[i][1],arr2[i][2]);
}
cout<<ans<<endl;
return 0;
}
我用dfs写的
试题 I: 李白打酒加强版
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod=1000000007;
int N,M;
set<string> ans;
void dfs(int jiu,int flow,int dian,string now){
if(flow>M){
return;
}
if(dian>N){
return;
}
if(flow==M&&dian==N&&now[now.length()-1]=='0'&&jiu==0){
ans.insert(now);
return;
}
if(jiu==0){
dfs(jiu*2,flow,dian+1,now+"1");
}
else{
dfs(jiu*2,flow,dian+1,now+"1");
dfs(jiu-1,flow+1,dian,now+"0");
}
}
int main(){
cin>>N>>M;
dfs(2,0,0,"");
cout<<ans.size()%mod<<endl;
return 0;
}
我用dfs写的
试题 J: 砍竹子
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n;
pair<ll,ll> findMax(ll h[]){
ll temp=0;
ll index=0;
for(int i=0;i<n;i++){
if(h[i]>temp){
temp=h[i];
index=i;
}
}
return pair<ll,ll>(temp,index);
}
int main(){
cin>>n;
ll h[n];
for(ll i=0;i<n;i++){
cin>>h[i];
}
ll step=0;
while(findMax(h).first!=1){
ll index=findMax(h).second;
ll num=1;
for(ll i=index;i<n;i++){
if(h[i]==h[i+1]){
num++;
}
else{
break;
}
}
ll temp=h[index]/2;
temp++;
ll temp1=sqrt(temp);
for(ll i=index;i<index+num;i++){
h[i]=temp1;
}
step++;
}
cout<<step<<endl;
return 0;
}