HDOJ 1015   Safecracker
2015-12-18 18:08
295 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1015
暴力一点还是可以的....
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
int f(char a,char b,char c,char d,char e)
{
char
aa[5]={a,b,c,d,e};
for(int
i=0;i<5;i++)
for(int j=i+1;j<5;j++)
if(aa[i]==aa[j])
return 0;
return
1;
}
int main()
{
char
a[30];
long int
n;
while(scanf("%ld%s",&n,a),(n||strcmp(a,"END")!=0))
{
long int s=0,flag=1,temp;
sort(a,a+strlen(a));
for(int
i=strlen(a)-1;i>=0&&flag;i--)
{
s=0;
s+=(a[i]-'A'+1);
for(long int
j=strlen(a)-1,temp1=s;j>=0&&flag;j--)
{
s=temp1-(a[j]-'A'+1)*(a[j]-'A'+1);
for(long int
k=strlen(a)-1,temp2=s;k>=0&&flag;k--)
{
temp=(a[k]-'A'+1);
s=temp2+temp*temp*temp;
for(long int
l=strlen(a)-1,temp3=s;l>=0&&flag;l--)
{
temp=(a[l]-'A'+1);
s=temp3-temp*temp*temp*temp;
for(long int m=strlen(a)-1,temp4=s;m>=0;m--)
{
temp=(a[m]-'A'+1);
s=temp4+temp*temp*temp*temp*temp;
if(s==n&&f(a[i],a[j],a[k],a[l],a[m])==1)
{
printf("%c%c%c%c%c\n",a[i],a[j],a[k],a[l],a[m]);
flag=0;
break;
}
}
}
}
}
}
if(flag)
{
printf("no solution\n");
}
}
return
0;
}
暴力一点还是可以的....
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
int f(char a,char b,char c,char d,char e)
{
char
aa[5]={a,b,c,d,e};
for(int
i=0;i<5;i++)
for(int j=i+1;j<5;j++)
if(aa[i]==aa[j])
return 0;
return
1;
}
int main()
{
char
a[30];
long int
n;
while(scanf("%ld%s",&n,a),(n||strcmp(a,"END")!=0))
{
long int s=0,flag=1,temp;
sort(a,a+strlen(a));
for(int
i=strlen(a)-1;i>=0&&flag;i--)
{
s=0;
s+=(a[i]-'A'+1);
for(long int
j=strlen(a)-1,temp1=s;j>=0&&flag;j--)
{
s=temp1-(a[j]-'A'+1)*(a[j]-'A'+1);
for(long int
k=strlen(a)-1,temp2=s;k>=0&&flag;k--)
{
temp=(a[k]-'A'+1);
s=temp2+temp*temp*temp;
for(long int
l=strlen(a)-1,temp3=s;l>=0&&flag;l--)
{
temp=(a[l]-'A'+1);
s=temp3-temp*temp*temp*temp;
for(long int m=strlen(a)-1,temp4=s;m>=0;m--)
{
temp=(a[m]-'A'+1);
s=temp4+temp*temp*temp*temp*temp;
if(s==n&&f(a[i],a[j],a[k],a[l],a[m])==1)
{
printf("%c%c%c%c%c\n",a[i],a[j],a[k],a[l],a[m]);
flag=0;
break;
}
}
}
}
}
}
if(flag)
{
printf("no solution\n");
}
}
return
0;
}
相关文章推荐
- package.json依赖管理dependencies中 ^ 和 ~ 的区别
- 【jQuery】使用getScript()方法异步加载并执行js文件
- Ext.js5的表格插件—展开和冻结和Ext.XTemplate(14)
- 摘抄的一些js
- 用JavaScript动态加载CSS和JS文件
- html list列表
- web前端
- prufer序列
- js 创建对象的三种方式——工厂模式、构造函数模式、原型模式
- jsp实现ajax的一个例子
- JS设置cookie,删除cookie
- web前端学习摘要
- jQuery中width()、innerWidth()、outerWidth()的区别
- JSON的读取与写入
- JSP Servlet 上传
- 使用 CSS3 实现超炫的 Loading(加载)动画效果
- angularjs的事件 $broadcast and $emit and $on
- The JSR-133 Cookbook for Compiler Writers
- JavaScript面向对象编程(10)高速构建继承关系之对象拷贝
- arcgis api for javascript (2) 获取属性字段及自定义graphic样式