日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java算法例子_java算法小例子

發(fā)布時間:2023/11/27 生活经验 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java算法例子_java算法小例子 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

作為一個程序員,有時候我覺得自己都不適合,因為自己數(shù)學(xué)不好,算法不好,腦子不夠靈活。而對于算法,感覺就像是數(shù)學(xué)題,總覺得很難。以前上學(xué),在班里總有幾個什么都不好,但唯獨數(shù)學(xué)很厲害,真氣人!面對難題時,我只能求助,自己想破腦瓜子都想不出什么好的解決辦法,都不知道怎么提高這方面的能力。只能慢慢學(xué)習(xí)了。

自己買了本算法的書,有空的時候就看看,寫一寫,總是收獲的!下面是幾個例子:

1.判斷閏年

public class LeapYear {

public static int leapYear(int year) {

if ((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0)) {

return 1; //is leap year

} else {

return 0; //not a leap year

}

}

public static void main(String[] args) {

int year;

int count = 0;

System.out.println("2000年到3000年之間所有的閏年如下:");

for (year = 2000; year <= 3000; year++) {

if (leapYear(year) == 1) {

System.out.print(year + " ");

count++;

if (count % 16 == 0) {

System.out.println();

}

}

}

System.out.println("一共有" + count + "個閏年!");

}

}

2.水仙花數(shù)

public class NarcissusNum {

public static void narcissusNum(int n) {

long i,start,end,temp,num,sum;

int j;

start = (long) Math.pow(10, n - 1);

end = (long) (Math.pow(10, n) - 1);

for (i = start; i <= end; i++) {

temp = 0;

num = i;

sum = 0;

for (j = 0; j < n; j++) {

temp = num % 10;

sum = (long) (sum + Math.pow(temp, n));

num = (num - temp) / 10;

}

if (sum == i) {

System.out.printf("%d\n", i);

}

}

}

public static void main(String[] args) {

int n;

n = 3;

System.out.printf("列舉%d位的水仙花數(shù):\n", n);

narcissusNum(n);

System.out.println();

n = 4;

System.out.printf("列舉%d位的水仙花數(shù):\n", n);

narcissusNum(n);

System.out.printf("\n");

}

}

3.判斷素數(shù)

public class IsPrime {

public static int isPrime(int a) {

for (int i = 2; i < a; i++) {

if (a % i == 0) {

return 0; //not prime number

}

}

return 1; //is a prime number

}

public static void main(String[] args) {

int i,n,count;

n = 1000;

count = 0;

System.out.println("列舉1~1000之間所有的素數(shù)");

for (i = 1; i < 1000; i++) {

if (isPrime(i) == 1) {

System.out.printf("%7d", i);

count++;

if (count % 10 == 0) {

System.out.println();

}

}

}

System.out.println();

}

}

4. List去重

public static List removeByFor(List list) {

for (int i = 0; i < list.size() - 1; i++) {

for (int j = list.size() - 1; j > i; j--) {

if (list.get(i).equals(list.get(j))) {

list.remove(j);

}

}

}

return list;

}

public static List removeBySet(List list) {

HashSet h = new HashSet(list);

list.clear();

list.addAll(h);

return list;

}

public static List removeDuplicateWithOrder(List list) {

Set set = new HashSet();

List newList = new ArrayList();

for (Iterator iterator = list.iterator(); iterator.hasNext();) {

Object element = iterator.next();

if (set.add(element)) {

newList.add(element);

}

}

list.clear();

list.addAll(newList);

return list;

}

public static List removeByContain(List list) {

List tempList = new ArrayList();

for (int i = 0;i < list.size(); i++) {

if (!tempList.contains(list.get(i))) {

tempList.add(list.get(i));

}

}

return tempList;

}

5.Map與Bean相互轉(zhuǎn)換

public static Object mapToObject(Map map, Class> beanClass) throws Exception {

if (map == null) {

return null;

}

ConvertUtils.register((aClass, o) -> {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

try {

return simpleDateFormat.parse(o.toString());

} catch (Exception e) {

e.printStackTrace();

}

return null;

}, Date.class);

Object obj = beanClass.newInstance();

BeanUtils.populate(obj, map);

return obj;

}

public static Map objectToMap(Object obj) throws Exception {

return BeanUtils.describe(obj);

}

public static Map objectToMapTwo(Object obj) throws Exception {

if (obj == null) {

return Maps.newHashMap();

}

Map map = Maps.newHashMap();

Field[] declaredFields = obj.getClass().getDeclaredFields();

for (Field field : declaredFields) {

field.setAccessible(true);

map.put(field.getName(), field.get(obj));

}

return map;

}

public static Object mapToObjectTwo(Map map, Class> beanClass) throws Exception {

if (map == null) {

return null;

}

Object obj = beanClass.newInstance();

Field[] fields = obj.getClass().getDeclaredFields();

for (Field field : fields) {

int mod = field.getModifiers();

if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {

continue;

}

field.setAccessible(true);

field.set(obj, map.get(field.getName()));

}

return obj;

}

6.排序

//插入

public static void insertSort(int[] a) {

int length = a.length;

for (int i = 1; i < length; i++) {

int insertNum = a[i];

int j = i - 1;

while (j >= 0 && a[j] > insertNum) {

a[j + 1] = a[j];

j--;

}

a[j + 1] = insertNum;

}

}

//希爾

public static void sheelSort(int a[]) {

int length = a.length;

for (int gap = length / 2; gap > 0; gap /= 2) {

for (int i = gap; i < length; i++) {

int insertNum = a[i];

int j = i - gap;

while (j >= 0 && a[j] > insertNum) {

a[j + gap] = a[j];

j -= gap;

}

a[j + gap] = insertNum;

}

}

}

//冒泡

public static void bubble(int[] a) {

int length = a.length;

int temp;

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length - i - 1; j++) {

if (a[j] > a[j + 1]) {

temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

}

//簡單選擇排序

public static void selectSort(int[] a) {

int length = a.length;

for (int i = 0; i < length; i++) {

//記錄當(dāng)前最小數(shù)和位置

int key = a[i];

int position = i;

for (int j = i + 1; j < length; j++) {

if (key > a[j]) {

key = a[j];

position = j;

}

}

//交換

a[position] = a[i];

a[i] = key;

}

}

//快速排序

public static void quickSort(int[] a, int start, int end) {

if (a == null || start >= end) return;

int key = a[start];

int i = start, j = end;

while (i < j) {

while (i < j && a[j] >= key) {

j--;

}

if (i < j) {

a[i++] = a[j];

}

while (i < j && a[i] <= key) {

i++;

}

if (i < j) {

a[j--] = a[i];

}

}

a[i] = key;

quickSort(a, start, i - 1);

quickSort(a, i + 1, end);

}

//基數(shù)排序

public static void radixSort(int[] a) {

int queueNum = 10;

int length = a.length;

//找到最大數(shù),判斷位數(shù)

int max = a[0];

for (int i = 1; i < length; i++) {

if (a[i] > max) {

max = a[i];

}

}

int times = 0;

while (max > 0) {

max /= 10;

times++;

}

//初始化10個隊列

ArrayList queue = new ArrayList<>();

for (int i = 0; i < queueNum; i++) {

ArrayList queue1 = new ArrayList<>();

queue.add(queue1);

}

for (int i = 0; i < times; i++) {

//分配數(shù)組元素

for (int j = 0; j < length; j++) {

//得到位數(shù)

int x = a[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);

ArrayList queue2 = queue.get(x);

queue2.add(a[j]);

queue.set(x, queue2);

}

//記錄元素數(shù)

int count = 0;

//收集隊列元素

for (int k = 0; k < queueNum; k++) {

while (queue.get(k).size() > 0) {

ArrayList queue3 = queue.get(k);

a[count] = queue3.get(0);

queue3.remove(0);

count++;

}

}

}

}

//堆排序

public static void heapSort(int[] a) {

int length = a.length;

for (int i = 0; i < length - 1; i++) {

adjustHeap(a, length - i - 1);

swap(a, 0, length - i - 1);

}

}

public static void adjustHeap(int[] a, int lastIndex) {

for (int i = (lastIndex - 1) / 2; i >= 0 ; i--) {

int k = i;

while (2*k + 1 <= lastIndex) {

int left = 2*k + 1;

if (left < lastIndex && a[left] < a[left + 1]) {

left++;

}

if (a[k] >= a[left]) break;

swap(a, k, left);

k = left;

}

}

System.out.println("調(diào)整后" + Arrays.toString(a));

}

//歸并排序

public static void mergeSortTwo(int[] a, int start, int end) {

if (start < end) {

int mid = (start + end) / 2;

mergeSortTwo(a, start, mid);

mergeSortTwo(a, mid + 1, end);

mergeTwo(a, start, mid, end);

}

}

public static void mergeTwo(int[] a, int start, int mid, int end) {

int[] temp = new int[a.length];

int p1 = start, p2 = mid + 1, k = start;

while (p1 <= mid && p2 <= end) {

if (a[p1] <= a[p2]) {

temp[k++] = a[p1++];

} else {

temp[k++] = a[p2++];

}

}

while (p1<=mid) {

temp[k++] = a[p1++];

}

while (p2<=end) {

temp[k++] = a[p2++];

}

for (int i = start; i <= end; i++) {

a[i] = temp[i];

}

}

總結(jié)

以上是生活随笔為你收集整理的java算法例子_java算法小例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。