導航:首頁 > 編程語言 > java大數最大

java大數最大

發布時間:2022-09-08 11:47:07

A. java 編寫問題 求最大數和次大數 要求用while

package test;
import java.util.Scanner;
public class MaxAndMin {
public static void main(String[] args) {
int i = 0;
int ten[] = new int[10];
int j = 0;
int max,min;
while(i<10){
Scanner sc = new Scanner(System.in) ;
ten[i] = sc.nextInt();
i++;
}
max = min = ten[0];
while(j<10){
if(max < ten[j]){
max = ten[j];
}
if(min > ten[j]){
min = ten[j];
}
j++;
}
System.out.println(max+"max "+min+"min ");
}
}

我學的也不好,演算法好久不用了,寫的不太好,你試試看吧,應該滿足你的需求。我自己敲的,選滿意答案啊,

B. java求數組中的最大數,並輸出最大數和它的位置。(數組內容自擬)

實現思路就是循環判斷找到最大值(和最小值),保存起來和其他的值進行比較。代碼如下:
public class Test
{
public static void main(String args[])
{
int i,min,max;
int A[]={74,48,30,17,62}; // 聲明整數數組A,並賦初值
min=max=A[0];
System.out.print("數組A的元素包括:");
int j =0;
int n =0 ;
for(i=0;i<A.length;i++)
{
System.out.print(A[i]+" ");
if(A[i]>max) // 判斷最大值
j =i;
max=A[i];
if(A[i]<min) // 判斷最小值
min=A[i];
n =i
}
System.out.println("\n數組的最大值是:"+max+".數組的位置是:"+(j+1)); // 輸出最大值和最大值的位置
System.out.println("數組的最小值是:"+min+".數組的位置是:"+(n+1)); // 輸出最小值
}
}

C. 用java求倆個超大數的最大公約數,不允許用大數類,該怎麼求

1.兩個數num1,num2
2.long min = Math.min(num1,num2);
3.for(long i = min ; min>1;i--){
判斷i是否可以同時整除num1和num2;
如果可以,return i;
如果不可以,繼續循環
}

簡單吧,能理解么?需要就追問,我把代碼給你

D. java求最大數和次大數

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GetMaxNum {

private Number[] source;

public Number[] makeArray(String str, String sep) {
String[] arr;
if (str != null && !str.equals("")) {
arr = str.split(sep);
} else {
arr = new String[0];
}
this.source = new Number[arr.length];
for (int i = 0; i < arr.length; i++) {
source[i] = Double.parseDouble(arr[i]);
}
return source;
}

/*
* 冒泡排序方法:遞增順序
*/
public Number[] doSort_asc() {
for (int i = source.length - 1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (source[j].doubleValue() > source[j + 1].doubleValue()) {
Number tmp = source[j];
source[j] = source[j + 1];
source[j + 1] = tmp;
}
}
}
return source;
}
/*
* 冒泡排序方法:遞減順序
*/

public Number[] doSort_desc() {
for (int i = source.length - 1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (source[j].doubleValue() < source[j + 1].doubleValue()) {
Number tmp = source[j];
source[j] = source[j + 1];
source[j + 1] = tmp;
}
}
}
return source;
}
/*
* 所有數據顯示
*/

public void display() {
for (int i = 0; i < source.length; i++) {
System.out.print(source[i] + " ");
}
System.out.print("\n");
}

public void display(Number[] source) {
for (int i = 0; i < source.length; i++) {
System.out.print(source[i] + " ");
}
System.out.print("\n");
}
/*
* 顯示前兩個
*/

public void display2() {
for (int i = 0; i < source.length; i++) {
if (i < 2) {
System.out.print(source[i] + " ");
}
}
System.out.print("\n");
}

public void display2(Number[] source) {
for (int i = 0; i < source.length; i++) {
if (i < 2) {
System.out.print(source[i] + " ");
}
}
System.out.print("\n");
}

public static void main(String[] args) throws IOException {
System.out.println("請輸入一個數值類型的字元串(空格分隔):");
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String str = buffer.readLine();
GetMaxNum gmn = new GetMaxNum();
Number[] num = gmn.makeArray(str, " ");
Number[] num2 = gmn.doSort_desc();
System.out.println("你的要求結果:");
gmn.display2(num2);
gmn = null;
num = null;
num2 = null;
str = null;
buffer = null;
}
}
//注釋:以上實現原理,從鍵盤輸入任意的數值型字元串,空格隔開,回車結束;立即顯示結果:你輸入值的最大值和此大值,當然還可以顯示排序(遞增或者遞減)後的值,自己測試一下結果吧!

E. java中找出三個數最大的!

//判斷三個數中的大數輸出
//2010-9-16

import java.util.Scanner;

public class ThreeBig {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //掃描器
int one,two,three; //三個數

//輸入三個數,輸出
System.out.print("請輸入第1個數 :");
one = input.nextInt();
System.out.print("請輸入第2個數 :");
two = input.nextInt();
System.out.print("請輸入第3個數 :");
three = input.nextInt();

//方法一
// int Big = one;
//
// if(Big < two)
// Big = two;
// if(Big < three)
// Big = three;
//
// System.out.println("\n最大數為" + Big);

//方法二
// if( one > two ){
// if( one > three )
// System.out.println("\n最大數為大" + one );
// else
// System.out.println("\n最大數為" + three );
// }else{
// if( two > three )
// System.out.println("\n最大數為" + two );
// else
// System.out.println("\n最大數為" + three );
// }
//

//方法三
// if( one>two&&one>three ){
// System.out.println("最大數為 :" + one);
// }else if(two>three){
// System.out.println("最大數為 :" + two);
// }else{
// System.out.println("最大數為 :" + three);
// }

//方法四,加排序過程
int x = one;//轉換變數
if( one < two ){
one = two;
two = x;
}

if( three > one )
System.out.println("三個數 這樣排序 : " + three + "\t" + one + "\t" + two);
else if(three < two )
System.out.println("三個數 這樣排序 :" + one + "\t" + two + "\t" + three);
else
System.out.println("三個數 這樣排序 :" + one + "\t" + three + "\t" + two);

}
}

F. 用Java實現:求數組中的最大數,最小數,次大數,次小數,按順序排列

Arrays.sort(a)對數組a進行從小到大排序。
最大數a[a.length-1],次大數a[a.length-2]
最小數a[0],次小數a[1]

G. Java任意輸入一個長整數,找出其中最大的數字並輸出。例如:輸入19871002,輸出9

/*這個程序可以比較無數個數字,不局限
/**這是一個命令行版本,使用時在命令行輸入java Maxnum <你要比較的數,數字間用空格分開>,比如java Maxnum 22 33 2 56 38*/
public class Maxnum{
int num[];
public int getMax(){
int temp;
for(int i=0;i<num.length;i++)
for(int j=1;j<num.length-i;j++){
if(num[i]<num[j]){
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
return num[0];
}
Maxnum(int num[]){
m=num;
}
public static void main(String args[]){
int no[]=new int[args.length];
for(int i=0;i<args.length;i++)
no[i]= rseInt(args[i]);
Maxnum maxn=new Maxnum(no);
intln("最大的數為:"+ tMax());
}
}

H. java中最大的int型數據佔多少位兩個最大的int型數據相乘佔多少位

一個int型是占的32位,連個相乘就是64位,如果你要表示64位的話,就直接用long型就可以了

I. java語言中最大怎麼表示

Java API 中有幾個最大值函數,他們是:public static double max(double a, double b),public static int max(int a, int b),還有其他的,只是返回值類型不同而已。
舉個例子說明:double d=Math.max(2, 6);
System.out.println(d);
則d的值等於6.0,這就是java中最大函數的表示。

閱讀全文

與java大數最大相關的資料

熱點內容
androidm3u8緩存 瀏覽:234
imphp開源知乎 瀏覽:706
清除網路通配符dos命令 瀏覽:837
鴻蒙系統怎麼快速換回安卓 瀏覽:712
pdf綠色虛擬列印機 瀏覽:213
androidtab框架 瀏覽:147
java轉php的時間戳 瀏覽:638
編譯libstdc依賴 瀏覽:657
清演算法人與原法人的區別 瀏覽:410
家庭裝修下載什麼app軟體 瀏覽:575
美食博主用什麼app拍視頻 瀏覽:815
ipone手機如何加密微信 瀏覽:357
自來水加密閥閥帽 瀏覽:437
華為交換機dhcp配置命令 瀏覽:319
androidbitmap縮小 瀏覽:275
單片機串口控制燈 瀏覽:88
大訊雲伺服器安裝視頻 瀏覽:788
華為演算法領先世界 瀏覽:658
linux路由重啟 瀏覽:570
php的模板編程 瀏覽:324