题意:卡片有正反面,卡片要求若其一面是 'a', 'e', 'i', 'o' or 'u', 则 令一面必须是'0', '2', '4', '6' or '8'.给出一串卡片的一面,让求需要检查是否符合要求的卡片数,只要检查一面是元音字母的卡片另一面是不是偶数,和检查一面是奇数的另一面是不是元音字母就行
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
char ch[55];
bool ok(int i)
{
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u'||ch[i]=='1'||ch[i]=='3'||ch[i]=='5'||ch[i]=='7'||ch[i]=='9')
return true;
return false;
}
int main()
{
int i,len;
scanf("%s",ch);
len=strlen(ch);
int ans=0;
for(i=0;i<len;i++)
{
if(ok(i))
ans++;
}
printf("%d\n",ans);
return 0;
}
题意:给出迷宫和一串命令,命令是由‘1’~‘3’组成的,0~3分别对应着4个方向,问有多少种对应方案能使得根据命令序列能从起点到达终点
用了两个dfs,第一个用来枚举每一种对应关系,第二个dfs在第一个dfs没枚举出一种情况后进行判断能否到达终点
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
int n,m,ans,ex,ey,sx,sy,len;
char ins[105]; //命令序列
char mp[55][55];
char go[4]; //存放枚举结果,即是当前枚举情况下0~3分别对应go[0]~go[3],go[0]~go[3]在第一个dfs中赋予不同值
char ch[4]={'u','d','l','r'};
bool vis[4]; //四个方向的使用情况
bool ok(int x,int y,int it)
{
if(x<0||x>=n||y<0||y>=m)
return false;
if(mp[x][y]=='#')
return false;
if(x==ex&&y==ey)
return true;
if(it>=len)
return false;
else
{
char temp=go[ins[it]-'0']; //temp即是ins[it]代表的方向
if(temp=='u')
{
x-=1;
}else if(temp=='d')
{
x+=1;
}else if(temp=='l')
{
y-=1;
}else if(temp=='r')
{
y+=1;
}
return ok(x,y,it+1);
}
}
void dfs(int step)
{
if(step==4) //形成一种对应
{
//cout<<"oooooooooooooooooooooook"<<endl;
if(ok(sx,sy,0)) //如果可以到达终点
ans++;
return;
}
int i;
for(i=0;i<4;i++)
{
if(vis[i]==false)
{
vis[i]=true;
go[step]=ch[i];
dfs(step+1);
vis[i]=false;
}
}
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",mp[i]);
for(j=0;j<m;j++)
{
if(mp[i][j]=='S')
{
sx=i; sy=j;
}else if(mp[i][j]=='E')
{
ex=i; ey=j;
}
}
}
scanf("%s",ins);
len=strlen(ins);
ans=0;
memset(vis,false,sizeof(vis));
dfs(0);
printf("%d\n",ans);
return 0;
}
Carol is currently curling.
She has n disks each with radius r on the 2D plane.
Initially she has all these disks above the line y = 10100.
She then will slide the disks towards the line y = 0 one by one in order from 1 to n.
When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk.
Compute the y-coordinates of centers of all the disks after all disks have been pushed.
The first line will contain two integers n and r (1 ≤ n, r ≤ 1 000), the number of disks, and the radius of the disks, respectively.
The next line will contain n integers x1, x2, ..., xn (1 ≤ xi ≤ 1 000) — the x-coordinates of the disks.
Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6.
Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if for all coordinates.
6 2 5 5 6 8 3 12
2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613
The final positions of the disks will look as follows:
In particular, note the position of the last disk.
题意:给出n个半径为r的圆,起初他们都在离轴很远的地方,他们的横坐标为x[i],现在让他们保持横坐标不变,沿着直线x=x[i]往下平移,直到接触x轴或者与其他圆
相切,这里是按照给出的横坐标的顺序往下平移的,
那么对于当前往下平移的圆,只要检查在他下方的圆是不是可能会与他相切,即两者横坐标之差小于等于直径的就有可能相切,在这些有可能相切的圆里面,它一定与纵坐标大的相切,那么通过公式sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0))=2*r,即可求出y1,对于没有圆可以与他相切的情况下,它直接与x轴相切。
题目烦在精度处理,容易出问题
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 1005
int x[M];
double y[M];
int main()
{
int i,j,n,r;
scanf("%d%d",&n,&r);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
y[0]=r;
printf("%.10lf",y[0]);
for(i=1;i<n;i++)
{
double cnt=r;
for(j=0;j<i;j++)
{
int ab=abs(x[j]-x[i]);
if(ab<=2*r)
{
cnt=max(cnt,y[j]+sqrt(4*r*r-ab*ab));
}
}
y[i]=cnt;
printf(" %.20lf",y[i]);
}
return 0;
}
看到Petr写的JAVA:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int r = in.nextInt();
int[] x = new int[n];
double[] y = new double[n];
for (int i = 0; i < n; ++i) {
x[i] = in.nextInt();
double mx = r;
for (int j = 0; j < i; ++j) {
int d = Math.abs(x[j] - x[i]);
if (d <= 2 * r) {
mx = Math.max(mx, y[j] + Math.sqrt(4 * r * r - d * d));
}
}
if (i > 0) out.print(" ");
out.print(String.format("%.10f", mx));
y[i] = mx;
}
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}