在校園網的論壇上看到的一些題目
放假了沒有什麼事做就想到了自己來試著做一些
到今天 才總是把它給做成了我想要實現的結果了
有時候就是那樣
做了好久
都沒辦法做出來
到靈感來了的那一下
一下子就可以做出來
心裡有點高興
在這個過程中
我對JAVA的IO了解得比以前多了一點
雖然還不是很熟悉
但大概也知道怎麼用了
如下
給定等式 A B C D E 其中每個字母代表一個數字
且不同數字對應不
D F G 同字母
編程求出這些數字並且打出這個數字的
+ D F G 算術計算豎式
───────
語句
X Y Z D E
下面是我自己的JAVA代碼
裡面有很大一段是人家寫的
個數的全排列
我只是把修改成了JAVA
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075897.gif)
import java
io
*;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075836.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075861.gif)
public class PaiLei
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
static int count=
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
private char[] symbol=
{
A
B
C
D
E
F
G
X
Y
Z
} ;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
int [] num=
{
};
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
public void check()
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//System
out
print(
進來了呀
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//temp
表示數字ABCDE
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int temp
=num[
]*
+num[
]*
+num[
]*
+num[
]*
+num[
];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//temp
表示數字DFG
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int temp
=num[
]*
+num[
]*
+num[
];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//temp
表示數字XYZDE
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int temp
=num[
]*
+num[
]*
+num[
]*
+num[
]*
+num[
];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//判斷表達式是不是成立
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//System
out
print(
進入循環
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
if((temp
+temp
*
)==temp
)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
count++;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(temp
+temp
+temp
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(temp
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(
第
+count+
種答案
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
print();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//function方法是核心方法
就是求
個數的全排列
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
/** *//**
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
例
:將
到N的N個自然數排成一列
共有
*
*
……*N種不同的排列方法
如
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
N=
時
有
種排列方案
分別為
試編程序輸出
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
到N的全部排列
假設N<
為了設計出由計算機輸出
到N的全部排列程序
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
就必須尋找不同排列之間的規律
通過觀察N=
(參見本例的運行結果)的排
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
列情況
可以發現
如果把每個排列看作一個自然數
則所有排列對應的數是
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
按從小到大的順序排列
從當前的排列產生下一個排列時必然會造成某一 位
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
置上的數字變大
這一位置顯然應該盡量靠右
並且在它左邊位置上的數字保
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
持不變
這就意味著這一位置變成的數字來自於它的右邊
並且變大的幅度
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
要盡可能小
也就是說在它右邊如有幾個 數同時比它大時
應該用其中最小的
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
來代替它
由於這一位置是滿足上述條件的最右邊的一位
所以在它右邊的所
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
有數字按逆序排列
即在這些數字的右邊沒有一個大於它的數
程序中先從右
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
至左找到第一個位置
該位置上的數比它右邊的數小
這個位置就是所要找的
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
滿足上述條件的位置
然後再從右到左找到第一個比該位置上的數字大的數
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
字所在的位置
將這兩個位置上的數字交換
再將該位置右邊的所有元素顛倒
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
過來
即將它們按從小到大的順序排列
就得到了下一個排列
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
結果應該是
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
Input n:
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
#include<iostream>
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
using namespace std;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
void main(){
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int n; //n個數字的排列;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
cout<<
input:
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
cin>>n;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int *p=new int[n]; //數組保存各位數字;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
for(int i=
;i<n;i++) //初始化為第一個排列;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
p[i]=i+
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
do{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
for(i=
;i<n;i++) //輸出排列中的數字
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
cout<<p[i]<<
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
cout<<endl;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
for(int j=n
;j>
;j
) //從右向左;找要交換的位置
(j);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(p[j]>p[j
]) break;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(j==
)break; //找不到要交換的位置
退出do
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
j
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
for(int k=n
;k>j;k
) //在位置
右邊;從右向左;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//找要交換的位置
(k);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(p[k]>p[j])break;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
swap(p[k]
p[j]); //交換位置
和位置
的值;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//把位置
後邊的所有位反序排列;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
for(int x=j+
y=n
;x<y;x++
y
)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
swap(p[x]
p[y]);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
}while(
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
delete []p;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
*/
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
public void function()throws IOException
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
int i
temp
j;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
do
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
check();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//write();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(i=
;i>
;i
)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(num[i]>num[i
])
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
break;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(i==
)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
break;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
i
;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(j=
;j>i;j
)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
if(num[j]>num[i]) break;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
temp= num[j];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
num[j]=num[i];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
num[i]=temp;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//下面循環是
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(int x=i+
y=
;x<y;x++
y
)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
temp=num[x];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
num[x]=num[y];
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
num[y]=temp;
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}while(true);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//輸出正確結果的方法
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
public void print()
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(int j=
;j<=
;j++)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//輸出ABCDEFGXYZ
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
print(symbol[j]+
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(int j=
;j<=
;j++)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//輸出他們對應的值
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
print(num[j]+
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(
+num[
]+num[
]+num[
]+num[
]+num[
]);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(
+num[
]+num[
]+num[
]);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(
+
+num[
]+num[
]+num[
]);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println(
+num[
]+num[
]+num[
]+num[
]+num[
]);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
System
out
println();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//將所有排列寫入記事本
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
public void write()throws IOException
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
File myfile=new File(
d:\paiei
txt
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
FileOutputStream out=new FileOutputStream(myfile
true);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
DataOutputStream out
=new DataOutputStream(out);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
byte[] temp=
{
};
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
for(byte i=
;i<=
;i++)
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
temp[i]=(byte)num[i]; //num數組是一個int型
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
out
writeByte(temp[i]+
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
PrintStream out
=new PrintStream(out);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
out
print(
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// out
close();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
PrintStream out
=new PrintStream(out);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
out
println();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// out
close();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
//==============下面幾行注釋了的是
向控治台輸出顯示
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// ByteArrayInputStream in=new ByteArrayInputStream(temp);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// int data=in
read();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// while(data!=
){
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// //out
write(temp);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// System
out
print(
+data);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// data=in
read();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// }
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// System
out
print(
);
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
// in
close();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
out
close();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075902.gif)
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075984.gif)
public static void main(String [] args) throws IOException
{
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
PaiLei p=new PaiLei();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075848.gif)
p
function();
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075958.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075977.gif)
}
![](http://tw.wingwit.com/Article/UploadFiles/201311/2013112319075991.bmp)
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26328.html