数据结构和基本语法 训练1

Skip to content

Search or jump to…

Pull requests
Issues
Marketplace
Explore

@zyhzyt
Learn Git and GitHub without any code!
Using the Hello World guide, you’ll start a branch, write comments, and open a pull request.

zyhzyt
/
java-
1
0 0
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Settings
java-/每日一题1
@zyhzyt
zyhzyt Create 每日一题1
Latest commit d4d3b54 1 minute ago
History
1 contributor
444 lines (368 sloc) 11.6 KB

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int w = sc.nextInt();//列 大于1
    int h = sc.nextInt();//行 小于1000
    int a = w/2;
    int b = h/2;
    int count = 0;
    if (h%2 != 0) { //横坐标的个位数是奇数
        if (w%2 == 0) { //纵坐标的个位是偶数
            //把横坐标两份两份的分开

        }else {

        }
    }else {
        if (w%2 == 0) {
            //都是偶数的情况
            
        }else {

        }
    }
    System.out.println(count);
}

//正整数A和正整数B的最小公倍数是指能被A和B整除的最小的正整数值,设计—个算法,求输入A和B的最小公倍数。
public static void main1(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    if (a == 1 || b == 1) {
        if (a == 1) System.out.println(b);
        if (b == 1) System.out.println(a);
    }
    if (a%b == 0) System.out.println(a);
    if (b%a == 0) System.out.println(b);
    int i = Math.max(a,b);
    while (i%b != 0 || i%a != 0) {
        i++;
    }
    System.out.println(i);
}

}

import java.util.Arrays;
import java.util.Scanner;

class Main1 {
//输入n个整数,输出出现次数大于等于数组长度—半的数。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine();
String[] s = str.split(" ");
if (s.length == 1) System.out.println(s[0]);
Arrays.sort(s);
String mid = s[s.length/2];
Integer a = Integer.parseInt(mid);
System.out.println(a);
}
}

public int inta(int[] array) {
    if(array == null||array.length<=0){
        return 0;
    }
    if(array.length==1){
        return array[0];
    }
    Arrays.sort(array);
    int mid = array[array.length/2];
    int j = 0;
    for (int i=0; i<array.length;i++){
        if (array[i] == mid){
            j++;
        }
    }
    return j > array.length/2 ? mid : 0;
}

}

public class Main {
public static void main(String[] args) {
//数组
Scanner sc = new Scanner(System.in);
int[] elem = new int[4];
for (int i = 0;i < elem.length;i++) {
elem[i] = sc.nextInt();
}
int a = (elem[0]+elem[2])/2;
int b = (elem[1+elem[3]])/2;
if (a%2 != 0 || b%2 != 0) {
System.out.println(“No”);
}
//或者
// Scanner sc = new Scanner(System.in);
// int a = sc.nextInt();
// int b = sc.nextInt();
// int c = sc.nextInt();
// int d = sc.nextInt();
//
// int A = (a+c)/2;
// int B = (c-a)/2;
// int C = (d - b)/2;
//
// if(A-B==a && B-C == b && A+B ==c && B+C == d){
// System.out.println(A+" “+B +” "+C);
// }else{
// System.out.println(“No”);
// }

}

}

import java.util.Stack;

class Solution { //两个栈实现队列,stack1为进队列,stack2为出队列,先进的要先出
Stack stack1 = new Stack();
Stack stack2 = new Stack();

public void push(int node) {
    stack1.push(node);
}

public int pop() {
    if (stack2.empty()) {
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
    }if (!stack2.empty()) return stack2.pop();
    return -1;
}

}

public class Main {
public static void main(String[] args) {

}

}

import java.util.Scanner;

public class Main {

//删除第二个字符串中出现的字符
public static void main3(String[] args) {
    Scanner in = new Scanner(System.in);
    String s1 = in.nextLine();
    String s2 = in.nextLine();
    StringBuilder s = new StringBuilder();//存储结果字符串
    /*char[] b1 = new char[256];
    for(int i = 0;i < s2.length();i++){
        b1[s2.charAt(i)] = 1;
    }
    for(int i = 0;i < s1.length();i++){
        if(b1[s1.charAt(i)] != 1){
            s.append(s1.charAt(i));
        }
    }
    System.out.println(s.toString());*/
}



//奸诈的商贩买苹果
public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()){
        int n = in.nextInt();
        System.out.println(count(n));
    }
}
public static int count(int n){
    if(n%2!=0||n==10||n<6) return -1;//一定是偶数(6,8都是),最小是6,10以上偶数都可以;
    if(n%8==0) return n/8;//如果拿八个拿完最好
    return 1+n/8;//对于10以上的偶数,只要对8取余数不为0,就要从前面的1或者2个8中拿出2个,把余数补为6(本来余数就是6,就不用拿)。所以+1;
}

public static void main2(String args[]) {
    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()){
        int n = in.nextInt();
        int count = 0;
        int min = 0;
        if(n%2 != 0 || n < 6 || n == 10) {
            System.out.println(-1);
            return;
        }
        for (int i = 0;i <= (n/8)+1;i++) {
            for (int j = 0;j <= (n/6)+1;j++) {
                if ((i*8)+(j*6) == n) {
                    count = i + j;
                    if (min < count) {
                        min = count;
                    }
                }
            }
        }

        System.out.println(min);
    }
}

public static void main1(String args[]) {
    Scanner in = new Scanner(System.in);
    while(in.hasNextInt()){
        int n = in.nextInt();
        int count = 0;
        if(n%2!=0||n==10||n<6) count = -1;//一定是偶数(6,8都是),最小是6,10以上偶数都可以;
        if(n%8==0) count = n/8;//如果拿八个拿完最好
        count = 1+n/8;//对于10以上的偶数,只要对8取余数不为0,就要从前面的1或者2个8中拿出2个,把余数补为6(本来余数就是6,就不用拿)。所以+1;
        System.out.println(count);
    }
}

}

import java.util.Scanner;
import java.util.Stack;

class Parenthesis {

//一个合法的括号串定义为:1.只包括括号字符;2.左括号和右括号——对应
public boolean chkParenthesis(String A, int n) {
    // write code here
    Stack<Character> stack = new Stack<>();
    for(int i = 0;i < n;i++){
        if(A.charAt(i) == '('){
            stack.push(A.charAt(i));
        }
        if(A.charAt(i) == ')'){
            if(!stack.isEmpty()){
                stack.pop();
            }else {
                return false;
            }
        }
    }
    if(stack.isEmpty()){
        return true;
    }
    return false;
}

}

public class Main {

//在一行内输出str中里连续最长的数字串。
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while(scanner.hasNext()){
        String str = scanner.nextLine();
        int max = 0;
        int count = 0;
        int end = 0;
        for(int i = 0;i < str.length();i++){
            if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                count++;
                if(max < count){  // 重新计数产生的count大于max时才让把count赋值给max
                    max = count;//记录最大数字串的长度
                    end = i;//记录最大数字串的末尾位置
                }
            }
            else{
                count = 0; //计数器归o
            }
        }
        System.out.println(str.substring(end-max+1,end+1));
    }
}

}

import java.util.Scanner;

class AntiOrder {
//逆序对
public int count(int[] A, int n) {

    // write code here
    if (n <= 0) return -1;
    int count = 0;
    for(int i = 0;i < n;i++){
        for(int j = i+1;j < n;j++) {
            if(A[i] > A[j]) {
                count++;
            }
        }
    }
    return count;
}

}

public class Work {
public static void main(String[] args){
// Scanner scan = new Scanner(System.in);
// while(scan.hasNext()){
// int n = scan.nextInt();
// int total = 0;
// if(n == 0)
// return;
// while(n >= 3){
// total += n/3;
// n = n/3+n%3;
// }
// if(n==2)
// total += 1;
// System.out.println(total);
// }

    //卖水瓶子
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        int n = sc.nextInt();
        int t = 0;
        if (n == 0) return;
        while(n > 2){
            t = t+n/3;
            n = n/3 + n%3;
        }
        if(n == 2){
            t = t+1;
        }
        System.out.println(t);
    }
}

}

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Work1 {

//快速排序
//给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。

public class Finder {
    public int findKth(int[] a, int n, int K) {
        return quickSort(a,0,n-1,K);
    }
    private int quickSort(int[] arr, int low, int high,int K){
        int p = partion(arr,low,high);
        if(K == p-low+1){
            return arr[p];
        }else if(p-low+1 > K){
            //递归左边
            return quickSort(arr,low,p-1,K);
        }else{
            //递归右边
            return quickSort(arr,p+1,high,K-(p-low+1));
        }
    }
    private int partion(int[] arr, int low, int high){
        int tmp = arr[low];
        while(low < high){
            while(low < high && arr[high] <= tmp){
                high--;
            }
            if(low == high){
                break;
            }else{
                arr[low] = arr[high];
            }
            while(low < high && arr[low] >= tmp){
                low++;
            }
            if(low == high){
                break;
            }else{
                arr[high] = arr[low];
            }
        }
        arr[low] = tmp;
        return low;
    }
}





//判断是否为回文
public static boolean HuiWen(String s){
    int i=0;
    int j=s.length()-1;
    while(i<j){
        if(s.charAt(i)!=s.charAt(j)){
            return false;
        }
        i++;
        j--;
    }
    return true;
}
public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    String str1=sc.nextLine();
    String str2=sc.nextLine();
    int count=0;
    for(int i=0;i<=str1.length();i++){
        StringBuilder sb=new StringBuilder(str1);
        sb.insert(i,str2);//从0号位置开始插入
        if(HuiWen(sb.toString())){
            //说明是回文,计数+1
            count++;
        }
    }
    System.out.println(count);
}

}

© 2020 GitHub, Inc.
Terms
Privacy
Security
Status
Help
Contact GitHub
Pricing
API
Training
Blog
About

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值