import java.util.*;

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比龍游網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式龍游網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋龍游地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
public class Mat {
public static void main(String[] args) {
// TODO 自動生成的方法存根
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int s1=2*n;
int s2=1;
for(int i=1;i=n;i++)
s2=s2*i;
System.out.printf(s1+" "+s2);
}
}
連連看的小源碼
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標(biāo)
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再來一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i++) {
for(int j=0;j=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //這里一定要將按鈕點(diǎn)擊信息歸為初始
init();
for(int i = 0;i 6;i++){
for(int j = 0;j 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判斷是否相鄰
remove();
}
else{
for (j=0;j7;j++ ) {
if (grid[x0][j]==0){ //判斷第一個(gè)按鈕同行哪個(gè)按鈕為空
if (yj) { //如果第二個(gè)按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕左邊
for (i=y-1;i=j;i-- ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說明通過了第一次驗(yàn)證
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二個(gè)按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕右邊
for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按鈕同行空按鈕在左邊
for (i=y0-1;i=j ;i-- ){ //判斷第一按鈕同左側(cè)空按鈕之間有沒按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說明通過了第二次驗(yàn)證
}
}
if (y0j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
/*這個(gè)相當(dāng)詳細(xì)了.
程序也不算太難.而且給老師看的時(shí)候效果比較好.因?yàn)橛袌D形化界面,又實(shí)現(xiàn)一個(gè)比較實(shí)用的功能.老師會比較高興的.
建立一個(gè)文件名為Change.java就可以編譯了*/
/*
* 這個(gè)程序?qū)崿F(xiàn)輸入身高算出標(biāo)準(zhǔn)體重,輸入體重,算出身高的功能
*/
import java.awt.*; //導(dǎo)入相關(guān)類包,這才樣使用相應(yīng)awt圖形界面的類
import java.awt.event.*;//同上
public class Change extends Frame { //定義一個(gè)類Change, 父類是Frame(圖形界面的)
Button b = new Button("互查"); //創(chuàng)建一個(gè)按鈕的對象b,顯示為"互查"
Label l1 = new Label("身高(cm)");//創(chuàng)建一個(gè)lable.顯示身高
Label l2 = new Label("體重(kg)");//創(chuàng)建一個(gè)lable 顯示體重
double heigth, weigth; //定義變量
double x, y; //定義變量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//類的構(gòu)造函數(shù),完成初始化
super("互查表");//創(chuàng)建窗口,標(biāo)題為互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//設(shè)置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 體重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自動放到窗口里排列上邊的組件
setVisible(true);//可以讓用戶看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 關(guān)閉窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button監(jiān)聽函數(shù)
}
class ButtonListener implements ActionListener {//實(shí)現(xiàn)click button時(shí)功能操作
public void actionPerformed(ActionEvent e) {//當(dāng)click調(diào)用
if (tf1.getText()!=null) {//檢查tf1 test 是否為空
try {//取異常
x = Double.parseDouble(tf1.getText());//字符轉(zhuǎn)為double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText("" + weigth);//顯示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果輸入不是數(shù)字,設(shè)為空
}
}
if (tf1.getText().equals("")==true){//tf1是否為空
y = Double.parseDouble(tf2.getText());//把tf2里的文本轉(zhuǎn)為double 型 的
heigth = y / 0.9 + 100; //算身高根據(jù)重量
tf1.setText("" + heigth);}//顯示身高
}
}
public static void main(String[] args) {//主函數(shù),程序入口
new Change(); //建立類Change的對象,并調(diào)用他的構(gòu)造函數(shù)Change().顯示窗口
}
}
最有效,切不復(fù)雜的方法使用Breadth First Search (BFS). 基本代碼如下(偽代碼)。因?yàn)锽FS不用遞歸,所以可能會有點(diǎn)難理解。
public Stack findPath(Vertex 起始景點(diǎn), Vertex 目標(biāo)景點(diǎn)){
Queue Vertex q = new QueueVertex();
s.enqueue(起始景點(diǎn));
Vertex 當(dāng)前位置;
while(!s.isEmpty()){
當(dāng)前位置 = s.dequeue();
if (當(dāng)前位置 == 目標(biāo)景點(diǎn)) break;
for (每一個(gè)相鄰于 當(dāng)前位置 的景點(diǎn) Vertex v){
if (!v.visited){
v.parent = 當(dāng)前位置;
// 不是規(guī)定,不過可以節(jié)省一點(diǎn)時(shí)間
if (v == 目標(biāo)景點(diǎn)){
current = v;
break;
}
s.enqueue(Vertex v);
v.visited = true;
}
}
}
Stack Vertex solution = new Stack Vertex();
Vertex parent = current;
while (parent != 起始景點(diǎn)){
solution.push(parent);
parent = current.parent;
}
for (graph中的每一個(gè)vertex) vertex.visited = false;
return solution(); // 其實(shí)這里建議用一個(gè) Path 的inner class 來裝所獲得的路線
}
然后再 main 求每兩個(gè)景點(diǎn)之間的距離即可
public static void main(String[] argv){
PathFinder pf = new PathFinder();
Stack[][] 路徑 = new Stack[10][10];
for(int i=0; ipf.vertices.length; i++){
for(int j=i+1; jpf.vertices.length; j++){
Stack s = pf.findPath(pf.vertices[i], pf.vertices[j]);
路徑[i][j] = s; 路徑[j][i] = s; // 假設(shè)你的graph是一個(gè)undirected graph
}
}
// 這么一來就大功告成了!對于每兩個(gè)景點(diǎn)n 與 m之間的最短路徑就是在 stack[n][m] 中
}
還有一種方法就是用Depth First Search遞歸式的尋找路徑,不過這樣比較慢,而且我的代碼可能會造成stack overflow
public Stack dfs(Vertex 當(dāng)前景點(diǎn),Vertex 目標(biāo)景點(diǎn)){
if(當(dāng)前景點(diǎn) == 目標(biāo)景點(diǎn)) return;
Stack solution = new Stack();
Stack temp;
for (相鄰于 點(diǎn)錢景點(diǎn) 的每一個(gè) Vertex v){
if (!v.visited){
v.visited = true;
temp = dfs(v, 目標(biāo)景點(diǎn));
// 抱歉,不記得是stack.size()還是stack.length()
if (solution.size() == 0) solution = temp;
else if(temp.size() solution.size()) solution = temp;
v.visited = false; 復(fù)原
}
}
return solution;
}
然后再在上述的Main中叫dfs...
參考:
名稱欄目:java隨便代碼 一段java代碼的詳細(xì)解說
文章起源:http://chinadenli.net/article28/dojocjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、App設(shè)計(jì)、網(wǎng)站維護(hù)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)