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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

一道3G门户的面试题 (Java)

發布時間:2023/12/15 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一道3G门户的面试题 (Java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給一個大小為100的(整型)數組,分配0-1000的隨機數100個,但這100個數不能重復,然后把它從小到大排序,并輸出來。(時間10分鐘)

Java代碼如下:
?


package?org.lxh.demo16.common;

import?java.util.Random;

/**

?*?implement?file:?Tfrk.java

?*?desc:?給一個大小為100的(整型)數組,分配0-1000的隨機數100個,

?*?但這100個數不能重復,然后把它從小到大排序,并輸出來。(時間10分鐘)

?*?@author?hemmingway?<hemmingway@163.com>

?*

?*/

public?class?Tfrk?{

/**

?*?@param?args

?*/

public?static?void?main(String[]?args)?{

//?TODO?Auto-generated?method?stub

//采用插入排序法,?產生一個隨機數就插入,插入的過程排除重復數字的插入即可

Random?random?=?new?Random();

int[]?arrayList?=?new?int[101];

int?len;??//動態記錄已分配的數組長度

int?j;

//initialization

arrayList[0]?=?Math.abs(random.nextInt()?%?1000);

len?=?1;

while(true){

int?num=?Math.abs(random.nextInt()?%?1000);?

for(j?=?len-1;?j?>?-1?&&?num?<?arrayList[j];?j--)?;

//檢查重復的數字

if(?(j?==?-1?)?||?(?j?>?-1?&&?num?>?arrayList[j])?){

for(int?tmp?=?len-1;?tmp?>?j;?tmp--?){

arrayList[tmp+1]?=?arrayList[tmp];

}

arrayList[j?+?1]?=?num;

????len++;

}

?????

//隨機分配100個后,就跳出,不再分配

????if?(len?>?99)?break;

}?//?end?while(true)

??????

//打印輸出

for?(int?i?=?0;?i?<?100;?i++){

???System.out.println(?"arrayList["?+?i?+?"]:?"?+?arrayList[i]);

?}

}

}



C語言代碼

//?Test_rnd.cpp?:?定義控制臺應用程序的入口點。

//

#include?"stdafx.h"

#include?<stdio.h>

#include?<time.h>

#include?<stdlib.h>

int?main(int?argc,?char*?argv[])

{

int?arrayList[100];?

int?len;?? //動態記錄已分配的數組長度

int?j;

srand((unsigned)time(NULL));

//initialization

memset(arrayList,?0,?sizeof(arrayList)/sizeof(arrayList[0]));

arrayList[0]?=?rand()?%?1000;

len?=?1;

while(true) {

int?num=?rand()?%?1000;?

for(j?=?len-1;?j?>?-1?&&?num?<?arrayList[j];?j--)?;

//檢查重復的數字

if(?(j?==?-1?)?||?(?j?>?-1?&&?num?>?arrayList[j])?){

for(int?tmp?=?len-1;?tmp?>?j;?tmp--?){

arrayList[tmp+1]?=?arrayList[tmp];

}

arrayList[j?+?1]?=?num;

len++;

}

//隨機分配個后,就跳出,不再分配

if?(len?>?99)? break;

}?//?end?while(true)

//print

for?(int?i?=?0;?i<100;?i++)

{

printf("arrayList[%d]:?%d\n",i,arrayList[i]);

}

return?0;

}



總結

以上是生活随笔為你收集整理的一道3G门户的面试题 (Java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。