c語言重排九宮,c語言直接選擇排序

本文目錄一覽:

c語言 九宮格

#includestdio.h

#includemalloc.h

int N=15;

int main()

{

int i,j,k,m,n;

int G[N][N];

printf(“Input the odd number(number=15) you want !\n”);

scanf(“%d”,m);

if((m0)(m%2))

{

printf(“Your intput number is %d\n”,m);

n=m*m;

j=0;

k=m/2;

for(i=1;i=n;i++)

{

G[j][k]=i;

if(i%m==0)

if(j==m-1)

j=0;

else

j++;

else

{

if(j==0)

j=m-1;

else

j–;

if(k==m-1)

k=0;

else

k++;

}

}

for(i=0;im;i++)

{

for(j=0;jm;j++)

printf(“%6d”,G[i][j]);

printf(“\n”);

}

}

else

printf(“The number you inputed is ERROR!!!”);

system(“pause”);

}

可以接受15以內的任意魔方 你的九宮格按照這個程序輸出是這樣的:

C語言 簡化版九宮格

#include stdio.h

#define N 9

int check_row( int a[N][N], int r )

{

    int i,j;

    for( i=0; iN; i++ )

        for( j=i+1; jN; j++ )

            if ( a[r][i] == a[r][j] )

                return 0;

    return 1;

}

int check_column( int a[N][N], int c )

{

    int i,j;

    for( i=0; iN; i++ )

        for( j=i+1; jN; j++ )

            if ( a[i][c] == a[j][c] )

                return 0;

    return 1;

}

void get_total( int a[N][N], int *total1, int *total2 )

{

    int i;

    for( i=0; iN; i++ )

    {

        *total1 += a[i][i];

        *total2 += a[i][N-i-1];

    }

}

int main()

{

    int a[N][N], i, j, yes=1;

    for( i=0; iN; i++ )

        for( j=0; jN; j++ )

            scanf( “%d”, a[i][j] );

    

    for( i=0; iN; i++ )

    {

        if ( !check_row( a, i ) || !check_column( a, i ) )

        {

            yes = 0;

            break;

        }

    }

    printf( “%s\n”, yes ? “YES” : “NO” );

    int total1 = 0, total2 = 0;

    get_total( a, total1, total2 );

    printf( “%d %d\n”, total1total2?total1:total2, total1total2?total2:total1 );

}

1 2 3 4 5 6 7 8 9

2 3 4 5 6 7 8 9 1

3 4 5 6 7 8 9 1 2

4 5 6 7 8 9 1 2 3

5 6 7 8 9 1 2 3 4

6 7 8 9 1 2 3 4 5

7 8 9 1 2 3 4 5 6

8 9 1 2 3 4 5 6 7

9 1 2 3 4 5 6 7 8

求九宮問題程序(C語言實現)

【轉】

/*九宮圖算法說明:根據分析,九宮圖中間一位一定是5,另外關於中間位置

對稱的每兩個數字相加等於10,所以對於一個九宮圖只通過其相鄰位置的數

字就能計算出其他所有位置的數字,而且相鄰位置的數字必然一個比5大,一

個比5小

算法設計思想:先根據選取規則,從剩下八個數裡面選一大一小,再使用目

標函數檢測選取是否正確,如果正確則打印出,否則重新選取,選數的過程

使用回溯法*/

#includestdio.h

int place(int*);

void main(void){/*九宮圖算法*/

int a[9]={1,2,3,4,5,6,7,8,9};

int b[9]={0};/*表示數字的位置狀態*/

int j,k,i;

for(k=0;k=3;k++){/*對於所有比5小的數*/

b[1]=a[k];/*選一比5小的數放到第一行第二列*/

for(j=8;j=5;j–){/*對於所有比5大的數*/

if(k!=(8-j)){/*不選擇前面已選小數所對應的大數*/

b[0]=a[j];/*選一比5大的數放到第一行第一列*/

if(place(b)){/*若得到一解,則打印出*/

printf(“\n 九宮圖\n\n”);

for(i=0;i=8;i++){

printf(” %d “,b[i]);

if((i+1)%3==0){

printf(“\n\n\n”);

}

}

getch();

exit(0);

}

} /* 回溯到上一級*/

} /*回溯到根一級 */

}

}

int place(int*p){ /*計算出其他位置的數字並檢測是否為所求的解*/

p[2]=15-p[0]-p[1];

p[4]=5;

p[6]=10-p[2];

p[3]=15-p[0]-p[6];

p[5]=10-p[3];

p[7]=10-p[1];

p[8]=10-p[0];

if(p[2]+p[5]+p[8]==p[6]+p[7]+p[8]){/*其他行和列已通過計算檢測了,只有第三行,第三列未檢測*/

return(1);

}

else{

return(0);

}

}

【轉】

///////////////////////////////////////

// 九宮圖算法;

//////////////////////////////////////

#includesio.h

#includetime.h

#includeslib.h

//////////////////////////////////

//// the function defination

//////////////////////////////////

void create(int [][3]);

void show(int [][3]);

void set_value(int [][3]);

void aim_get(int [][3]);

void target(int [][3]);

void judge_x1(int [][3]);

void judge_x2(int [][3]);

void judge_x3(int [][3]);

void judge_x4(int [][3]);

void judge_x5(int [][3]);

void shift_all(int [][3]);

void shift_low_six(int [][3]);

void anti_shift_all(int [][3]);

void shift_low_four(int [][3]);

void last_shift(int [][3]);

void set_x5(int [][3]);

///////////////////////////////////////

////// the main function body ////

////////////////////////////////////////

main()

{

srand(time(NULL));

int cDiagram[3][3];

create(cDiagram); /////// creat the new array ,set the value are 10;

set_value(cDiagram);

//last_shift(cDiagram);

return 0;

}

///////////////////////////////////////

/// 建立一個3*3數組,初值都設為10;//

//////////////////////////////////////

void create(int array[][3])

{

printf(“\n\n***********************************\n\n”);

printf(“九宮圖算法實現過程\n\n”);

printf(“***********************************\n\n”);

int line;

int row;

for(line=0;line3;line )

{

for(row=0;row3;row )

{

array[line][row]=10;

}

}

// set_value(array);

//show(array);

}

/////////////////////////////////////////

/// 顯示數組狀態 ////

////////////////////////////////////////

void show(int array[][3])

{

for(int i=0;i3;i )

{

for(int j=0;j3;j )

{

printf(“=”,array[i][j]);

}

printf(“\n\n”);

}

}

///////////////////////////////

/// 產生數組的初始狀態 ///////

///////////////////////////////

void set_value(int array[][3])

{

int i=0;

int rand_num_line;

int rand_num_row;

printf(” \n\n九宮圖的初始值為:\n\n”);

while(i=8)

{

rand_num_line=rand()%3;

rand_num_row=rand()%3;

if(array[rand_num_line][rand_num_row]!=i array[rand_num_line][rand_num_row]==10)

{

array[rand_num_line][rand_num_row]=i;

i;

}

}

show(array);

//printf(” let’s begin!!\n”);

aim_get(array);

}

////////////////////////////////////////////////////////

//// judge the initial array get the target or no ! ///

//////////////////////////////////////////////////////////

void aim_get(int array[][3])

{

int aim[3][3]={{1,2,3},{8,0,4},{7,6,5}};

int line;

int row;

int judge=0;

for(line=0;line3;line )

{

for(row=0;row3;row )

{

if(array[line][row]!=aim[line][row])

{

judge=1;

}

}

}

if(judge==1)

{

judge_x1(array);

}

else

{

target(array);

}

}

/////////////////////////////////////

/////// the target diagram //////////

/////////////////////////////////////

void target(int array[][3])

{

printf(“\n\n the last diagram is :\n”);

show(array);

}

////////////////////////////////////

///judge the x1 is 1 or no! ///////

////////////////////////////////////

void judge_x1(int array[3][3])

{

//int x1=1;

int temp;

//printf(” \n\n\n the array[0][2]=%d\n\n”,array[0][2]);

if(array[0][2]!=1 array[0][2]!=0) // x3!=1 || x3!=0;

{

while(array[0][0]!=1)

{

//printf(“i am here!!1”);

temp=array[0][0];

array[0][0]=array[0][1];

array[0][1]=array[1][1];

array[1][1]=array[1][2];

array[1][2]=array[2][2];

array[2][2]=array[2][1];

array[2][1]=array[2][0];

array[2][0]=array[1][0];

array[1][0]=temp;

}

}

else

{

if(array[0][2]==0) // x3==0;

{

// printf(“\n\n array[0][2]=0\n\n”);

temp=array[0][2];

array[0][1]=array[0][2];

array[0][2]=temp;

judge_x1(array);

goto tt;

}

else /// x3==1;

{ //printf(“\n\narray[0][2] should is 1, %d”,array[0][2]);

if(array[1][1]==0) //// x0==0;

{

temp=array[0][1];

array[0][1]=array[1][1];

array[1][1]=temp;

judge_x1(array);

}

else //// x3==1 x0!=0;

{

shift_all(array);

judge_x1(array);

}

}

}

printf(” 確定了X1位置後,九宮圖為:\n”);

誰能編一個解決九宮數獨的程序(用C語言)

#include stdio.h

typedef struct

{

int line;

int row;

int num;

}Node;

int main()

{

/*

int a[9][9]={

{4,0,3,6,0,0,0,0,0},

{0,0,0,0,0,1,0,2,4},

{0,1,0,0,4,0,5,0,0},

{0,0,0,9,0,4,0,6,0},

{3,0,2,0,0,0,4,0,9},

{0,7,4,1,0,3,0,0,0},

{0,0,1,0,9,0,0,4,0},

{2,4,0,3,0,0,0,0,0},

{0,0,0,4,0,8,2,0,7}};

*/

int a[9][9]={

{0,0,0,8,0,0,0,6,0},

{8,7,0,0,0,0,0,0,0},

{2,9,0,0,4,1,0,0,5},

{0,0,5,7,0,0,0,0,9},

{0,2,0,0,0,0,0,1,0},

{9,0,0,0,0,4,3,0,0},

{7,0,0,6,1,0,0,9,8},

{0,0,0,0,0,0,0,5,2},

{0,6,0,0,0,9,0,0,0}};

/*

int a[9][9]={

{0,2,0,0,6,0,0,0,0},

{0,9,0,4,0,5,1,3,0},

{0,0,8,7,0,0,0,0,5},

{6,0,0,3,0,0,4,0,0},

{0,0,0,9,0,6,0,0,0},

{0,0,7,0,0,1,0,0,3},

{4,0,0,0,0,7,3,0,0},

{0,8,5,2,0,4,0,7,0},

{0,0,0,0,9,0,0,1,0}};

*/

/*

int a[9][9]={

{0,0,3,0,2,0,0,0,6},

{0,0,2,0,9,0,0,0,4},

{7,0,0,8,0,0,2,0,3},

{0,8,0,0,7,0,5,0,0},

{0,7,0,1,0,6,0,3,0},

{0,0,0,2,0,0,0,9,0},

{4,0,6,0,0,8,0,0,5},

{6,0,0,0,4,0,3,0,0},

{9,0,0,0,1,0,7,0,0}};

*/

int i,j,n,en,flag,y,k=0,x,qu,p,q;

Node b[70];

for(i=0;i9;i++)

{

for(j=0;j9;j++)

{

if(!a[i][j])

{

b[k].line=i;

b[k].row=j;

b[k].num=0;

k+=1;

}

}

}

en=k;

/*從b[0]開始試,若b[k].num9,則k-1,否則k+1*/

for(k=0;ken;)

{

++b[k].num;

i=b[k].line;

j=b[k].row;

a[i][j]=b[k].num;

n=0;

while(n9b[k].num=9)

{

if(n==i)

{

for(y=0;y9;y++)

{

if(y==j)

continue;

if(a[n][y]==a[i][j])

flag=1;

}

}

else if(n==j)

{

for(y=0;y9;y++)

{

if(y==i)

continue;

if(a[y][n]==a[i][j])

flag=1;

}

}

/*判斷同一塊中有沒有相同值*/

qu=3*(i/3)+j/3;

switch(qu)

{

case 0:x=0;

y=0;

break;

case 1:x=0;

y=3;

break;

case 2:x=0;

y=6;

break;

case 3:x=3;

y=0;

break;

case 4:x=3;

y=3;

break;

case 5:x=3;

y=6;

break;

case 6:x=6;

y=0;

break;

case 7:x=6;

y=3;

break;

default :x=6;

y=6;

break;

}

p=x;

q=y;

for(;xp+3;x++)

{

for(;yq+3;y++)

{

if(x==iy==j)

continue;

if(a[x][y]==a[i][j])

{

flag=1;

break;

}

}

if(flag==1)

break;

}

if(flag==1)

{

a[i][j]=++b[k].num;

flag=0;

n=0;

continue;

}

n++;

}

if(b[k].num9)

{

a[i][j]=b[k].num=0;

k–;

if(k0)

{

printf(“error!\r\n”);

return -1;

}

}

else

k++;

}

for(i=0;i9;i++)

{

for(j=0;j9;j++)

{

printf(“%d”,a[i][j]);

}

printf(“\r\n”);

}

return 1;

}

C語言編程輸出九宮格

#includestdio.h

#includestdlib.h

#includeconio.h

main()

{

#define n 3

int a[n][n]={0},i,sum=0,j;

i=0;j=n/2;

a[0][j]=++sum;

while(sumn*n+1)

{i–;j++;

if(i0j=n)

{i=i+2;j–;}

else

{if(i0)

i=n-1;

if(j=n)

j=0;

}

if(a[i][j]==0)

a[i][j]=++sum;

else

{

i=i+2;

j–;

a[i][j]=++sum;

continue;}

}

for(i=0;in;i++)

{for(j=0;jn;j++)

printf(“%5d”,a[i][j]);

printf(“\n”);}

getch();

}

原創文章,作者:GQWY,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/150104.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
GQWY的頭像GQWY
上一篇 2024-11-07 09:49
下一篇 2024-11-07 09:49

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論