标签:技术分享 [] 它的 bin print count tag auto gravity
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int i = 5; while (i >= 0) { for (int j = i; j > 0; j--) System.out.print("*"); System.out.println(); i--; } } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int i = 1; while (i <= 5) { for (int j = 0; j < i; j++) System.out.print("*"); System.out.println(); i++; } i = 4; while (i >= 0) { for (int j = i; j > 0; j--) System.out.print("*"); System.out.println(); i--; } } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int k = 5; for (int i = 1; i <= k; i++) { for (int m = k - i; m > 0; m--) System.out.print(" "); for (int n = i; n > 0; n--) System.out.print("* "); System.out.println(); } for (int i = k - 1; i >= 0; i--) { for (int m = 1; m <= k - i; m++) System.out.print(" "); for (int n = i; n > 0; n--) System.out.print("* "); System.out.println(); } } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <= 9; i++) for (int j = 0; j <= 9; j++) for (int k = 0; k <= 9; k++) { if (i * i * i + j * j * j + k * k * k == (i * 100 + j * 10 + k)) System.out.println(i * 100 + j * 10 + k); } } }
5.
问题一:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); funCount(sc.nextInt()); sc.close(); } private static void funCount(int type) { Scanner sc=new Scanner(System.in); int i=0,plus=0,minus=0; if(type==1){ System.out.println("输入10个数"); do{ if(sc.nextInt()>=0)plus++; else minus++; i++; }while(i<10); System.out.println("正数的个数为:"+plus+"负数的个数为:"+minus); } else{ System.out.println("输入任意个数字,以0结束"); int ip; while((ip=sc.nextInt())!=0){ if(ip>0)plus++; else minus++; } System.out.println("正数的个数为:"+plus+"负数的个数为:"+minus); } sc.close(); } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 2; i <= 100; i++) { int sum = 0; if (i == 2 || i == 3) System.out.println(i); else { for (int j = 2; j < i; j++) { if (i % j == 0) { sum++; break; } } if (sum == 0) System.out.println(i); } } } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <= 1000; i++) { int sum = 0; for (int j = 1; j <= i / 2; j++) { if (i % j == 0) sum += j; } if (sum == i) System.out.println(i); } } }
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int dataSort[] = new int[10]; for (int i = 0; i < dataSort.length; i++) { dataSort[i] = (int) (Math.random() * 200) - 100; } int dataBackup[] = dataSort;// 备份数组保证每次排序的初始内容一样 dataSort = dataBackup; StraightInsertionSort sis = new StraightInsertionSort(dataSort);// 直接插入排序 printData(sis.TAG, sis.data); dataSort = dataBackup; StraightSelectSort sss = new StraightSelectSort(dataSort);// 直接选择排序 printData(sss.TAG, sss.data); dataSort = dataBackup; BubbleSort bbs = new BubbleSort(dataSort);// 冒泡排序 printData(bbs.TAG, bbs.data); dataSort = dataBackup; ShellSort ss = new ShellSort(dataSort);// Shell排序 printData(ss.TAG, ss.data); dataSort = dataBackup; QuickSort qs = new QuickSort(dataSort); printData(qs.TAG, qs.data); dataSort = dataBackup; BinarySort bs = new BinarySort(dataSort);// 折半插入排序 printData(bs.TAG, bs.data); } private static void printData(String a, int[] d) { // TODO Auto-generated method stub System.out.printf("%-15s\t", new Object[] { a }); System.out.print("-->\t"); for (int i = 0; i < d.length; i++) { System.out.print(d[i] + "\t"); } System.out.println(); } } public class StraightInsertionSort { String TAG="直接插入排序"; int data[]; public StraightInsertionSort(int data[]){ this.data=data; sort(data); } private void sort(int[] d) { // TODO Auto-generated method stub int temp;// 存放最小元素 for (int i = 1; i < d.length; i++) { int j = i - 1; temp = d[i]; while (temp < d[j]) { d[j+1] = d[j]; j--; if (j < 0) break; } d[j + 1] = temp; } } } public class StraightSelectSort { String TAG="直接选择排序"; int data[]; public StraightSelectSort(int data[]){ this.data=data; sort(data); } private void sort(int[] d) { // TODO Auto-generated method stub int temp;// 临时变量 for (int i = 1; i < d.length; i++) { int k = i;// 记录最小元素位置 for (int j = i + 1; j < d.length; j++) { if (d[j] < d[k]) k = j; if (k != i) { temp = d[k]; d[k] = d[i]; d[i] = temp; } } } } } public class BubbleSort { String TAG = "冒泡排序"; int data[]; public BubbleSort(int data[]) { this.data = data; sort(data); } private void sort(int[] d) { // TODO Auto-generated method stub boolean hasSort = true;// 判断是否进行排序了的标识 int i = 0; while (i < d.length && hasSort == true) { hasSort = false; for (int j = 0; j < d.length - 1; j++) { if (d[j + 1] < d[j]) { int temp = d[j]; d[j] = d[j + 1]; d[j + 1] = temp; hasSort = true; } } i++; } } } public class ShellSort { String TAG="Shell排序"; int data[]; public ShellSort(int data[]){ this.data=data; sort(data); } private void sort(int[] d) { // TODO Auto-generated method stub for (int t = d.length / 2; t >= 1; t = t / 2) { for (int i = t ; i < d.length; i++) { int temp = d[i]; int j; for (j = i - t; j >= 0 && temp < d[j]; j = j - t) { d[j + t] = d[j]; } d[j + t] = temp; } } } } public class QuickSort { String TAG="快速排序"; int data[]; public QuickSort(int data[]){ this.data=data; sort(data,0,data.length-1); } private void sort(int[] d, int left, int right) { // TODO Auto-generated method stub if(left>=right)return; int temp=0; temp=d[left]; int l= left, r = right; while (l < r) { while (l<r&&d[r] > temp) r--; if(l<r)d[l++]=d[r]; while (l<r&&d[l] < temp) l++; if(l<r)d[r--]=d[l]; } d[l]=temp; sort(d, left, l-1); sort(d, l+1, right); } } public class BinarySort { String TAG = "折半插入排序"; int data[]; public BinarySort(int data[]) { this.data = data; sort(data); } private void sort(int[] d) { // TODO Auto-generated method stub for (int i = 1; i < d.length; i++) { int temp = d[i]; int left = 0, right = i - 1; while (left <= right) { int mid = (left + right) / 2; if (d[i] < d[mid]) right = mid - 1; else left = mid + 1; } for (int j = i - 1; j >= left; j--) d[j + 1] = d[j]; d[left] = temp; } } }
标签:技术分享 [] 它的 bin print count tag auto gravity
原文地址:http://www.cnblogs.com/zjfjava/p/5991593.html