数组,基本类

#include "stdio.h"
void main()
{printf("%d",sizeof())
}
``

类外调用

class ChinaPerson
{
   /* String name;
    int age;
    String sex;
    void eat() {}
    void sleep(){}
    void work(){}
    void show(){
        System.out.println("MNIS:"+name+",mage:" +age);}
          public class 十六 {
          public static void main(String[] args)
        {
           /* System.out.println();
            Person per=new Person();
            per.name="张三";
            per.age=18;
            per.sex="male";
            per.name="af";
        per.age=14;
        per1.name="fds";
        per1.age=13;
        per.show();
```}}

static类外

``class ChinaPerson
{`
String name;
    int age;
    static String country;  
    final  static String country="china";
      static void printCountry(){
        System.out.println(" love:"+country);
    }

          public class 十六 {
 public static void main(String[] args)
        { per1.name="fds";
        per1.age=13;
        per.show();
            per1.show();
                        Person per=new Person();

        Person per1=new Person();
        Person per2=new Person();
        Person per3=new Person();
        Person.country="aaa";
```}}

给你一个整数数组 arr,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 true ;否则,返回 false

 public class 十六 {
 public boolean ConsecotiveOdds(int[] num) {
     int count = 0;
     for (int i : num) {
         if (i % 2 != 0) {
             count++;
         } else {
             count = 0;
         }
         if (count == 3) {
             return true;
         }
     }
     return false;
 }
 public static void main(String[] args)
        {
            十六 homeWork=new 十六();
            int[] num1={2,6,3,1};
            int[] num2={12,3,2,1,4,125,12,3,312,3,1,7,32,12,2};
            System.out.println(homeWork.ConsecotiveOdds(num1));
            System.out.println(homeWork.ConsecotiveOdds(num2));     }
    }
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=d906661cb55f4e659f76b6da81866337.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY3poYzExNDAwNzU2NjM=,size_20,color_FFFFFF,t_70,g_se,x_16)

hashi

 public int mantNum(int[] num) {
     HashMap<Integer, Integer> hashMap = new HashMap<>();
     for (int i = 0; i < num.length; i++) {
         if (hashMap.containsKey(num[i])) {
             int value = hashMap.get(num[i]);
             hashMap.put(num[i], value + i);
         } else {
             hashMap.put(num[i], 1);
         }
     }
     Set<Map .Entry<Integer,Integer>>set=hashMap.entrySet();
     for (Map.Entry<Integer,Integer>entry:set)
     {
         if (entry.getValue()>num.length/2){
             return entry.getKey()
         }
     }return -1;

给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

 public int mamuNum(int[] num ){
     Arrays.sort(num);
     return num[num.length/2];
 }

上一篇:C语言控制台实现进度条


下一篇:jmeter插件