1.Post Robot
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5007
解题思路:
题目大意:
给你一个文本,存在“Apple”, “iPhone”, “iPod”, “iPad”的字样输出”MAI MAI MAI!“,碰到”Sony“则输出“SONY DAFA IS GOOD!”
解题思路:
暴力模拟。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[1000005];
char tmp1[] = {"Apple"};
char tmp2[] = {"iPhone"};
char tmp3[] = {"iPod"};
char tmp4[] = {"iPad"};
char tmp5[] = {"Sony"};
char ans1[] = {"MAI MAI MAI!"};
char ans2[] = {"SONY DAFA IS GOOD!"};
int main(){
int l;
while(gets(str)){
l = strlen(str);
for(int i = 0; i <= l-4; i++){
if(i <= l-5 && strncmp(str+i,tmp1,5) == 0)
printf("%s\n",ans1);
else if(i <= l-6 && strncmp(str+i,tmp2,6) == 0)
printf("%s\n",ans1);
else if(strncmp(str+i,tmp3,4) == 0)
printf("%s\n",ans1);
else if(strncmp(str+i,tmp4,4) == 0)
printf("%s\n",ans1);
else if(strncmp(str+i,tmp5,4) == 0)
printf("%s\n",ans2);
}
}
return 0;
}
5.Game(博弈)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5011
解题思路:
Nim博弈:http://blog.csdn.net/piaocoder/article/details/48472995
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
while(~scanf("%d",&n)){
int x,sum = 0;
for(int i = 0; i < n; i++){
scanf("%d",&x);
sum ^= x;
}
if(sum == 0)
printf("Lose\n");
else
printf("Win\n");
}
return 0;
}
6.Dice(模拟+bfs)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5012
解题思路:
题目大意:
给你两个色子,规定每个面的序号,这两个色子的每个面的颜色分别为:(a1,a2,a3,a4,a5,a5)(b1,b2,b3,b4,b5,b6),现在要你通过
四种旋转使得a序列和b序列一样。可以的话输出最少的旋转步数,不能输出-1.
解题思路:
模拟即可。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int aim;
struct node{
int s[10];
int sum;
int step;
}a,b;
int vis[700000];
int base[10] = {0,100000,10000,1000,100,10,1};
int bfs(){
struct node n1, n2;
int i,j,sum;
queue<node>q;
q.push(a);
while(!q.empty()){
n1 = q.front();
q.pop();
if(n1.sum == aim)
return n1.step;
for(i = 1; i <= 4; i++){
sum = 0;
for(j = 1; j <= 6; j++)
n2.s[j] = n1.s[j];
if(i == 1){
n2.s[1] = n1.s[6];
n2.s[2] = n1.s[5];
n2.s[5] = n1.s[1];
n2.s[6] = n1.s[2];
}
else if(i == 2){
n2.s[1] = n1.s[5];
n2.s[2] = n1.s[6];
n2.s[5] = n1.s[2];
n2.s[6] = n1.s[1];
}
else if(i == 3){
n2.s[1] = n1.s[4];
n2.s[2] = n1.s[3];
n2.s[3] = n1.s[1];
n2.s[4] = n1.s[2];
}
else{
n2.s[1] = n1.s[3];
n2.s[2] = n1.s[4];
n2.s[3] = n1.s[2];
n2.s[4] = n1.s[1];
}
for(j = 1; j <= 6; j++)
sum += n2.s[j] * base[j];
if(!vis[sum]){
n2.step = n1.step + 1;
n2.sum = sum;
vis[sum] = 1;
q.push(n2);
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d%d%d%d%d",&a.s[1],&a.s[2],&a.s[3],&a.s[4],&a.s[5],&a.s[6])){
memset(vis,0,sizeof(vis));
int sum = 0;
for(int i = 1; i <= 6; i++)
sum += a.s[i] * base[i];
a.step = 0;
a.sum = sum;
vis[sum] = 1;
for(int i = 1; i <= 6; i++)
scanf("%d",&b.s[i]);
aim = 0;
for(int i = 1; i <= 6; i++)
aim += b.s[i] * base[i];
printf("%d\n",bfs());
}
return 0;
}
8.Number Sequence(贪心+位处理)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5014
解题思路:
贪心枚举即可。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[100005];
int b[100005];
int vis[100005];
int main(){
int n;
while(scanf("%d",&n) != EOF){
memset(vis,0,sizeof(vis));
for(int i = 0; i <= n; i++)
scanf("%d",&a[i]);
for(int i = n; i >= 0; i--){
int tmp = 0;
if(!vis[i]){
for(int j = 0; ; j++){
if(!(i&(1<<j)))
tmp += (1<<j);
if(tmp >= i){
tmp -= (1<<j);
break;
}
}
vis[i] = tmp;
vis[tmp] = i;
}
}
printf("%lld\n",(ll)(n)*(n+1));
for(int i = 0; i < n; i++)
printf("%d ",vis[a[i]]);
printf("%d\n",vis[a[n]]);
}
return 0;
}
11.Ellipsoid
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5017
解题思路:
http://blog.csdn.net/piaocoder/article/details/39398507
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double r = 0.9; //降温速度
const int dx[] = {-1,0,1,-1,1,-1,0,1},dy[] = {1,1,1,0,0,-1,-1,-1};
double a,b,c,d,e,f;
double dis(double x,double y,double z){
return sqrt(x*x+y*y+z*z);
}
//已知x,y;求z(把x,y看做常量,z看做变量,求解普通的一元二次方程即可)
double getZ(double x,double y){
double tmp = (d*y+e*x)*(d*y+e*x)-4*c*(a*x*x+b*y*y+f*x*y-1);
if(tmp < 0)
return 10; //返回一个大于2的值即可,
double z1 = (-(d*y+e*x)+sqrt(tmp))/(2*c);
double z2 = (-(d*y+e*x)-sqrt(tmp))/(2*c);
if(z1*z1 <= z2*z2)
return z1;
else
return z2;
}
//模拟退火算法
double solve(){
double step = 1;
double x = 0,y = 0,z,nx,ny,nz;
double ans = INF,sum;
int flag;
while(step > eps){
flag = 1;
while(flag){
flag = 0;
for(int i = 0; i < 8; i++){
nx = x+dx[i]*step;
ny = y+dy[i]*step;
nz = getZ(nx,ny);
if(nz > 2) //此处应是一个大于1的值最好是2以上,因为double
continue;
sum = dis(nx,ny,nz);
if(ans > sum){
x = nx;y = ny;
ans = sum;
flag = 1;
}
}
}
step *= r;
}
return ans;
}
int main(){
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF)
printf("%.8lf\n",solve());
}