前几天考了校内模拟赛,因为自己技术有限和一些特殊原因,提前交了,然后之后再看一遍题目,思考,再做。小白做题,如有错误,敬请指正。
第一题
在计算机储存中,15.125GB是多少MB?
分析:送分题,1GB=1024MB, 15.125GB=15488MB
第二题
1200000有多少个约数(只计算正约数)?
public class Main {
public static void main(String[] args) {
int count=0;
for (int i = 1; i <=1200000; i++) {
if (1200000%i==0) {
count++;
}
}
System.out.println(count);
}
}
输出结果:96
第三题
在1至2019中有多少个数的数位中包含数字9?
注意:有的数的数位中包含多个9,这个数只算一次
方法一:
学会用Boolean,然后调用方法,用while循环,含9的数就返回true。
public class Main {
public static boolean get(int n){
while(n>0){
if(n%10==9){
return true;
}
n=n/10;
}
return false;
}
public static void main(String []args){
boolean flag=true;
int count=0;
for(int i=1;i<=2019;i++){
if(get(i)){
count++;
}
}
System.out.println(count);
}
}
方法二:暴力破解
public class Main {
public static void main(String[] arge) {
int count = 0;
for (int i = 1; i <= 2019; i++) {
if (i % 10 == 9 || i / 10 % 10 == 9 || i / 100 % 10 == 9
|| i / 1000 % 10 == 9) {
//System.out.println(i);
count++;
}
}
System.out.println(count);
}
}
输出结果:544
第四题
一棵包含有2019个结点的树,最多包含多少个叶结点?
分析:叶结点就是度为0,度为2的结点比度为0的结点少1,当叶结点最多的时候,让度为1的结点为0,n0+0+n0-1=2019 (n0=1010)
第五题
小明对类似于hello这种单词非常感兴趣,这种单词正好分为四段,第一段由一个或多个辅音字母组成,第二段由一个或多个元音字母组成,第三段由一个或多个辅音字母组成,第四段由一个或多个元音字母组成。
给定一个单词,请判断这个单词是否也是这种单词,如果是,请输出yes,否则输出no,元音字母包括a,e,i,o,u,其它均为辅音字母
分析:四部分组成,辅|元|辅|元,将元音a,e,i,o,u存入集合list中;通过从键盘输入一连续字母,这里需要用到toCharArray()转为字符串存入字符数组中;用list集合中的contains与字符数组ch比较,(!list.contains(ch[i])是辅音字母,然后count++;如果查找到元音就转到else中,同理再比较,是辅音又转回if中,四部分就是count==4,就输出yes,否则输出no)
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ss=sc.next();
ArrayList list=new ArrayList();
list.add('a');
list.add('e');
list.add('i');
list.add('o');
list.add('u');
char[]ch=ss.toCharArray();
boolean flag=true;
int count=0;
for(int i=0;i<ch.length;i++){
if(flag){
if(!list.contains(ch[i])){
count++;
flag=false;
continue;
}
}
else{
if(list.contains(ch[i])){
count++;
flag=true;
continue;
}
}
}
if(count==4){
System.out.println("yes");
}
else
System.out.println("no");
}
}
第六题
在数列a[1],a[2],…,a[n]中,如果对于下标i,j,k满足0<i<j<k<n+1且a[i]<a[j]<a[k]为一组递增三元组,a[j]为递增三元组的中心 。给定一个数列,请问数列中有多少个元素可能是递增三元组的中心?
分析:从键盘输入n,定义数组的长度为n,将从键盘上输入n个数放入数组中,用while循环,每次比较有一个中心数就count++,定义ss为条件,每次出来一个中心数就ss++,当ss大于数组的长度时,就结束循环,输出count。调用了sum方法
import java.util.Scanner;
public class Sb {
static int count = 0;
static int ss = 1;
static Scanner sc = new Scanner(System.in);
static int n = sc.nextInt();
static int[] arr = new int[n];
public static void main(String[] args) {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
while (ss < arr.length) {
sum();
ss++;
}
System.out.println(count);
}
private static void sum() {
for (int j = ss; j < arr.length; j++) {
ss = j;
for (int i= j-1; i>=0;i--) {
if(arr[j]>arr[i]){
for (int k = j+1; k < arr.length; k++) {
if(arr[j]<arr[k]){
count++;
return;
}
}
}
}
}
}
}
第七题
一个正整数如果任何一个数位不大于右边相邻的数位,则称为一个数位递增的数,例如1135是一个数位递增的数,而1024不是一个递增的数。
给定正整数n,请问在整数1至n中有多少个数位递增的数?
分析:递增数,当输入的数小于10时,那么递增的个数就是输入的数,当大于10时,就需要比较了,写一个panduan()方法,如果是递增数就count++,不是递增数就只有9个,也就是count(static int count = 9)。
import java.util.Scanner;
public class Test{
static int count=9;
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if (n<10) {
count=n;
}
for (int i = 11; i <=n; i++) {
char[]ch=String.valueOf(i).toCharArray();
panduan(ch);
}
System.out.println(count);
}
private static int panduan(char[] ch) {
for (int i = 0; i < ch.length; i++) {
for (int j = i+1; j < ch.length; j++) {
if (ch[i]>ch[j])
return count;
}
}
return count++;
}
}
第八题
小明有一块空地,他将这块空地划分为n行m列的小块,每行每列的长度都为1,小明选了一些小块空地,种上了草,其它小块仍然保持是空地。这些草长的很快,每个月,草都会向外长出一些,如果一个小块种上了草,则它将向自己的上下左右四小块扩展,这四小块空地都将变为有草的小块。
请告诉小明,k个月后空地上那些地方有草?(小数点表示空地,g表示有草)
import java.util.Scanner;
public class Main {
public static int[][] bool;
public static int[] start;
public static int[] end;
public static char[][] num ;
public static int k = 0, n = 0, m = 0;
public static int[] x = { 0, 1, 0, -1 };
public static int[] y = { 1, 0, -1, 0 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
num = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
num[i] = s.toCharArray();
}
k = sc.nextInt();
sc.close();
start = new int[m * n];
end = new int[m * n];
int temp = 0;
bool = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (num[i][j] == 'g') {
start[temp] = i;
end[temp++] = j;
}
else{
bool[i][j]=-1;
}
}
}
for (int i = 0; i < temp; i++) {
dfs(start[i],end[i],k);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <m; j++) {
System.out.print(num[i][j]);
}
System.out.println();
}
}
public static void dfs(int xx, int yy, int kk) {
bool[xx][yy]=kk;
num[xx][yy]='g';
for (int i = 0; i < 4; i++) {
int newx = x[i] + xx;
int newy = y[i] + yy;
if ( newx >= 0 && newy >= 0 && newx < n && newy < m&& kk - 1 > bool[newx][newy]) {
dfs(newx, newy, kk - 1);
}
}
}
}
第九题
小明想知道满足以下条件的正整数序列的数量
1、第一项为n
2、第二项不超过n
3、第三项开始,每一项小于前两项的差的绝对值,请计算,给定的n,有多少种满足条件的 序列?
如:输入4
输出7
说明:
满足条件的序列
4 1
4 1 1
4 1 2
4 2
4 2 1
4 3
4 4
import java.util.Scanner;
public class Main {
public static int n=0,count=0;
public static int [] []map ;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n =sc.nextInt();
sc.close();
map = new int [n+1][n+1];
for (int i = 1; i <=n; i++) {
map[i][i]=1;
map[i][0]=1;
map[0][i]=1;
}
for (int i = 1; i <=n; i++) {
count+=f(n,i);
count%=10000;
}
System.out.println(count);
}
public static int f(int x,int y){
if(map[x][y]!=0){
return map[x][y];
}
for (int i = Math.abs(x-y)-1; i>=0; i--) {
map[x][y]+=f(y,i);
}
map[y][x]=map[x][y];
return map[x][y];
}
}
第十题
小明要组织晚会,总共n个节目,晚会时间有限,他只能选择其中的m个节目。
这n个节目按照顺序给定了,顺序不能改变,小明发现,观众对于晚会的喜欢程度与前几个节目的好看程度有非常大的关系,他希望选出第一个节目尽可能好看,在此前提下希望第二个节目尽可能好看,以此类推。
小明给每个节目定义了一个好看值,请你帮助小明选出m个节目,满足他的要求。
如:输入
5 3
3 1 2 5 4
输出
3 5 4
import java.util.ArrayList;
import java.util.Arrays;
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();
int[] num = new int[n];
int[] order = new int[n];
for (int i = 0; i < n; i++) {
num[i] = sc.nextInt();
order[i] = num[i];
}
sc.close();
Arrays.sort(order);
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = n - m; i < n; i++) {
list.add(order[i]);
}
StringBuilder sb = new StringBuilder("");
for (int i : num) {
if (list.contains(i)) {
sb.append(i + " ");
}
}
System.out.println(sb);
}
}