回 帖 发 新 帖 刷新版面

主题:[讨论]GCC入门题部分题目解答

以下程序在VC6.0环境下测试通过
若出现程序编译不了,或需要注释的,可以加Q634419082
//////////////////////////////////////////////////////////////////////////////
//
//  1.  给定等式    A B C D E     其中每个字母代表一个数字,且不同数字对应不
//                      D F G     同字母。编程求出这些数字并且打出这个数字的
//          /    +      D F G     算术计算竖式。
//               ───────
//                  X Y Z D E
//////////////////////////////////////////////////////////////////////////////
//
//  (DE+FG+FG)%100 = DE -> FG = 50 
//  Z = (C+D+D+1)%10
//  Y = ((C+D+D+1)/10 + B)%10 && Y != 0 && Z != 0 
//    -> B == 9 && C +D +D +1 > 20 ->  C >= 5 && D >= 5
//  X != A ->X = A+1
//  E = 45 - (A +9 + C +D +E + 5 +0 +X +Y +Z) = 31 - A -C -D -E -X -Y -Z
/////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
void main()
{
    unsigned int a, c, d, e, x, y, z;

    printf("%10s\n", "A B C D E"); 
    printf("%10s\n", "D F G");
    printf("+%9s\n", "D F G");
    printf("%10s\n", "───────");
    printf("%10s\n\n\n", "X Y Z D E");

    for(a = 1; a < 9; a++)
    {
        if(a == 5)
            continue;
        for(c = 5; c < 9; c++)
        {
            if(c == a )
                continue;
            for(d = 5; d < 9; d++)
            {
                if(d == a || d == c)
                    continue;
                x = a +1;
                y = ((c +2*d +1)/10 + 9)%10;
                z = (c +2*d +1)%10;
                e = 31-a-c-d-x-y-z;
                if(x != a && y != a && z != a && e != a
                    && x != 9 && y != 9 && z != 9 && e != 9
                    && x != c && y != c && z != c && e != c
                    && x != d && y != d && z != d && e != d
                    && x != 5 && y != 5 && z != 5 && e != 5
                    && x != 0 && y != 0 && z != 0 && e != 0                    
                    && x != y && x != z && x != e && y != z
                    && y != e && z != e
                    && 10000*a +9000   +100*c +10*d +e +2*(100*d +50)
                    == 10000*x +1000*y +100*z +10*d +e)
                {
                    printf("%2d%2d%2d%2d%2d\n", a, 9, c, d, e);
                    printf("%*d%2d%2d\n", 6, d, 5, 0);
                    printf("+%*d%2d%2d\n", 5, d, 5, 0);
                    printf("%10s------------\n", "------------");
                    printf("%2d%2d%2d%2d%2d\n", x, y, z, d, e);
                }
            }
        }
    }
}

回复列表 (共144个回复)

91 楼

好啊 


呵呵  


上见识了啊

92 楼

厉害呀,对我来说,你就是神仙了.

93 楼


[em5]真乃;神人也..

94 楼

太棒了!!!

95 楼

感谢楼主提供!!!

96 楼

[quote]//////////////////////////////////////////////////////////////////////////////
//
//  5. 输入一个十进数,将其转换成 N 进制数(0<N<=16)。  
//////////////////////////////////////////////////////////////////////////////
//只能是整数型

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void transform( int ,int );

int main( int argc, char *argv[] )
{
    int number, n;
    do
    {
        printf("input a number and transformed system n( 2 =< n <= 16 ):");
        scanf("%d %d", &number, &n );
    }while( n<2 || n>16 );
    transform( number, n );
    system("pause");
    return 0;
}


void transform( int number, int n )
{
    char result[100];        //用数组表示一个数(可表示到100位)
    int i = 0;

    while( number )
    {
        if( (number%n)<=9 )
            result[i++] = (number%n)+48;    //得到的数存在数组中(倒序)
        else
            result[i++] = (number%n)-10+65;    //得到A,B,C等>=10的数(11~16进制用到)
        number = number/n;
    }
    result[i] = '\0';

    printf("the number %d transformed to %d system is:\n",number, n);

    for( i=strlen(result); i>=0; i-- )    //倒序打印得到顺序的数
        printf("%c",result[i]);

    printf("\n");
}[/quote]
看我的:
十进制整数转换成任意正/负进制:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
long num[4000],index=0;
void modfu(long m,long n)
{
  long mo,m1;
while(m!=0)
 {
  if(m<0&&m%n!=0)
   {

   if(abs(n)<abs(m))
    { m1=m;
      m=m/n+1;
      mo=m1-m*n;
     }
   else
   {
     mo=m-n*1;
     m=1;
   }
  }
  else if(m%n==0||m>0)
  {
   mo=m%n;
   m=m/n;
  }
  num[index++]=mo;
 }
}
/*
void modzen(long m,long n)
{
 long m1,mo;
 while(m!=0)
 {
  if(m%n!=0)
  {
  if(abs(n)<abs(m))
  {
   m1=m;
   m=m/n-1;
   mo=m1-m*n;
   }
   else if(abs(n)>abs(m))
   {
    mo=m+n*1;
    m=-1;
    }
  }
 else
 {
  mo=m%n;
  m=m/n;
  }
  num[index++]=mo;
 }
} */


int main()
{
  long m,n,i,j;
  scanf("%ld %ld",&m,&n);
  if(n<0)
   modfu(m,n);
  else
     if(m>0)

       {
    while(m!=0)
    {num[index++]=m%n;
     m/=n;}
     }
      else
      {
       printf("Input wrong!!!");
      }
  for(i=index-1;i>=0;i--)
  {

      if(num[i]>9)
          printf("%c",'A'+(num[i]-10));
      else
          printf("%ld",num[i]);

  }
  return 0;
  //system("pause");

}

97 楼


好多都看不懂呀,怎么办呀,不理解每一句的意思,格式也不理解要怎么加强呀

98 楼

[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
//////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#define  N  4

static int a[N][N], count;

bool legal(int row, int col)
{
    int i;
    for(i = 0; i < row; i++)
    {
        if(a[i][col] == a[row][col])
            return false;
    }
    for(i = 0; i < col; i++)
    {
        if(a[row][i] == a[row][col])
            return false;
    }
    return true;
}

void trial(int row, int col, FILE *fp)
{
    int i, j;
    if(row == N)
    {        
        printf("*********** %d **********\n", ++count);
        fprintf(fp, "*********** %d **********\n", count);
        for(i = 0; i < N; i++)
        {
            for(j = 0; j < N; j++)
            {
                printf("%5d", a[i][j]);
                fprintf(fp, "%5d", a[i][j]);
            }
            putchar(10);
            fputc(10, fp);
        }
    }
    for(i = 1; i <= N; ++i)
    {
        a[row][col] = i;
        if(legal(row, col))
            trial((row*N+col+1)/N, (row*N+col+1)%N, fp);
    }
}

void main()
{
    FILE *fp = fopen("latin.txt", "w");
    trial(0, 0, fp);
}[/quote]
不用这么麻烦吧!
请看:
 #include <stdio.h>
                                                                                
        int main()
        {
                int i,j,k;
                int N ;
                                                                                
                scanf("%d", &N);
                                                                                
                for(i=1 ; i<= N; i++)
                {
                for(k=1,j=i ;k<=N;k++,j++)
                {
                        if(j>N)
                        printf("%d ",j%N);
                        else
                        printf("%d ",j);
                }
                printf("\n");
                }
                                                                                
                return 0;
        }
测试如下:
[vcacm@localhost gcc]$ ./la
5
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
[vcacm@localhost gcc]$ ./la
10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
4 5 6 7 8 9 10 1 2 3
5 6 7 8 9 10 1 2 3 4
6 7 8 9 10 1 2 3 4 5
7 8 9 10 1 2 3 4 5 6
8 9 10 1 2 3 4 5 6 7
9 10 1 2 3 4 5 6 7 8
10 1 2 3 4 5 6 7 8 9
[vcacm@localhost gcc]$ vi lajie.c
[vcacm@localhost gcc]$

99 楼


//第一题代码
// gcc1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>


int f=5,g=0,a,b,c,d,e,x,y,z=0;
void Search(int n)
{
  for(c=1;c<10;c++)
    {
    if(c==5) continue;
        for(d=1;d<10;d++)
        {
            if(d==5) continue;
            for(e=1;e<10;e++)
            {
                if(e==5) continue;
                for(z=1;z<10;z++)
                {
                    if(z==5) continue;

             if(c+2*d+1==10*n+z)
             if(a!=b&&a!=c&&a!=d&&a!=e&&a!=x&&a!=y&&a!=z)
             if(b!=c&&b!=d&&b!=e&&b!=x&&b!=y&&b!=z)
             if(c!=d&&c!=e&&c!=x&&c!=y&&c!=z)
             if(d!=e&&d!=x&&d!=y&&d!=z)
             if(e!=x&&e!=y&&e!=z)
             if(x!=y&&x!=z)
             if(y!=z)
             if(a+b+c+d+e+x+y+z==40)

             {
                 printf("   %d  %d  %d  %d  %d\n",a,b,c,d,e);
                 printf("         %d  %d  %d\n",d,f,g);
                 printf("+        %d  %d  %d\n",d,f,g);
                 printf("_____________________\n");
                 printf("   %d  %d  %d  %d  %d\n",x,y,z,d,e);
                 printf("\n");
                 printf("\n");
              }
                  }
            }
        }
    }
}


int main()
{
    for(a=1;a<10;a++)
    {
        if(a==4||a==5) continue;

        for(x=1;x<10;x++)
        {
            if(a+1!=x) continue;
            if(x==1||x==5||x==6) continue;
            for(y=1;y<10;y++)
            {
                if(y==5) continue;
                for(b=1;b<10;b++)
                {
                    if(b==5) continue;
               if(y+10-b==1)
                    {
                    Search(1);    
                    }
               else if(y-b+10==2)
                    {
                    Search(2);    
                    }
               else continue;
                }
            }
        }
    }
    getchar();
    return 0;
}

100 楼

to vcacm :请注意题目中的“统计个数”字样

我来回复

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