回 帖 发 新 帖 刷新版面

主题:[原创]没事一起写程序吧(入门)

没事一起写程序吧(入门)
题目::“入门必做的题”http://www.programfan.com/club/post-157289.html
编程环境:DEV_CPP

[size=4][color=FF0000][b]提建议者或参与者必加分[/b][/color][/size]

[size=3][b][color=FF0000]已完成题目:1、2、3、4[/color][/b][/size]

回复列表 (共31个回复)

11 楼

[quote]楼主,你的这个算法貌似太。。。。for循环太多啦,对程序来讲,这个是不太好的吧。。

有没有更好一点的算法?。。。想一想。。。[/quote]

你好!我也知道用了最傻的办法了,也笔算过结果也很容易出来,一看就知道G=0和F=5,但是这样就显示不了编程的有用性了。

希望你能给一个合理的算法。谢谢!

12 楼

[quote]难道没有更好的方法了吗
[/quote]
希望你能给出好方法。谢谢!

13 楼

[quote]我也在开始做 一起写程序吧 哈哈
以前一直想做但没做  这回回过头来再做下  一起+U+U吧~~
我的第三题 好像跟你的差不多 哦

不过LZ的第一个for循环不要循环到K-1就可以了吧 只要他的一半就可以了吧 ~~


[/quote]
你好!  我们一起开始编程吧!
谢谢你的指点,我已经改好了。谢谢!~

14 楼

 4. 在N行N列的数阵中, 数K(1〈=K〈=N)在每行和每列中出现且仅
  出现一次,这样的数阵叫N阶拉丁方阵。例如下图就是一个五阶拉丁方阵。
  编一程序,从键盘输入N值后,打印出所有不同的N阶拉丁方阵,并统计个数。

        1  2  3  4  5
        2  3  4  5  1
        3  4  5  1  2
        4  5  1  2  3
        5  1  2  3  4
[code=c]
方法1:
#include<stdio.h>
#include<conio.h>
int main(void)
{
 int a,n,i,j,k;
 printf("please enter N:");
 scanf("%d",&n);
 int b[2*n];
 for(i=0;i<2;i++)
  for(j=0;j<n;j++)
   b[i*n+j]=j+1;
 for(i=0,k=0;i<n;i++,k++)
 {
  for(j=0;j<n;j++)
   printf("  %d",b[j+k]);
  printf("\n");
 }
 getch();
}

方法2:
#include<stdio.h>
#include<conio.h>
int main(void)
{
 int n,i,j;
 printf("please enter N:");
 scanf("%d",&n);
 int a[n][n];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   {
    if(i+j+1<=n)
     a[i][j]=i+j+1;
    else
     a[i][j]=i+j+1-n;    
   }
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",a[i][j]);
  printf("\n");
 }
 getch();
}

[/code]

15 楼

[quote][quote]楼主,你的这个算法貌似太。。。。for循环太多啦,对程序来讲,这个是不太好的吧。。

有没有更好一点的算法?。。。想一想。。。[/quote]

你好!我也知道用了最傻的办法了,也笔算过结果也很容易出来,一看就知道G=0和F=5,但是这样就显示不了编程的有用性了。

希望你能给一个合理的算法。谢谢!
[/quote]

*
稍微简单的判断一下,以减少计算量
G=5或0;
F=5或0;
X=A+1,而且A小于9;
因为总共有10个各不相同的数字,所以:
Z=45-A-B-C-D-E-F-G-X-Y;
*/

我暂时没想到什么好的算法。。。在网上看有一些代码,比如穷举算法。。。

我给你贴出来。。。有时候好好,看对你有没有用处,不能保证能通过编译哦,因为我

没拿它们运行,只是帮你找找,楼主你自己试试吧。。。不行的话自己改改,看可以

么?[em12]


[code=c]
#include <stdio.h>
#include <conio.h>
int num[13];
long i,j;
void seperate(int* nums, long abcde, long dfg, long xyzde);
int check(void);
int main()
{
for(i=10234;i<=98765;i++)
{for(j=102;j<=987;j++)
{seperate(num,i,j,i+j*2);
if(check()) goto ok;
}}
ok:
printf("      %d%d%d%d%d\n",num[0],num[1],num[2],num[3],num[4]);
printf("        %d%d%d\n",num[5],num[6],num[7]);
printf("    +    %d%d%d\n",num[5],num[6],num[7]);
printf("----------\n");
printf("      %d%d%d%d%d\n",num[8],num[9],num[10],num[11],num[12]);
getch();
}
void seperate(int* nums, long abcde, long dfg, long xyzde)
{
long n,p;
n=abcde;
nums[4]=n%10;
n=n/10;
nums[3]=n%10;
n=n/10;
nums[2]=n%10;
n=n/10;
nums[1]=n%10;
n=n/10;
nums[0]=n%10;
n=dfg;
nums[7]=n%10;
n=n/10;
nums[6]=n%10;
n=n/10;
nums[5]=n%10;
n=xyzde;
nums[12]=n%10;
n=n/10;
nums[11]=n%10;
n=n/10;
nums[10]=n%10;
n=n/10;
nums[9]=n%10;
n=n/10;
nums[8]=n%10;
}
int check()
{
int temp[10],m,n;
if(num[0]==num[8]) return 0;
if(num[1]==num[9]) return 0;
if(num[2]==num[10]) return 0;
if(num[3]!=num[11]||num[3]!=num[5]) return 0;
if(num[4]!=num[12]) return 0;
temp[0]=num[0];
temp[1]=num[1];
temp[2]=num[2];
temp[3]=num[3];
temp[4]=num[4];
temp[5]=num[6];
temp[6]=num[7];
temp[7]=num[8];
temp[8]=num[9];
temp[9]=num[10];
for(m=0;m<10;m++)
{
for(n=m+1;n<10;n++)
if(temp[m]==temp[n]) return 0;
}
return 1;
}
[/code]



[code=c]
void NumAnalyse(){ 
int a,b,c,d,e,f,g,x,y,z; 
for(a=0;a<10;a++) 
for(b=0;b<10;b++) 
if(b==a) 
continue; 
else 
for(c=0;c<10;c++) 
if(c==a || c==b) 
continue; 
else 
for(d=0;d<10;d++) 
if(d==a || d==b || d==c) 
continue; 
else 
for(e=0;e<10;e++) 
if(e==a || e==b || e==c ||e==d) 
continue; 
else 
for(f=0;f<10;f++) 
if(f==a || f==b || f==c || f==d || f==e) 
continue; 
else 
for(g=0;g<10;g++) 
if(g==a || g==b || g==c || g==d || g==e || g==f) 
continue; 
else 
for(x=0;x<10;x++) 
if(x==a || x==b || x==c || x==d ||x==e || x==f || x==g) 
continue; 
else 
for(y=0;y<10;y++) 
if(y==a || y==b || y==c ||y==d||y==e||y==f||y==g||y==x) 
continue; 
else 

z=45-a-b-c-d-e-f-g-x-y; 
if(a*10000+b*1000+c*100+d*10+e + d*100+f*10+g+d*100+f*10+g == x*10000+y*1000+z*100+d*10+e) 
printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d,x=%d,y=%d,z=%d\n",a,b,c,d,e,f,g,x,y,z); 



main() 


NumAnalyse(); 
getchar(); 
}
[/code]

16 楼


[code=c]
/*第二题的代码,楼主给你做下参考,看看行不行。。。呵。。。*/ 

#include <stdio.h>
#include <conio.h>
#define TestBit(number,bit) (((number)&(1<<(bit)))!=0)

int main(void)
{
  int i,j;
  for(i=0; i<32; ++i)
  {
    if( TestBit(i,0) && !TestBit(i,1) )   // A参加但B不参加,不符合第一条
        continue;
    if( TestBit(i,1) && TestBit(i,2) )   // B和C都参加,不符合第二条
        continue;
    if( TestBit(i,2) != TestBit(i,3) )   // C和D只有一个参加,不符合第三条
        continue;
    if( !TestBit(i,3) && !TestBit(i,4) )   // D和E都没参加,不符合第四条
        continue;
    if( TestBit(i,4) && !(TestBit(i,0)&&TestBit(i,3)) )   // D参加,但A和D不全参加,不符合第五条
        continue;

    printf("参加竞赛的人是:");
    for(j=0;j<5;++j)
        if(TestBit(i,j))
          putchar('A'+j);
    printf("\n");
  }
  getch();
  return 0;
}
[/code]

17 楼


[code=c]
/*第二题的代码,楼主给你做下参考,看看行不行。。。呵。。。*/ 

#include <stdio.h>
#include <conio.h>
#define TestBit(number,bit) (((number)&(1<<(bit)))!=0)

int main(void)
{
  int i,j;
  for(i=0; i<32; ++i)
  {
    if( TestBit(i,0) && !TestBit(i,1) )   // A参加但B不参加,不符合第一条
        continue;
    if( TestBit(i,1) && TestBit(i,2) )   // B和C都参加,不符合第二条
        continue;
    if( TestBit(i,2) != TestBit(i,3) )   // C和D只有一个参加,不符合第三条
        continue;
    if( !TestBit(i,3) && !TestBit(i,4) )   // D和E都没参加,不符合第四条
        continue;
    if( TestBit(i,4) && !(TestBit(i,0)&&TestBit(i,3)) )   // D参加,但A和D不全参加,不符合第五条
        continue;

    printf("参加竞赛的人是:");
    for(j=0;j<5;++j)
        if(TestBit(i,j))
          putchar('A'+j);
    printf("\n");
  }
  getch();
  return 0;
}
[/code]

18 楼

[quote][quote]我也在开始做 一起写程序吧 哈哈
以前一直想做但没做  这回回过头来再做下  一起+U+U吧~~
我的第三题 好像跟你的差不多 哦

不过LZ的第一个for循环不要循环到K-1就可以了吧 只要他的一半就可以了吧 ~~


[/quote]
你好!  我们一起开始编程吧!
谢谢你的指点,我已经改好了。谢谢!~[/quote]

好啊 ,不过平时要上班,不一定有那么多时间,尽量抽时间啦~~~

19 楼

[quote] 4. 在N行N列的数阵中, 数K(1〈=K〈=N)在每行和每列中出现且仅
  出现一次,这样的数阵叫N阶拉丁方阵。例如下图就是一个五阶拉丁方阵。
  编一程序,从键盘输入N值后,打印出所有不同的N阶拉丁方阵,并统计个数。

        1  2  3  4  5
        2  3  4  5  1
        3  4  5  1  2
        4  5  1  2  3
        5  1  2  3  4
[code=c]
方法1:
#include<stdio.h>
#include<conio.h>
int main(void)
{
 int a,n,i,j,k;
 printf("please enter N:");
 scanf("%d",&n);
 int b[2*n];
 for(i=0;i<2;i++)
  for(j=0;j<n;j++)
   b[i*n+j]=j+1;
 for(i=0,k=0;i<n;i++,k++)
 {
  for(j=0;j<n;j++)
   printf("  %d",b[j+k]);
  printf("\n");
 }
 getch();
}

方法2:
#include<stdio.h>
#include<conio.h>
int main(void)
{
 int n,i,j;
 printf("please enter N:");
 scanf("%d",&n);
 int a[n][n];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   {
    if(i+j+1<=n)
     a[i][j]=i+j+1;
    else
     a[i][j]=i+j+1-n;    
   }
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",a[i][j]);
  printf("\n");
 }
 getch();
}

[/code][/quote]
题目是要求打印出所有的拉丁方阵哦 你这只是其中一种

20 楼

抽点空再写一题
6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:

   ① 倒填,例如N=5             ② 蛇形填数              ③ 回转填数

 ┌─┬─┬─┬─┬─┐   ┌─┬─┬─┬─┬─┐   ┌─┬─┬─┬─┬─┐
 │25│24│23│22│21│   │ 1│ 3│ 4│10│11│   │ 1│16│15│14│13│
 ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
 │20│19│18│17│16│   │ 2│ 5│ 9│12│19│   │ 2│17│24│23│12│
 ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
 │15│14│13│12│11│   │ 6│ 8│13│18│20│   │ 3│18│25│22│11│
 ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
 │10│ 9│ 8│ 7│ 6│   │ 7│14│17│21│24│   │ 4│19│20│21│10│
 ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤   ├─┼─┼─┼─┼─┤
 │ 5│ 4│ 3│ 2│ 1│   │15│16│22│23│25│   │ 5│ 6│ 7│ 8│ 9│
 └─┴─┴─┴─┴─┘   └─┴─┴─┴─┴─┘   └─┴─┴─┴─┴─┘

[code=c]
#include <stdio.h>

#define N    8

int main()
{
    int i = 0, j = 0;
    long int temp;
    long int a[N][N] = {0};
    
/*--------class 1 倒填---------*/    
/*    
    temp = N*N;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            a[i][j] = temp--;
        }
    }
*/    

/*--------class 2 蛇形---------*/
/*  int dir = 0;    //对于上三角,dir为0时,i值加一,dir为1时,j值加一
                    //对于下三角,dir为1时,i值加一,dir为0时,j值加一
    int t;    
    temp = 1;
    a[i][j] = temp++;

    //上三角
    for (; temp <= (N*N + N) / 2;)
    {
        if (dir == 0)
        {
            i++;
            a[i][j] = temp++;
            while (i > 0)
            {
                i--;
                j++;
                a[i][j] = temp++;
            }
            dir = 1;
        }
        else
        {
            j++;
            a[i][j] = temp++;
            while (j > 0)
            {
                j--;
                i++;
                a[i][j] = temp++;
            }
            dir = 0;
        }
    }

    //下三角
    for (; (temp <= N*N) && (temp >= (N*N + N) / 2);)
    {
        if (dir == 0)
        {
            j++;
            t = j;
            a[i][j] = temp++;
            while (i > t)
            {
                i--;
                j++;
                a[i][j] = temp++;
            }
            dir = 1;
        }
        else
        {
            i++;
            t = i;
            a[i][j] = temp++;
            while (j > t)
            {
                j--;
                i++;
                a[i][j] = temp++;
            }
            dir = 0;
        }
    }
*/

/*--------class 3 回转---------*/
/*
    temp = 1;
    for (i = 0; i < N / 2 + 1; i++)
    {
        for (j = i; j < N - i; j++)
        {
            a[j][i] = temp++;
        }
        for (j = i + 1; j < N - i; j++)
        {
            a[N - i - 1][j] = temp++;
        }
        for (j = N - i - 2; j >= i; j--)
        {
            a[j][N - i - 1] = temp++;
        }
        for (j = N - i - 2; j > i; j--)
        {
            a[i][j] = temp++;
        }
    }
*/

/*--------output----------*/
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            printf("%-5ld",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
[/code]

我来回复

您尚未登录,请登录后再回复。点此登录或注册