第六届蓝桥杯javaC组真题

隔行变色

Excel表的格子很多,为了避免把某行的数据和相邻行混淆,可以采用隔行变色的样式。

小明设计的样式为:第1行蓝色,第2行白色,第3行蓝色,第4行白色,....

现在小明想知道,从第21行到第50行一共包含了多少个蓝色的行。

请你直接提交这个整数,千万不要填写任何多余的内容。

/**
 * @createDate:2019年3月15日 上午8:04:30
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:隔行变色.java @describe:
 */
public class 隔行变色 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count = 0;
		for (int i = 21; i <= 50; i++) {
			if (i % 2 != 0) {
				count++;
			}
		}
		System.out.println(count);
	}
}


立方尾不变

有些数字的立方的末尾正好是该数字本身。

比如:1,4,5,6,9,24,25,....

请你计算一下,在10000以内的数字中(指该数字,并非它立方后的数值),符合这个特征的正整数一共有多少个。

请提交该整数,不要填写任何多余的内容。

/**
 * @createDate:2019年3月15日 上午8:07:09
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:立方尾不变s.java @describe:
 */
public class 立方尾不变s {

	static long bl;
	static int count = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 1; i <= 10000; i++) {
			if (fun(i)) {
				System.out.print(i + " ");
				count++;
			}
		}
		System.out.println();
		System.out.println(count);
	}

	public static boolean fun(int n) {
		bl = (long) n * n * n;
		while (n > 0) {
			if (n % 10 != bl % 10) {
				return false;
			}
			n = n / 10;
			bl = (long) bl / 10;
		}
		return true;
	}
}


无穷分数

无穷的分数,有时会趋向于固定的数字。

请计算【图1.jpg】所示的无穷分数,要求四舍五入,精确到小数点后5位,小数位不足的补0。

请填写该浮点数,不能填写任何多余的内容。

/**
 * @createDate:2019年3月15日 上午8:18:05
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:无穷分数.java @describe:
 */
public class 无穷分数 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.printf("%.5f", fun(1));
	}

	public static double fun(int n) {
		if (n == 10) {
			return 0;
		}
		return n / (n + fun(n + 1));
	}
}


循环节长度

两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。

比如,11/13=6=>0.846153846153.....  其循环节为[846153] 共有6位。

下面的方法,可以求出循环节的长度。

 

请仔细阅读代码,并填写划线部分缺少的代码。

         public static int f(int n, int m)

         {

                   n = n % m;       

                   Vector v = new Vector();

                  

                   for(;;)

                   {

                            v.add(n);

                            n *= 10;

                            n = n % m;

                            if(n==0) return 0;

                            if(v.indexOf(n)>=0)  _________________________________ ;  //填空

                   }

         }

import java.util.Vector;

/**
 * @createDate:2019年3月15日 上午8:30:45
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:循环节长度s.java @describe:
 */
public class 循环节长度s {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(f(11, 13));
	}

	public static int f(int n, int m) {
		n = n % m;
		Vector v = new Vector();
		for (;;) {
			v.add(n);
			n *= 10;
			n = n % m;
			if (n == 0)
				return 0;
			// 如果第二次余数n存在,则返回下标索引值
			if (v.indexOf(n) >= 0)
				// 然后让总长减去前一个余数的下标就是循环节长度
				return v.size() - v.indexOf(n); // 填空
		}
	}
}


格子中输出

stringInGrid方法会在一个指定大小的格子中打印指定的字符串。

要求字符串在水平、垂直两个方向上都居中。

如果字符串太长,就截断。

如果不能恰好居中,可以稍稍偏左或者偏上一点。

 

下面的程序实现这个逻辑,请填写划线部分缺少的代码。

 

         public static void stringInGrid(int width, int height, String s)

         {

                   if(s.length()>width-2) s = s.substring(0,width-2);

                   System.out.print("+");

                   for(int i=0;i<width-2;i++) System.out.print("-");

                   System.out.println("+");

                  

                   for(int k=1; k<(height-1)/2;k++){

                            System.out.print("|");

                            for(int i=0;i<width-2;i++) System.out.print(" ");

                            System.out.println("|");

                   }

                  

                   System.out.print("|");

                  

                   String ff = ___________________________________________;  //填空

                   System.out.print(String.format(ff,"",s,""));

                            

                   System.out.println("|");

                  

                   for(int k=(height-1)/2+1; k<height-1; k++){

                            System.out.print("|");

                            for(int i=0;i<width-2;i++) System.out.print(" ");

                            System.out.println("|");

                   }       

                  

                   System.out.print("+");

                   for(int i=0;i<width-2;i++) System.out.print("-");

                   System.out.println("+"); 

         }

 

对于题目中数据,应该输出:

+------------------+

|                  |

|     abcd1234     |

|                  |

|                  |

+------------------+

 

(如果出现对齐问题,参看【图1.jpg】)

/**
 * @createDate:2019年3月15日 下午4:28:39
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:格子中输出.java @describe:
 */
public class 格子中输出 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		stringInGrid(14, 13, "dasdasda");
	}

	public static void stringInGrid(int width, int height, String s) {
		if (s.length() > width - 2)
			s = s.substring(0, width - 2);
		System.out.print("+");
		for (int i = 0; i < width - 2; i++)
			System.out.print("-");
		System.out.println("+");

		for (int k = 1; k < (height - 1) / 2; k++) {
			System.out.print("|");
			for (int i = 0; i < width - 2; i++)
				System.out.print(" ");
			System.out.println("|");
		}

		System.out.print("|");

		String ff = "%" + ((width - s.length() - 1) / 2) + "s%s%" + (width - s.length() - 2) / 2 + "s"; // 填空
		System.out.print(String.format(ff, "", s, ""));

		System.out.println("|");

		for (int k = (height - 1) / 2 + 1; k < height - 1; k++) {
			System.out.print("|");
			for (int i = 0; i < width - 2; i++)
				System.out.print(" ");
			System.out.println("|");
		}

		System.out.print("+");
		for (int i = 0; i < width - 2; i++)
			System.out.print("-");
		System.out.println("+");
	}
}

答案:String ff = "%" + ((width - s.length() - 1) / 2) + "s%s%" + (width - s.length() - 2) / 2 + "s"; // 填空


奇妙的数字

小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。

你能猜出这个数字是多少吗?

请填写该数字,不要填写任何多余的内容。

/**
 * @createDate:2019年3月15日 下午5:20:20
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:奇妙的数字.java @describe:
 */
public class 奇妙的数字 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String zz;
		String xx;
		for (int i = 0; i <= 100; i++) {
			zz = String.valueOf(i * i);
			xx = String.valueOf(i * i * i);
			if (zz.length() + xx.length() == 10) {
				System.out.println(i + ": " + zz + xx);
			}
		}
	}
}

加法变乘法

我们都知道:1+2+3+ ... + 49 = 1225

现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015

比如:

1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015

就是符合要求的答案。

请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。

注意:需要你提交的是一个整数,不要填写任何多余的内容。

/**
 * @createDate:2019年3月15日 下午5:31:51
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:加法变乘法.java @describe:
 */
public class 加法变乘法 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num, numt;
		long startTime = System.currentTimeMillis(); // 获取开始时间

		for (int i = 1; i <= 49; i++) {
			num = i * (i + 1);
			for (int j = i + 1; j <= 49; j++) {
				numt = j * (j + 1);
				if (1225 + num + numt - i - (i + 1) - j - (j + 1) == 2015) {
					System.out.println(i + " " + j);
				}
			}
		}

		long endTime = System.currentTimeMillis(); // 获取结束时间

		System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
	}
}


移动距离

X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3...

当排满一行时,从下一行相邻的楼往反方向排号。

比如:当小区排号宽度w为6时,开始情形如下:

1  2  3  4  5  6

12 11 10  9  8  7

13 14 15  16 17 18

24 23 22  21 20 19

我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动)

输入为3个整数w m n,空格分开,都在1到10000范围内

要求输出一个整数,表示m n 两楼间最短移动距离。

例如:

用户输入:

6 8 2

则,程序应该输出:

4

再例如:

用户输入:

4 7 20

则,程序应该输出:

5

资源约定:

峰值内存消耗(含虚拟机) < 256M

CPU消耗  < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码处理。

import java.util.Scanner;

/**
 * @createDate:2019年3月15日 下午5:56:30
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:移动距离.java @describe:
 */
public class 移动距离 {

	static int x1, y1, x2, y2;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int m = sc.nextInt();
		int n = sc.nextInt();

		GetPos(w, n, m);
		int sum = Math.abs(x1 - x2) + Math.abs(y1 - y2);
		System.out.println(sum);
	}

	public static void GetPos(int w, int n, int m) {
		x1 = (n - 1) / w + 1;
		y1 = n % w;
		if (y1 == 0)
			y1 = w;
		if (x1 % 2 == 0) {
			y1 = w - y1 + 1;
		}

		x2 = (m - 1) / w + 1;
		y2 = m % w;
		if (y2 == 0)
			y2 = w;
		if (x2 % 2 == 0) {
			y2 = w - y2 + 1;
		}
	}
}


打印大X

小明希望用星号拼凑,打印出一个大X,他要求能够控制笔画的宽度和整个字的高度。

为了便于比对空格,所有的空白位置都以句点符来代替。

要求输入两个整数m n,表示笔的宽度,X的高度。用空格分开(0<m<n, 3<n<1000, 保证n是奇数)

要求输出一个大X

例如,用户输入:

3 9

程序应该输出:

***.....***

.***...***.

..***.***..

...*****...

....***....

...*****...

..***.***..

.***...***.

***.....***

(如有对齐问题,参看【图1.jpg】)

再例如,用户输入:

4 21

程序应该输出

****................****

.****..............****.

..****............****..

...****..........****...

....****........****....

.....****......****.....

......****....****......

.......****..****.......

........********........

.........******.........

..........****..........

.........******.........

........********........

.......****..****.......

......****....****......

.....****......****.....

....****........****....

...****..........****...

..****............****..

.****..............****.

****................****

(如有对齐问题,参看【图2.jpg】)

资源约定:

峰值内存消耗(含虚拟机) < 256M

CPU消耗  < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码处理。

  

import java.util.Scanner;

/**
 * @createDate:2019年3月15日 下午10:51:58
 * @porjectName:lanqiao
 * @author Static
 * @version 1.0
 * @since JDK 1.8.0_21
 * @filename:打印大X.java @describe:
 */
public class 打印大X {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		char arr[][] = new char[n][m + n - 1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m + n - 1; j++) {
				arr[i][j] = '.';
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = i; j < i + m; j++) {
				arr[i][j] = '*';
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = arr[0].length - 1 - i; j >= arr[0].length - i - m; j--) {
				arr[i][j] = '*';
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m + n - 1; j++) {
				System.out.print(arr[i][j]);
			}
			System.out.println();
		}
	}
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值