網頁

2017年4月25日 星期二

Ex6. C++河內塔

C++河內塔



26 則留言:

  1. B10333086 陳晏堂2017年4月25日 上午10:57

    #include <stdio.h>

    void hanoi(int n, char A, char B, char C) {
    if(n == 1) {
    printf("Move sheet from %c to %c\n", A, C);
    }
    else {
    hanoi(n-1, A, C, B);
    hanoi(1, A, B, C);
    hanoi(n-1, B, A, C);
    }
    }

    int main() {
    int n;
    printf("請輸入盤數:");
    scanf("%d", &n);
    hanoi(n, 'A', 'B', 'C');
    return 0;
    }

    回覆刪除
  2. B10333086 陳晏堂2017年4月25日 上午11:09

    #include <iostream>
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  3. #include <iostream>

    using namespace std;


    void hanoi(int,char*,char*,char*);


    int main()

    {

    cout << "input a number:";

    int number;

    cin >> number;


    char p1[]="A";

    char p2[]="B";

    char p3[]="C";


    hanoi(number,p1,p2,p3);

    system("pause");

    return 0;

    }

    void hanoi(int n,char* p1,char* p2,char* p3)

    {

    if(n==1){

    cout << "no." << n << " from " << p1 << " to " << p3 << endl;

    }else{

    hanoi(n-1,p1,p3,p2);

    cout << "no." << n << " from " << p1 << " to " << p3 << endl;

    hanoi(n-1,p2,p1,p3);

    }

    }

    回覆刪除
  4. B10333106 劉郁芃2017年4月25日 上午11:22

    #include <iostream>
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  5. #include
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  6. #include
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  7. #include
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  8. #include
    using namespace std;

    void hanoi(int, char, char, char);

    int main() {
    int n;

    cout << "請輸入盤數:";
    cin >> n;

    hanoi(n, 'A', 'B', 'C');

    return 0;
    }

    void hanoi(int n, char a, char b, char c) {
    if(n == 1)
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    else {
    hanoi(n - 1, a, c, b);
    cout << "盤 " << n << " 由 " << a << " 移至 " << c << "\n";
    hanoi(n - 1, b, a, c);
    }
    }

    回覆刪除
  9. B10333055 翁恩義2017年4月25日 上午11:39

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    void move(int n, char a, char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }

    int main()
    {
    ff.open("h.txt", ios::out);
    hanoiTower(5, 'a', 'b', 'c');
    ff.close();
    return 0;
    }

    回覆刪除
  10. B10333079 葉禮魁2017年4月25日 上午11:40

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    void move(int n , char a , char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from , char peg3 , char to){

    if (n==1) move(n,from,to);
    else{
    hanoiTower(n-1, from , to , peg3);
    move(n,from,to);
    hanoiTower(n-1, peg3,from , to );

    }

    }
    int main()
    {
    ff.open("123.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  11. #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;

    void move(int n, char a, char b){
    cout<<"Disc "<<n<<" 能"<<a<<"到"<<b<<endl;
    ff<<"Disc "<<n<<" 能"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt", ios::out);
    hanoiTower(5, 'a', 'b', 'c');
    ff.close();
    return 0;
    }

    回覆刪除
  12. #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    int counter=0;
    void move(int n, char a,char b)
    {
    cout<<++counter<<":盤子" <<n<<"從"<<a<<"至"<<b<<endl;
    ff<<":盤子" <<n<<"從"<<a<<"至"<<b<<endl;
    }

    void hanoiTower(int n, char from_peg, char peg3,char to_peg) {
    if(n == 1) move(n,from_peg,to_peg);
    else {
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n,from_peg,to_peg);
    hanoiTower(n-1,peg3,from_peg,to_peg);
    ;
    }
    }
    int main() {
    ff.open("t.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  13. #include <iostream>
    #include <fstream>

    using namespace std;
    fstream ff;
    int counter=0;
    void move(int n, char a, char b){
    cout<<++counter<<"Disc "<<n<<" 從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<" 從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1)move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  14. B10333053 梁竣翔2017年4月25日 上午11:46

    using namespace std;
    fstream ff;
    void move (int n, char a,char b){
    cout<<" Disc "<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<counter<<" Disc "<<n<<" 從 "<<a<<" 從 "<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }

    int main()
    {
    ff.open("h.txt", ios::out);
    hanoiTower(5, 'a', 'b', 'c');
    return 0;
    }

    回覆刪除
  15. B10433156 張詠淞2017年4月25日 上午11:47

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    void move(int n, char a, char b){
    cout<<" Disc "<<n<<"從"<<"到"<<b<<endl;
    ff<<" Disc " <<n<<"從"<<"到"<<b<<endl;
    }
    viod hanoiTower(int n, char form_peg, char peg3, char to_peg)
    {
    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt", ios::out);
    hanoiTower(5, 'a','b','c');
    }

    回覆刪除
  16. b103333001黃子安2017年4月25日 上午11:47

    #indlude
    #indlude
    using namespace std;
    fstream ff;
    void move (int n,char a, char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;.
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from_peg ,char from_peg3,char to_peg)
    { if(n==1)move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1,from_peg, to_peg,peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1,peg3, from_peg,to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    close();
    return 0;
    }

    回覆刪除
  17. b10133187 楊鈞文2017年4月25日 上午11:48

    #include
    #include
    using namespace std;
    fstream ff("h.txt", ios::out);
    void move(int n, char a, char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from_peg,char peg3,char to_peg)

    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1,from_peg, to_peg,peg3);
    move(n, from_peg,to_peg);
    hanoiTower(n-1,from_peg, to_peg,peg3);
    }
    }
    int main()
    {
    hanoiTower(5,'a','b','c');
    return 0;
    }

    回覆刪除
  18. b10133187 楊鈞文2017年4月25日 上午11:48

    #include
    #include
    using namespace std;
    fstream ff("h.txt", ios::out);
    void move(int n, char a, char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from_peg,char peg3,char to_peg)

    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1,from_peg, to_peg,peg3);
    move(n, from_peg,to_peg);
    hanoiTower(n-1,from_peg, to_peg,peg3);
    }
    }
    int main()
    {
    hanoiTower(5,'a','b','c');
    return 0;
    }

    回覆刪除
  19. #include <iostream>
    #include <fstream>

    using namespace std;
    fstream ff;
    int counter=0;
    void move(int n, char a, char b){
    cout<<++counter<<"Disc "<<n<<" 從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<" 從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1)move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  20. B10333086 陳晏堂2017年4月25日 上午11:52

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    void hanoi(int n, char a, char b);

    int move(int n,char a, char b) {
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n == 1) move(n,from_peg,to_peg);
    else {
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3,from_peg, to_peg );
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  21. B10333072 黃玟茜2017年4月25日 上午11:53

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;

    void move(int n, char a, char b){
    cout<< " Disc" <<n<< " 從" <<a<< " 到" <<b<<endl;
    ff<< " Disc" <<n<< " 從" <<a<< " 到" <<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n==1) move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3, from_peg, to_peg);
    }
    }

    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5, 'a', 'b', 'c');
    ff.close();
    return 0;
    }

    回覆刪除
  22. b103333001黃子安(更新版)2017年4月25日 上午11:58

    #include
    #include
    using namespace std;
    fstream ff;
    void move (int n,char a, char b){
    cout<<++counter<<": Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<counter<<": Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from_peg ,char from_peg3,char to_peg)
    { if(n==1)move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1,from_peg, to_peg,peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1,peg3, from_peg,to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    close();
    return 0;
    }

    回覆刪除
  23. B10333083,周琬芸2017年4月25日 中午12:12

    #include <iostream>
    #include <fstream>
    using namespace std;
    fstream ff;
    int counter=0;
    void hanoi(int n, char a, char b);

    int move(int n,char a, char b) {
    cout<<++counter<<"盤子"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n, char from_peg, char peg3, char to_peg)
    {
    if(n == 1) move(n,from_peg,to_peg);
    else {
    hanoiTower(n-1, from_peg, to_peg, peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1, peg3,from_peg, to_peg );
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  24. #include
    #include
    using namespace std;
    fstream ff;
    void move(int n , char a , char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from , char peg3 , char to){

    if (n==1) move(n,from,to);
    else{
    hanoiTower(n-1, from , to , peg3);
    move(n,from,to);
    hanoiTower(n-1, peg3,from , to );

    }

    }
    int main()
    {
    ff.open("123.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  25. #include
    #include
    using namespace std;
    fstream ff;
    void move(int n , char a , char b){
    cout<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<"Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from , char peg3 , char to){

    if (n==1) move(n,from,to);
    else{
    hanoiTower(n-1, from , to , peg3);
    move(n,from,to);
    hanoiTower(n-1, peg3,from , to );

    }

    }
    int main()
    {
    ff.open("123.txt",ios::out);
    hanoiTower(5,'a','b','c');
    ff.close();
    return 0;
    }

    回覆刪除
  26. #include
    #include
    using namespace std;
    fstream ff;
    void move (int n,char a, char b){
    cout<<++counter<<": Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    ff<<counter<<": Disc"<<n<<"從"<<a<<"到"<<b<<endl;
    }
    void hanoiTower(int n,char from_peg ,char from_peg3,char to_peg)
    { if(n==1)move(n, from_peg, to_peg);
    else{
    hanoiTower(n-1,from_peg, to_peg,peg3);
    move(n, from_peg, to_peg);
    hanoiTower(n-1,peg3, from_peg,to_peg);
    }
    }
    int main()
    {
    ff.open("h.txt",ios::out);
    hanoiTower(5,'a','b','c');
    close();
    return 0;
    }

    回覆刪除

HTML 編輯器