BASIC - 1 闰年判断
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
给定一个年份,判断这一年是不是闰年。
当以下情况之一满足时,这一年是闰年:
-
年份是4的倍数而不是100的倍数;
-
年份是400的倍数。
其他的年份都不是闰年。
输入格式
输入包含一个整数y,表示当前的年份。
输出格式
输出一行,如果给定的年份是闰年,则输出yes,否则输出no。
说明:当试题指定你输出一个字符串作为结果(比如本题的yes或者no,你需要严格按照试题中给定的大小写,写错大小写将不得分。
样例输入
2013
样例输出
no
样例输入
2016
样例输出
yes
数据规模与约定
1990 <= y <= 2050。
算法分析
如何判断是闰年还是平年
- 普通闰年:公历年份是
4
的倍数的,且不是100
的倍数,为普通闰年。
- 世纪闰年:公历年份是整百数的,必须是
400
的倍数才是世纪闰年
时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) System.out.println("yes");
else System.out.println("no");
}
}
BASIC - 2 01字串
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
算法分析
求n的二进制表示中的第k为是几:n >> k & 1
枚举0到31,分别枚举每一位
时间复杂度
Java 代码
public class Main {
public static void main(String[] args) {
for(int i = 0;i < 32;i ++)
{
for(int k = 4;k >= 0;k --)
{
System.out.print((i >> k) & 1);
}
System.out.println();
}
}
}
BASIC - 3 字母图形
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
利用字母可以组成一些美丽的图形,下面给出了一个例子:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。
输入格式
输入一行,包含两个整数n和m,分别表示你要输出的图形的行数的列数。
输出格式
输出n行,每个m个字符,为你的图形。
样例输入
5 7
样例输出
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
数据规模与约定
1 <= n, m <= 26。
算法分析
image.png时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
for(int i = 0;i < n;i ++)
{
for(int j = 0;j < m;j ++)
{
if(j >= i) System.out.print((char)(j - i + 'A'));
else System.out.print((char)(i - j + 'A'));
}
System.out.println();
}
}
}
BASIC - 4 数字特征
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
给出n个数,找出这n个数的最大值,最小值,和。
输入格式
第一行为整数n,表示数的个数。
第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。
输出格式
输出三行,每行一个整数。第一行表示这些数中的最大值,第二行表示这些数中的最小值,第三行表示这些数的和。
样例输入
5
1 3 -2 4 5
样例输出
5
-2
11
数据规模与约定
1 <= n <= 10000。
算法分析
直接模拟
时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int maxv = Integer.MIN_VALUE,minv = Integer.MAX_VALUE;
int sum = 0;
for(int i = 0;i < n;i ++)
{
int x = scan.nextInt();
maxv = Math.max(maxv,x);
minv = Math.min(minv,x);
sum += x;
}
System.out.println(maxv);
System.out.println(minv);
System.out.println(sum);
}
}
BASIC - 5 寻找整数
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
输入格式
第一行包含一个整数n。
第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。
第三行包含一个整数a,为待查找的数。
输出格式
如果a在数列中出现了,输出它第一次出现的位置(位置从1开始编号),否则输出-1。
样例输入
6
1 9 4 8 3 9
9
样例输出
2
数据规模与约定
1 <= n <= 1000。
算法分析
直接模拟,找到第一个直接记录并break,最后输出
时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
static int N = 10010;
static int[] a = new int[N];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i = 1;i <= n;i ++)
{
a[i] = scan.nextInt();
}
int target = scan.nextInt();
int ans = -1;
for(int i = 1;i <= n;i ++)
{
if(a[i] == target)
{
ans = i;
break;
}
}
System.out.println(ans);
}
}
BASIC - 6 杨辉三角形
题目描述
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
输入格式
输入包含一个数n。
输出格式
输出杨辉三角形的前n行。每一行从这一行的第一个数开始依次输出,中间使用一个空格分隔。请不要在前面输出多余的空格。
样例输入
4
样例输出
1
1 1
1 2 1
1 3 3 1
数据规模与约定
1 <= n <= 34。
算法分析
关键公式:f[i,j] = f[i - 1,j - 1] + f[i - 1,j]
时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
static int N = 40;
static int[][] f = new int[N][N];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= i;j ++)
{
if(j == 1 || i == j) f[i][j] = 1;
else
{
f[i][j] = f[i - 1][j - 1] + f[i - 1][j];
}
}
}
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= i;j ++)
{
if(j != i) System.out.print(f[i][j] + " ");
else System.out.print(f[i][j]);
}
System.out.println();
}
}
}
BASIC - 7 特别的数字
题目描述
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
153是一个非常特殊的数,它等于它的每位数字的立方和,即153=111+555+333。编程求所有满足这种条件的三位十进制数。
输出格式
按从小到大的顺序输出满足条件的三位十进制数,每个数占一行。
算法分析
直接模拟,从100
遍历到999
时间复杂度
n = 900,k == 3
Java 代码
public class Main {
static boolean check(int x)
{
int res = 0;
int t = x;
while(t > 0)
{
int temp = t % 10;
res += temp * temp * temp;
t /= 10;
}
return res == x;
}
public static void main(String[] args) {
for(int i = 100;i < 1000;i ++)
{
if(check(i)) System.out.println(i);
}
}
}
BASIC - 8 回文数
题目描述
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
1221是一个非常特殊的数,它从左边读和从右边读是一样的,编程求所有这样的四位十进制数。
输出格式
按从小到大的顺序输出满足条件的四位十进制数。
算法分析
直接模拟,从1000
枚举到9999
,将当前数进行翻转
时间复杂度
n = 9000,k == 4
Java 代码
public class Main {
static boolean check(int x)
{
int res = 0;
int t = x;
while(t > 0)
{
res = res * 10 + t % 10;
t /= 10;
}
return res == x;
}
public static void main(String[] args) {
for(int i = 1000;i < 10000;i ++)
{
if(check(i)) System.out.println(i);
}
}
}
BASIC - 9 特殊回文数
题目描述
算法分析
直接模拟,从10000
枚举到999999
,将当前数进行翻转,且记录每个位数的累加和
时间复杂度
Java 代码
import java.util.Scanner;
public class Main {
static int n;
static boolean check(int x)
{
int res = 0;
int t = x;
int sum = 0;
while(t > 0)
{
int temp = t % 10;
sum += temp;
res = res * 10 + temp;
t /= 10;
}
return res == x && sum == n;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
for(int i = 10000;i < 1000000;i ++)
{
if(check(i)) System.out.println(i);
}
}
}
BASIC - 10 十进制转十六进制
题目描述
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
十六进制数是在程序设计时经常要使用到的一种整数的表示方式。它有0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F共16个符号,分别表示十进制数的0至15。十六进制的计数方法是满16进1,所以十进制数16在十六进制中是10,而十进制的17在十六进制中是11,以此类推,十进制的30在十六进制中是1E。
给出一个非负整数,将它表示成十六进制的形式。
输入格式
输入包含一个非负整数a,表示要转换的数。0<=a<=2147483647
输出格式
输出这个整数的16进制表示
样例输入
30
样例输出
1E
算法分析
直接调用或者手写模拟
时间复杂度
Java 代码(调用)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
String str = Integer.toString(a,16); // 10转化为16进制
for(int i = 0;i < str.length();i ++)
{
char t = str.charAt(i);
if(t >= 'a' && t <= 'z') System.out.print((char)(t - ('a' - 'A')));
else System.out.print(t);
}
}
}
Java 代码(手写模拟)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
static char[] g = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<Integer>();
if(n == 0) System.out.println(0);
else
{
while(n > 0)
{
list.add(n % 16);
n /= 16;
}
for(int i = list.size() - 1;i >= 0;i --)
{
System.out.print(g[list.get(i)]);
}
}
}
}
网友评论