这里主要是练习系统的初阶的题目,使用的方法都很简单暴力。路漫漫其修远兮,这和算法题相差太远了,我要上下而求索去学习呀,加油呀,为了以后的风与月!
2020年4月20日晚整理
目录
1: 字母图形
利用字母可以组成一些美丽的图形,下面给出了一个例子:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char a[]= {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(int i=0;i<n;i++) {
int count=0;
for(int j=i;j>0&&count<m;j--) {
count++;
System.out.print(a[j]);
}
for(int k=0;k<m-i;k++) {
System.out.print(a[k]);
}
System.out.println();
}
}
}
|
2: 杨辉三角形
杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int row=in.nextInt();
long[][] a=new long[row][];
for(int n=0;n<row;n++)
a[n]=new long[n+1];
for(int n=0;n<row;n++)
for(int k=0;k<n+1;k++)
{
long t=1;
a[n][0]=1;
a[n][n]=1;
for(int i=1;i<n;i++) {
a[n][i]=a[n-1][i-1]+a[n-1][i];
}
}
for(long[] m:a)
{
for(long r:m) {
System.out.print (r+" ");
}
System.out.println();
}
}
}
3: 特殊回文数
123321是一个非常特殊的数,它从左边读和从右边读是一样的。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
for(int a=1;a<=9;a++) {
for(int b=0;b<=9;b++) {
for(int c=0;c<=9;c++) {
if((a+b)*2+c==n) {
System.out.println(a+""+b+""+c+""+b+""+a);
}
}
}
}
for(int a=1;a<=9;a++) {
for(int b=0;b<=9;b++) {
for(int c=0;c<=9;c++) {
if((a+b+c)*2==n) {
System.out.println(a+""+b+""+c+""+c+""+b+""+a);
}
}
}
}
}
}
|
4: 十进制转十六进制
十六进制数是在程序设计时经常要使用到的一种整数的表示方式。它有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。
给出一个非负整数,将它表示成十六进制的形式。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n =new Scanner(System.in).nextInt();
System.out.print(Integer.toHexString(n).toUpperCase());
}
}
|
5: 十六进制转十进制
从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String s=input.nextLine();
System.out.println(Long.valueOf(s,16));
}
}
|
6: 十六进制转八进制
给定n个十六进制正整数,输出它们对应的八进制数。
问题描述
给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
输入的第一行为一个正整数n (1<=n<=10)。
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出格式
输出n行,每行为输入对应的八进制正整数。
【注意】
输入的十六进制数不会有前导0,比如012A。
输出的八进制数也不能有前导0。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String st[]=new String[n];
for(int i=0;i<n;i++) {
st[i] = sc.next();
}
for(int i=0;i<n;i++) {
trans(st[i]);
}
}
public static void trans(String s) {
BigInteger num= new BigInteger(s,16);
System.out.println(num.toString(8));
}
}
7: 阶乘计算
输入一个正整数n,输出n!的值。
其中n!=1*2*3*…*n。
n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法。使用一个数组A来表示一个大整数a,A[0]表示a的个位,A[1]表示a的十位,依次类推。
将a乘以一个整数k变为将数组A的每一个元素都乘以k,请注意处理相应的进位。
首先将a设为1,然后乘2,乘3,当乘到n时,即得到了n!的值。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger num= new BigInteger("1");
for(int i=1;i<=n;i++) {
num=num.multiply(new BigInteger(Integer.toString(i)));
}
System.out.print(num);
}
}
|
8: 报时助手
给定当前的时间,请用英文的读法将它读出来。
时间用时h和分m表示,在英文的读法中,读一个时间的方法是:
如果m为0,则将时读出来,然后加上“o'clock”,如3:00读作“three o'clock”。
如果m不为0,则将时读出来,然后将分读出来,如5:30读作“five thirty”。
时和分的读法使用的是英文数字的读法,其中0~20读作:
0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty。
30读作thirty,40读作forty,50读作fifty。
对于大于20小于60的数字,首先读整十的数,然后再加上个位数。如31首先读30再加1的读法,读作“thirty one”。
按上面的规则21:54读作“twenty one fifty four”,9:07读作“nine seven”,0:15读作“zero fifteen”。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int h = key.nextInt();
int m= key.nextInt();
String a[]={"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen","eighteen", "nineteen", "twenty","twenty one","twenty two","twenty three","thirty","forty","fifty"};
if(m==0){
System.out.println(a[h] + " o'clock");
}
else if(m<=23){
System.out.println(a[h] + " " + a[m]);
}
else if(m>23&&m<30){
System.out.println(a[h] + " twenty " + a[m % 10]);
}
else if(m>20&&m%10==0){
System.out.println(a[h] + " " + a[m / 10 + 21]);
}else{
System.out.println(a[h] + " " + a[m / 10 +21] + " " + a[m % 10]);
}
}
}
9: 分解质因数
求出区间[a,b]中所有整数的质因数分解。
【输出格式】
每行输出一个数的分解,形如k=a1*a2*a3...(a1<=a2<=a3...,k也是从小到大的)(具体可看样例)
【样例输入】
3 10
【样例输出】
3=3
4=2*2
5=5
6=2*3
7=7
8=2*2*2
9=3*3
10=2*5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int a = key.nextInt();
int b = key.nextInt();
for (int i = a; i <=b ; i++) {
getPrime(i);
}
}
public static boolean isPrime(int n) {
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void getPrime(int n) {
String res="";
int j=2;
int m=1;
int k=n;
if (isPrime(n)) {
System.out.println(n + "=" + n);
} else {
while(m!=n){
if(isPrime(j)&&k%j==0){
res+=j+"*";
m*=j;
k=n/m;
}else{
j++;
}
}
System.out.println(n+"="+res.substring(0,res.length()-1));
}
}
}
10: 矩阵乘法
给定一个N阶矩阵A,输出A的M次幂(M是非负整数)
例如:
A =
1 2
3 4
A的2次幂
7 10
15 22
【输入格式】
第一行是一个正整数N、M(1<=N<=30, 0<=M<=5),表示矩阵A的阶数和要求的幂数
接下来N行,每行N个绝对值不超过10的非负整数,描述矩阵A的值
【输出格式】
输出共N行,每行N个整数,表示A的M次幂所对应的矩阵。相邻的数之间用一个空格隔开
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int n = key.nextInt();
int m = key.nextInt();
int a[][]=new int[n][n];
for (int i = 0; i <n ; i++) {
for (int j = 0; j <n ; j++) {
a[i][j]=key.nextInt();
}
}
int res[][] = a.clone();
int t[][]=a.clone();
final int s[][]=a.clone();
for (int i = 1; i < m; i++) {
res = getRes(t, s);
t = res;
}
if(m>0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
}
else{
int b[][]=new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
b[i][j] = 1;
}
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}
public static int[][] getRes(int a[][],int b[][]){
int m=a.length;
int n=b.length;
int c[][]=new int[m][n];
for (int i = 0; i <m ; i++) {
for (int j = 0; j <n ; j++) {
int sum=0;
for (int k = 0; k <n ; k++) {
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
return c;
}
}