您的位置:首页 > 编程语言 > Java开发

java实现:牛客网中偏简单的那些代码题:找了那些通过率百分之30以上的题来练习,慢慢提高自己的代码能力

2018-08-25 20:41 253 查看

java实现:牛客网中偏简单的那些代码题:找了那些通过率百分之30以上的题来练习,慢慢提高自己的代码能力

 

 

 

题目1:

[code]package test21080825;

import java.util.Scanner;

public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
int j = 0;
int arr[] = new int[1000 * 1000 + 1];
while (sc.hasNext()) {

arr[j] = sc.nextInt();
j++;
if (arr[0] * 2 < j) {
break;
}
}
n = 2 * arr[0] + 1;
int m = arr[0];
for (int k = 0; k < n; k++) {
// System.out.println(arr[k]);
}
int min = arr[1] + arr[1 + m];
for (int i = 1; i < m + 1; i++) {

//System.out.println(min);
if (min > arr[i] + arr[i + m]) {
min = arr[i] + arr[i + m];
// System.out.println("min" + min);
}
}
System.out.println(min - 2);

}

}

控制台:

 

题目二:

代码实现如下:

[code]package test21080825;

import java.util.Scanner;

public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
char[] arr = str.toCharArray();
String[] arr2 = new String[arr.length];
boolean bool = true;
for (int i = 0; i < arr.length; i++) {

if (!(arr[i] >= 'A' && arr[i] <= 'Z')) {
// System.out.println("Dislikes");
bool = false;
break;

}
if (i + 1 < arr.length) {
if ((arr[i] == arr[i + 1])) {
// System.out.println("Dislikes");
bool = false;
break;
}
}
for (int j = i + 1; j < arr.length - 1; j++) {
if (arr[i] == arr[j]) {
for (int a = i+1; a < j; a++) {
for (int b = j+1; b < arr.length; b++) {
if (arr[a] == arr[b]) {
// System.out.println("Dislikes");
bool = false;
break;
}
}
}
}
}
// System.out.println("Likes");

}
if (bool == false) {
System.out.println("Dislikes");
} else {
System.out.println("Likes");
}

}
}
}

控制台输出:

题目三:

思路:这个蛋糕要从第一个位置开始放。同时放蛋糕的情况是可以每四行一个循环,每四列一个循环。

我们可以先算出前面四行的情况,后边就是循环问题了,就都解答出来了。

其实前面四行中,前面两行的情况一样,后边两行情况也是相同的。所以就把前面两行区分为了1,2行和3,4行。

比如针对第一行:算出前面四列情况,后边就根据循环来算出这一行总的蛋糕数量。

那么第3,4行和第1,2行还有一个关系,就是sum12+sum34=纵向数量

[code]

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int a = m / 4;// 横坐标除4取整
int b = m % 4;// 横坐标取余
int c = n / 4;// 纵坐标除4取整
int d = n % 4;// 纵坐标取余
int sum12 = 0;//前边两行放的蛋糕数量一样
int sum34 = 0;//3,4行蛋糕数量也一样
int sum = 0;最后的总的蛋糕数量
int sum1234 = 0;前边四行的蛋糕数量
// 先得出前四行分别的数目,首先前面两行一样,后边两行一样
// 首先是1,2两行放的蛋糕都一样
if (d == 1) {
sum12 = c * 2 + 1;
} else if (d == 0) {
sum12 = (c - 1) * 2 + 2;
} else {
sum12 = c * 2 + 2;
}
// 然后是3,4两行,放的蛋糕都一样
sum34 = n - sum12;
// 前面四行一共放蛋糕数量如下
sum1234 = 2 * (sum12 + sum34);

// 前边计算了前面四行分别的数目,接下来是往纵看,
if (b == 1) {
sum = a * sum1234 + sum12;
}else
if (b == 2) {
sum = a * sum1234 + 2 * sum12;
}else
if (b == 3) {
sum = a * sum1234 + sum12 * 2 + sum34;
}
else {
sum = a * sum1234;
}
System.out.println(sum);

}
}
}

控制台:

题目四:

代码:

[code]package test20180826;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()) {
int a = sc.nextInt();
int b = sc.nextInt();
if(a>=0&&a<=1024&&b>=0&&b<=1024){
if(a==b){
System.out.println(1);
}else{
System.out.println(0);
}
}else{
System.out.println(-1);
}
}
}
}

题目五:

思路:
 

代码如下:

[code]package test20180826;

import java.util.Scanner;

public class Main2 {
/*
* 感觉我这个思路蛮简单,首先是看可以对8整除不,
*
* 如果不能在看可以选出几个8和6组合,
*
* 8的个数从app/8到0个变化,取相应的6的个数。
*
* 如果上边取不到整数,就相当于不可行。
*
* 我前后使用了一个标记,boo,如果取到整数了
*
* 就是true,否则是false,
*/

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean boo = false;
while (sc.hasNext()) {
int app = sc.nextInt();
if (app % 8 == 0) {
System.out.println(app / 8);
boo = true;
} else {
for (int i = app / 8; i >= 0; i--) {
if ((app - i * 8) % 6 == 0) {
System.out.println(i + (app - i * 8) / 6);
boo = true;
break;
}
}
if (boo == false) {
System.out.println(-1);
}
}
}
}
}

控制台:

题目6

这个答案 我直接转载大佬的做法,简直吊炸天了:

题目七:

代码:

[code]package main20180827;

import java.util.Scanner;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int i = 0;
int j = 0;
while (i < str1.length() && j < str2.length()) {
if (str1.charAt(i) == str2.charAt(j)) {
i++;
j++;
} else {
i++;
}

}
if (j == str2.length()) {
System.out.println("Yes");
} else {
System.out.println("No");
}

}
}
}

题目八:

较为复杂,但是最容易想到的方法:

[code]package main20180827;

import java.util.Scanner;

public class Main3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int k=0;
String str = sc.nextLine();
String[] arr = str.split(" ");
int i;
for( i=0;i<arr.length;i++){
k=0;
for(int j=0;j<arr.length;j++){
if(arr[i].equals(arr[j])){
k++;
//					System.out.println(k+"  ");
//					System.out.println("下标"+i);
}
}
if(k>=arr.length/2){
//System.out.println(arr.length/2);
System.out.println(arr[i]);
break;
}
}

}
}
}

法二:是代码最少的做法:

法三:是时间复杂度只有O(n)的方式:

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐