目录
这道题是经典的三道门概率问题(传送门:经典数学问题——三门问题(数据分析面试题)_仲长杰秀的博客-CSDN博客_三门问题),换之后的概率永远大于等于换之前,当且仅当没有盒子被打开的时候换不换的概率就是一样的了那么答案就很明显了。当m为零的时候换不换都一样,否则都要换。
ac代码:
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
if(m==0)
{
cout<<"no thing"<<endl;
}
else
{
cout<<"YES"<<endl;
}
return 0;
}
这道题承接上题也属于三道门问题,不同的是上题不需要求概率公式这题需要求。但是简单版能理解的话推出来公式也是非常简单的(bushi?)。其实就是高中学过的条件概率问题啦。p(a)=(n-1)/n(未被锁定盒子里面有小飞棍的总概率),p(b)=1/(n-m-1)(打开m个盒子后每个盒子所占总概率的份数)。那么显而易见最大概率公式为:p(a)*p(b)=(n-1)/((n-m-1)*n),再注意一下精度问题就好ok啦。
ac代码:
#include<iostream>
using namespace std;
int main() {
double n,m;
cin>>n>>m;
printf("%.6lf",((n-1.0)/n)*(1.0/(n-m-1.0)));
}
纯纯的模拟题,只要按照图去推导再结合位运算的知识就ok啦,这里就不过多解释了。
ac代码:
#include<iostream>
using namespace std;
int main() {
int a,b,c,d;
cin>>a>>b>>c>>d;
cout << (((a ^ b) & (c | d)) ^ ((b & c) | (a ^ d))) << endl;
}
签到题,注意一下最后是从那一栋楼到那一栋楼就好了,注意一下最后的状态。
ac代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
long long m,k,a,b,x;
cin>>m>>k>>a>>b;
if(m%k==0) x=m/k;
else x=m/k+1;
cout<<(a+b)*x<<endl;
return 0;
}
这是一道模拟题,根据它的题意模拟一下就可以,可以运用哈希思想标记一下方向,走过之后更改方向就行了,并不是很难。
ac代码:
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stack>
#include <queue>
using namespace std;
int n,m,k,c,a[1009][1009];
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&a[i][j]);
}
}
while(k--)
{
scanf("%d",&c);
int ans=0,x=1,y=c;
while(x<=n)
{
if(a[x][y]==1)
{
a[x][y]=2;
y++;
}
if(a[x][y]==2)
x++;
if(a[x][y]==3)
{
a[x][y]=2;
y--;
}
}
printf("%d ",y);
}
return 0;
}
这是一道cf的思维题,不是很难(bushi),再次是有很多人写过题解的了,这里直接传送门:Codeforces Round #731 (Div. 3) E. Air Conditioners思维_半醒之间.的博客-CSDN博客
ac代码:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdio.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<numeric>
#include<stack>
using namespace::std;
typedef long long ll;
int n,t,q;
int a[300005];
int b[300005];
ll ans[300005];
ll min(ll x,ll y){
if(x<y)
return x;
return y;
}
void solv(){
cin>>n>>q;
for (int i =0; i<=n; i++) {
ans[i]=1e17+5;
}
for (int i =0; i<q; i++) {
cin>>a[i];
}
for (int i =0; i<q; i++) {
cin>>b[i];
}
for (int i =0; i<q; i++) {
ans[a[i]]=min(ans[a[i]], b[i]);
}
for (int i =1; i<=n; i++) {
if (ans[i]>ans[i-1]+1) {
ans[i]=ans[i-1]+1;
}
}
for (int i =n-1; i>0; i--) {
if (ans[i]>ans[i+1]+1) {
ans[i]=ans[i+1]+1;
}
}
for (int i =1; i<=n; i++) {
printf("%lld ",ans[i]);
}printf("\n");
}
int main(){
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
cin>>t;
while (t--) {
solv();
}
return 0;
}
这题可以二分或者利用数学解答,数据范围很大肯定是不能暴力的。
用二分就是板子:先搜索一个L让L*L>=a,然后再搜索一个R让R*R<=b最后让R-L+1就是最后答案喽。
数学思维也不是很难:先判断两边是不是能开方,能开方的话直接开方当成边界,不能的话让左边加一,右边直接sqrt(b)就好了;最后也是求出来一个L和一个R,答案就是R-L+1.
二分ac代码:
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
ll R=0,L=0;
ll l=0,r=1e9;
while(l<r)
{
ll mid=(l+r+1)>>1;
if(mid*mid<=b) l=mid;
else r=mid-1;
}
R=l;
l=0,r=1e9;
while(l<r)
{
ll mid=(l+r)>>1;
if(mid*mid>=a) r=mid;
else l=mid+1;
}
L=l;
cout<<R-L+1<<endl;
}
return 0;
}
数学ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<algorithm>
const int N=1e5+5;
typedef long long ll;
using namespace std;
unsigned long long a,b,n;
void slove(){
cin>>a>>b;
unsigned long long x=sqrt(a);
unsigned long long y=sqrt(b);
if(x*x==a) cout<<y-x+1<<endl;
else cout<<y-x<<endl;
}
int main()
{
int t;cin>>t;
while(t--)
slove();
return 0;
}
贪心+差分;
贪心策略为若a[i]>a[i-1],计数器sum+=a[i]-a[i-1];
为啥这样贪心是对的腻?由于过于懒大家可以直接看原题题解,传送门:登录 - 洛谷。
ac代码:
#include<bits/stdc++.h>
using namespace std;
int n,a[100005];
long long ans=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=2;i<=n;i++) if(a[i]>a[i-1]) ans+=a[i]-a[i-1];
cout<<ans+a[1];
return 0;
}
签到题,直接判断一下就可(别傻傻的忘了换行),不过多赘述了,直接上代码。
ac代码:
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,c;
cin>>a>>b>>c;
if(a*b==c||a*c==b||b*c==a) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
这是道思维题,可以从六个方向的视图看他每个视图都有多大面积,最后加到一起就可以了。
ac代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<math.h>
#include<string.h>
#include<vector>
using namespace std;
typedef long long ll;
int mp[1005][1005];
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
int x=max(a,c)+max(b,d);
int y=max(a,b)+max(c,d);
int z=0;
if(a!=0) z++;
if(b!=0) z++;
if(c!=0) z++;
if(d!=0) z++;
cout<<(x+y+z)*2<<endl;
return 0;
}
这是一道19年蓝桥杯初赛原题,用贪心思想就ok;
俺太懒了,直接来传送门吧:[蓝桥杯2019初赛]外卖店优先级_凯撒袁六兽的博客-CSDN博客
ac代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct node
{
int ts,num;
}a[N];
bool cmp(node s1,node s2)
{
if(s1.ts==s2.ts)return s1.num<s2.num;
return s1.ts<s2.ts;
}
int n,m,t,ans;
int last[N],sum[N];//第几号店的上一次时间,记录当前第几号店的值
bool st[N];//是否在优先缓存队列中
int main()
{
int tt=0;//记录上一个时间
scanf("%d%d%d",&n,&m,&t);
for(int i=1;i<=m;i++)scanf("%d%d",&a[i].ts,&a[i].num);
sort(a+1,a+1+m,cmp);
for(int i=1;i<=m;i++)
{
int tt,id;
tt=a[i].ts;id=a[i].num;
if(tt!=last[id])sum[id]-=tt-last[id]-1;
if(sum[id]<0)sum[id]=0;
if(sum[id]<=3)st[id]=0;
sum[id]+=2;
if(sum[id]>5)st[id]=1;
last[id]=tt;
}
for(int i=1;i<=n;i++)
{
if(last[i]<t)sum[i]-=t-last[i];
if(sum[i]<=3)st[i]=0;
}
for(int i=1;i<=n;i++)
{
if(st[i])ans++;
}
cout<<ans<<endl;
return 0;
}
这是一道思维+字符串的题,如果正向遍历字符串的话会很麻烦,那我们为什么不反向遍历字符串呢?先反向遍历字符串,然后再保存解密后的字符,再反向输出就行了。
ac代码:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
char a[1005],s[1005];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,p=0;
cin>>n;
cin>>s;
for(int i=n-1;i>=0;i--)
{
if(s[i]=='0')
{
int cnt1=(s[i-1]-'0')+(s[i-2]-'0')*10;
a[p++]=cnt1-1+'a';
i-=2;
}
else
{
a[p++]=s[i]-'0'-1+'a';
}
}
for(int i=p-1;i>=0;i--)
cout<<a[i];
cout<<endl;
memset(a,0,sizeof(a));
memset(s,0,sizeof(s));
}
return 0;
}
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳