(2)10
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
for(int i=1;i<20;i++)
{
for(int start=1;start<=i;start++)
{
int sum=(start+(start+i))*(i+1)/2-i;
if(sum==100)
{
System.out.println(i);
}
}
}
}
}
2.
答案:416
思路:全排暴力法居然超时了,这不是我认识的蓝桥杯。后面更正为DFS。(跑了4min)
import java.util.*;
public class Main {
static int count=0;
public static boolean check(int a[])
{
int []r=new int[10];
r[0]=1+a[0]+a[1]+a[2];
r[1]=a[3]+a[4]+a[5]+a[6];
r[2]=a[7]+a[8]+a[9]+a[10];
r[3]=a[11]+a[12]+a[13]+a[14];
r[4]=1+a[3]+a[7]+a[11];
r[5]=a[0]+a[4]+a[8]+a[12];
r[6]=a[1]+a[5]+a[9]+a[13];
r[7]=a[2]+a[6]+a[10]+a[14];
r[8]=1+a[4]+a[9]+a[14];
r[9]=a[2]+a[5]+a[8]+a[11];
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(r[i]==r[j])
{
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[] array={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
allSort(array, 0, array.length-1);
System.out.println(count);
}
static void allSort(int[] array,int begin,int end){
if(begin==end){
if(check(array))
{
System.out.println(Arrays.toString(array));
count++;
}
return;
}
for(int i=begin;i<=end;i++){
swap(array,begin,i );
allSort(array, begin+1, end);
swap(array,begin,i );
}
}
static void swap(int[] array,int a,int b){
int tem=array[a];
array[a]=array[b];
array[b]=tem;
}
}
import java.util.*;
public class Main {
private static int count=0;
public static void main(String args[]){
int a[]=new int[16];
a[0]=1;
for(int i=1;i<16;i++)
a[i]=0;//初始化
dfs(a,1);
System.out.print(count);
}
public static void dfs(int a[],int n){
//填充第n个格子
if(n==16){
count++;
}else{
for(int i=2;i<=16;i++){
a[n]=i;
if(check(a,n))
dfs(a,n+1);
}
}
}
public static boolean check(int a[],int n){
boolean f=true;
for(int i=0;i
3.
标题:显示二叉树
排序二叉树的特征是:
某个节点的左子树的所有节点值都不大于本节点值。
某个节点的右子树的所有节点值都不小于本节点值。
为了能形象地观察二叉树的建立过程,小明写了一段程序来显示出二叉树的结构来。
class BiTree
{
private int v;
private BiTree l;
private BiTree r;
public BiTree(int v){
this.v = v;
}
public void add(BiTree the){
if(the.v < v){
if(l==null) l = the;
else l.add(the);
}
else{
if(r==null) r = the;
else r.add(the);
}
}
public int getHeight(){
int h = 2;
int hl = l==null? 0 : l.getHeight();
int hr = r==null? 0 : r.getHeight();
return h + Math.max(hl,hr);
}
public int getWidth(){
int w = (""+v).length();
if(l!=null) w += l.getWidth();
if(r!=null) w += r.getWidth();
return w;
}
public void show(){
char[][] buf = new char[getHeight()][getWidth()];
printInBuf(buf, 0, 0);
showBuf(buf);
}
private void showBuf(char[][] x){
for(int i=0; ip2) buf[y+1][p3] = '\\';
if(l!=null) l.printInBuf(buf,x,y+2);
if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);
}
private int getRootPos(int x){
return l==null? x : x + l.getWidth();
}
}
public class Main
{
public static void main(String[] args)
{
BiTree tree = new BiTree(500);
tree.add(new BiTree(200));
tree.add(new BiTree(509));
tree.add(new BiTree(100));
tree.add(new BiTree(250));
tree.add(new BiTree(507));
tree.add(new BiTree(600));
tree.add(new BiTree(650));
tree.add(new BiTree(450));
tree.add(new BiTree(510));
tree.add(new BiTree(440));
tree.add(new BiTree(220));
tree.show();
}
}
对于上边的测试数据,应该显示出:
答案:buf[y+1][p2+i] = sv.charAt(i)
4.
标题:穿越雷区
X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?
已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
例如:
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
坦克车只能水平或垂直方向上移动到相邻的区。
数据格式要求:
输入第一行是一个整数n,表示方阵的大小, 4<=n<100
接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
A,B都只出现一次。
要求输出一个整数,表示坦克从A区到B区的最少移动步数。
如果没有方案,则输出-1
例如:
用户输入:
5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
则程序应该输出:
10
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
思路:简单DFS
import java.util.*;
public class Main {
static int n;
static int map[][];
static boolean vis[][];
static int nx,ny,tx,ty;
static int x[]={0,0,1,-1};
static int y[]={1,-1,0,0};
static int res=100000000;
public static void dfs(int px,int py,int step)
{
if(px==tx && py==ty)
{
if (step
5.
注意:主类的名字必须是:Main,否则按无效代码处理。
思路:暴力法
import java.util.*;
public class Main {
static String str;
static String str1;
static String str2;
public static boolean is_reT(String s)
{
int l=s.length();
if(l==1)
{
return true;
}
if(l%2==0)
{
return false;
}
int i=0;
while(i!=l-1-i)
{
if(s.charAt(i)!=s.charAt(l-1-i))
{
return false;
}
i++;
}
return true;
}
public static boolean is_not_reT(String s)
{
int l=s.length();
if(l==1)
{
return false;
}
if(l%2==0)
{
return true;
}
int i=0;
while(i!=l-1-i)
{
if(s.charAt(i)!=s.charAt(l-1-i))
{
return true;
}
i++;
}
return false;
}
public static int count1(String s)
{
int n=s.length();
Set set = new HashSet<>();
for(int i=1;i<=n;i++)
{
for(int p=0;p<=n-i;p++)
{
String str=s.substring(p,p+i);
if(is_reT(str))
{
set.add(str);
}
}
}
return set.size();
}
public static int count2(String s)
{
int n=s.length();
Set set = new HashSet<>();
for(int i=1;i<=n;i++)
{
for(int p=0;p<=n-i;p++)
{
String str=s.substring(p,p+i);
if(is_not_reT(str))
{
set.add(str);
}
}
}
return set.size();
}
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
str=in.next();
int max=0;
for(int i=1;imax)
{
max=temp;
}
}
System.out.println(max);
}
}
6.
标题:铺瓷砖
为了让蓝桥杯竞赛更顺利的进行,主办方决定给竞赛的机房重新铺放瓷砖。机房可以看成一个n*m的矩形,而这次使用的瓷砖比较特别,有两种形状,如【图1.png】所示。在铺放瓷砖时,可以旋转。
主办方想知道,如果使用这两种瓷砖把机房铺满,有多少种方案。
【输入格式】
输入的第一行包含两个整数,分别表示机房两个方向的长度。
【输出格式】
输出一个整数,表示可行的方案数。这个数可能很大,请输出这个数除以65521的余数。
【样例输入1】
4 4
【样例输出1】
2
【样例说明1】
这两种方案如下【图2.png】所示:
【样例输入2】
2 6
【样例输出2】
4
【数据规模与约定】
对于20%的数据,1<=n, m<=5。
对于50%的数据,1<=n<=100,1<=m<=5。
对于100%的数据,1<=n<=10^15,1<=m<=6。
资源约定:
峰值内存消耗(含虚拟机) < 512M
CPU消耗 < 8000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
思路:应该是先拆分砖块数,如12=3*4=4*3,再分别考察每种情况是否可行,但是题目貌似把旋转对称的方案也算作不同,先占坑。