当前位置:网站首页>2022夏暑假每日一题(五)
2022夏暑假每日一题(五)
2022-07-27 20:46:00 【摩卡摩卡~】
这一次全部都是考研机试题目。
一、最少交换次数(基础)
任意门
这道题目的题意是让你算出这个有多少个逆序对,然后按照数字的大小顺序输出。
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
int w[N];
int ans[N];
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>w[i];
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
if(w[j]>w[i])
ans[w[i]]++;
int sum=0;
for(int i=1;i<=n;i++)
{
cout<<ans[i]<<" ";
sum+=ans[i];
}
cout<<endl<<sum<<endl;
return 0;
}
二、二叉搜索树(前序、中序、后序)
任意门
这是一个朴素版的BST。
对于每一个根节点而言,他根结点的值都是大于左儿子小于右儿子。
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n;
int l[N],r[N],w[N],idx;
int root;
void insert(int& u,int x)//加了一个&,后面如果对他进行更改,原数值也是会变的
{
if(!u)u=++idx,w[u]=x;
else if(x<w[u])insert(l[u],x);
else if(x>w[u])insert(r[u],x);//如果相等的话,我们是不操作的
}
void dfs(int u,int t)
{
if(!u)return;
if(t==0)cout<<w[u]<<" ";
dfs(l[u],t);
if(t==1)cout<<w[u]<<" ";
dfs(r[u],t);
if(t==2)cout<<w[u]<<" ";
}
int main()
{
cin>>n;
while(n--)
{
int x;
cin>>x;
insert(root,x);
}
for(int i=0;i<3;i++)
{
dfs(root,i);
cout<<endl;
}
return 0;
}
三、日期(模拟)
任意门
给定你一个日期,然后让你说出他的星期数,这道题更加简化了一些,她告诉了你一个前提,并在此前提下进行了一个约束。
#include<bits/stdc++.h>
using namespace std;
int months[]={
0,31,29,31,30,31,30,31,31,30,31,30,31};
string k[]={
"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int main()
{
int m,d;
cin>>m>>d;
int mon=4,day=12,t=4;
while(1)//mon<m||day<d
{
if(mon==m&&day==d){
cout<<k[t-1];return 0;}
t++;
day++;
if(day>months[mon]){
day=1;mon++;}
if(t>7)t=1;
}
return 0;
}
四、水仙花数(枚举)
任意门
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身
这个题目是很经典的语法题,反正我在学习c基础的时候,这道题就被老师反复出题了。
#include<bits/stdc++.h>
using namespace std;
int a[1010];
int f(int x)
{
int res=0;
while(x)
{
int t=x%10;
res+=t*t*t;
x/=10;
}
return res;
}
int main()
{
int m,n,t;
while(cin>>m>>n,m&&n)
{
int t=0;
for(int i=m; i<=n; i++)
{
int ans=f(i);
if(ans==i)a[t++]=i;
}
if(t==0)cout<<"no"<<endl;
else
{
for(int i=0; i<t; i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
return 0;
}
五、最低票价(dp)
任意门
所给的days是要去旅行的日子
#include<bits/stdc++.h>
using namespace std;
const int N=400;
int n;
int days[N],f[N];
int c1,c7,c30;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>days[i];
cin>>c1>>c7>>c30;
for(int i=1,j7=0,j30=0;i<=n;i++)
{
while(days[i]-days[j7+1]>=7)j7++;
while(days[i]-days[j30+1]>=30)j30++;
f[i]=min({
f[i-1]+c1,f[j7]+c7,f[j30]+c30});
}
cout<<f[n]<<endl;
return 0;
}
六、最大连续子序列(dp)
任意门
这也是一个十分经典的问题,这个也是中南大学的考研机试题。
最大连续子序列是所有连续子序列中元素和最大的一个
这道题是以右端点i来记录的,就是从左往右,用f[i]来表示,如果是从右往左,则用g[i]来表示。
由于f[i]的状态只与后面一个数相关,我们其实可以开一个滚动数组来记录。
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n;
int w[N];
int main()
{
while(~scanf("%d",&n))//!=-1
{
for(int i=0;i<n;i++)
scanf("%d",&w[i]);
int res=0,left=0,right=0;
for(int i=n-1,f=0,g=n-1;i>=0;i--)
{
if(f<=0)//说明和是负数,所以肯定不要前面的
{
f=0;
g=i;
}
f+=w[i];//负责算他的最大值
if(f>=res)
{
res=f;
left=i;
right=g;
}
}
printf("%d %d %d\n",res,left,right);
}
return 0;
}
边栏推荐
猜你喜欢

The principle and demonstration of service path lifting without quotation marks

【数字识别】基于知识库实现手写体数字识别附matlab代码

Date的使用

NDK 系列(6):说一下注册 JNI 函数的方式和时机

Deploy dolphin scheduler high availability cluster based on rainbow

Lanproxy映射本地开发环境

Interviewer: I can't carry a backpack at all. Are you going by yourself or I'll give you a lift?
![[image defogging] image defogging based on dark channel and non-mean filtering with matlab code](/img/44/6120682f9571f6ad35f8b9249b7fea.png)
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code

一篇文章读懂人工神经网络

Nature综述:微生物群落形成过程中的优先效应
随机推荐
Zabbix4.0使用SNMP代理方式监控vcenter6.5
Jsonpath: a powerful rule parsing and parameter lookup tool for JSON
微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条
How do I shut down oracle?
The principle and demonstration of service path lifting without quotation marks
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code
[CVA valuation training camp] how to quickly read the annual reports of listed companies - Lesson 4
Preliminary understanding of Panda3D audio and advanced interactive components
ZCMU--1720: 死亡如风,我要装逼
XML 外部实体 (XXE) 漏洞及其修复方法
Winform怎么使用FTP实现自动更新
Keming food: the average increase in the sales price of various series of products is about 5%
Basic SQL DQL
Read an article to understand artificial neural network
Process and planned task management
Test article
股价暴涨180.46%!国产大硅片龙头沪硅产业上市:近4年净利累计不足6000万
常用泰勒展开
采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
技术认证 | 图扑软件携手华为云再创合作共赢新局面