题目链接在这里
A.数学题,(附上草稿,不要嫌弃字丑),事实上就是这么化成一个数学式子
代码如下:
#include <iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<fstream>
#include<queue>
#include<stack>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 1e3+5;
const int inf = 0x3f3f3f3f;
const int maxx = 1e3+50;
int main()
{
int T;
int tt = scanf("%d",&T);
for(int ka = 1; ka<=T; ka++)
{
int x1,x2,y1,y2;
int u1,u2,v1,v2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
scanf("%d%d%d%d",&u1,&v1,&u2,&v2);
int a,b,c,d;
double ans = 0;
a = x1-x2; b = u1-u2; c = y1 - y2; d = v1-v2;
if(a*b+c*d > 0)
{
ans = sqrt((double)a*a + (double)c*c);
// ans = ans + 5e-7;
}
else
{
double s1 = (double)a*b + (double)c*d;
double s2 = (double)b*b + (double)d*d;
double s3 = (double)a*a + (double)c*c;
double x = ( s1/s2 );
x = -x;
ans = (sqrt(s2*x*x + 2.0*s1*x + s3));
}
// ans = ans*1e7;
// ans = floor(ans);
//long long res = ans;
//if(res%10>=5)
// res+=10;
//ans = res;
//ans = floor(ans/10.0);
//ans/=1e6;
printf("Case %d: %.6f\n",ka,ans);
}
return 0;
}
之前一直wa,以为是浮点数四舍五入的问题,所以多写了最后面注释的问题,wa的原因是不小心把c*c写成了b*b
。。。最近很不在状态,写什么wa什么,全是 这种低级错误,真的是需要好好找状态了。
B. a+b 模拟题
#include <iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<fstream>
#include<queue>
#include<stack>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 1e3+5;
const int inf = 0x3f3f3f3f;
const int maxx = 1e3+50;
int n,m;
string num[10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
map<string,int> mp;
int main()
{
for(int i=0; i<=9; i++)
{
mp[num[i]] = i;
}
string x;
while(getline(cin,x))
{
string xa = "", xb="";
int a = 0;
int b = 0;
int ok = 0;
int ten = 1;
for(int i=0; i<x.size(); i++)
{
if(x[i]=='=')
break;
if(x[i]=='+')
{
xb = "";
ten = 1;
ok++;
i++;
continue;
}
if(ok==0)
{
if(x[i]==' ')
{
a = mp[xa]+ a*ten;
ten*=10;
xa.clear();
}
else
xa+=x[i];
// cout<<xa<<" "<<a<<endl;
}
else
{
if(x[i]==' ')
{
b = mp[xb]+b*ten;
ten*=10;
xb.clear();
}
else
xb+=x[i];
//cout<<xb<<" "<<b<<endl;
}
}
if(a+b==0)
break;
printf("%d\n",a+b);
}
return 0;
}
C:bfs 可以搜HDU1728 ,附上一个别人的代码 (有点懒,暂时不想写):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
char str[105][105];
int id[105][105];
int n,m;
int k,x1,y5,x2,y2;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int flag;
struct node
{
int x,y,k;
};
void bfs()
{
node t;
t.x=x1;
t.y=y5;
t.k=-1;
id[x1][y5]=1;
queue<node> q;
q.push(t);
while(q.empty()==0)
{
node t1;
t1=q.front();
q.pop();
if(t1.x==x2&&t1.y==y2&&t1.k<=k)
{
//cout<<t1.k<<endl;
flag=true;
}
for(int i=0;i<4;i++)
{
node t2;
t2.x=t1.x+dir[i][0];
t2.y=t1.y+dir[i][1];
while(t2.x>=1&&t2.x<=n&&t2.y>=1&&t2.y<=m&&str[t2.x][t2.y]!='*')
{
if(id[t2.x][t2.y]==0)
{
t2.k=t1.k+1;
id[t2.x][t2.y]=1;
q.push(t2);
}
node t3;
t3.x=t2.x+dir[i][0];
t3.y=t2.y+dir[i][1];
t2=t3;
}
}
}
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf(" %c",&str[i][j]);
}
}
memset(id,0,sizeof(id));
scanf("%d%d%d%d%d",&k,&y5,&x1,&y2,&x2);
flag=false;
bfs();
if(flag==true)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
D,贪心
一开始把题目看错了,以为是背包问题,注意题目里 “现在你想买至少 L毫升的可乐 ”,这个“至少”说明我们不用恰好买L毫升,也就是说我们可以买超,所以我们将所有的可乐安装单位价值从小到大进行排序,我们先取单位价值最小的,直到够L为止。
注意一点,因为可以超过L,所以我们再买的时候需要判断当我们买超的时候是不是也是最优解。
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 1e2+50;
struct node
{
int c,l;
double di;
node(int cc=0,int vv=0,double dii=0)
{
c = cc; l = vv; di = dii;
}
bool operator<(const node &s)const
{
return di <s.di;
}
};
node s[maxn];
int main(){
int n,L;
while(scanf("%d%d",&n,&L)==2)
{
for(int i=0; i<n; i++)
{
int tt = scanf("%d",&s[i].c);
s[i].l = 1<<i;
s[i].di = (double)s[i].c / s[i].l;
}
sort(s,s+n);
long long rel = L;
long long ans = 0;
long long cnt = 0;
for(int i=0; i<n; i++)
{
cnt = cnt + rel/s[i].l* s[i].c;
if(i==0)
ans = cnt + s[i].c;
if(rel%s[i].l == 0)
{
ans = min(ans,cnt);
break;
}
else
{
rel = rel % s[i].l;
ans = min(ans,cnt + s[i].c); // 多买一瓶
}
}
printf("%lld\n",ans);
}
return 0;
}