回 帖 发 新 帖 刷新版面

主题:[讨论]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个回复)

61 楼

////////////////////////////////////////////////////////////////////////////////
// 
//55. (液晶显示) 下图是用液晶七笔阿拉数字表示的十个数字,我们把横和竖的一
//    个短划都称为一笔,即7有3笔,8有7笔等。请把这十个数字重新排列,要做到
//    两相邻数字都可以由另一个数字加上几笔或减去几笔组成,但不能又加又减。比如
//    7→3是允许的,7→2不允许。编程打印出所有可能的排列。
//    如:4107395682。
////////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "string.h"

int match[10][7] =  
{
    {8,-1},
    {3,4,7,8,9,0,-1},
    {8,-1},
    {8,9,-1},
    {8,9,-1},
    {6,8,9,-1},
    {8,-1},
    {3,8,9,0,-1},
    {-1},
    {8,-1}
};

int res[10] = {0};
bool used[10];

bool legal(int a, int b)
{
    int i;
    for(i = 0; match[a][i] != -1 ; i++)
        if(match[a][i] == b)
            return true;
    for(i = 0; match[b][i] != -1 ; i++)
        if(match[b][i] == a)
            return true;
    return false;
}

void trial(int n)
{
    int i;
    if(n == 10)
    {
        for(putchar(10), i = 0; i < n; i++)
            printf("%5d", res[i]);
    }
    else
    {
        for(i = 0; i < 10; i++)
        {
            if(used[i])
                continue;
            if(legal(res[n-1], i))
            {
                res[n] = i;
                used[i] = true;
                trial(n+1);
            }
            res[n] = 0;
            used[i] = false;
        }
    }
}

void main()
{
    int i;
    for(i = 0; i < 10; i++)
    {
        memset(used, 0, 10);
        res[0] = i;
        used[i] = true;
        trial(1);
    }
}

62 楼


如果输入的表达式是错误的,而需要程序给出提示“您输入的表达式是错误的”那这个程序又是怎么样的?恳请高手调教一下。!!!

63 楼


急急急:如果输入的表达式是错误的,而需要程序给出提示:“您输入的表达式有错误。”,那这个程序又是怎么样子的呢???恳请高手调教一下!!!

64 楼

///////////////////////////////////////////////////////////////////////////
//
// 42. (算术表达式求值) 输入一个由数字、+,-,*,/ 及括号组成的算术表达式,
// 求其值。
///////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "ctype.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
#define MAX_LEN  40

struct
{
    char *base;
    char *top;
}optr;
struct
{
    double *base;
    double *top;
}opnd;
int compare(char a,char b)
{
    if((a == '+') || (a == '-'))
    {
        if((b == '+') || (b == '-') || (b == ')') || (b == '='))
            return 1;
        else 
            return -1;
    }
    else if((a == '*') || (a == '/'))
    {
        if(b == '(')
            return -1;
        else 
            return 1;
    }
    else if(a == '(')
    {
        if(b == ')')
            return 0;
        else 
            return -1;
    }
    else if(a == ')')
        return 1;
    else 
        return (b == '=')?0:-1;
}
int isoperator(char ch)
{
    return ((ch == '=') || (ch == '(') || (ch == ')') || (ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))?1:0;
}

double operate(double a,char b,double c)///calculate the expression
{
    switch (b)
    {
    case '+':
        return c+a;
    case '-':
        return c-a;
    case '*':
        return c*a;
    default:
        return c/a;
    }
}
void main()
{
    char c;
    char temp[10]={'0'};
    int i;
    double a,b;
    
    opnd.top = opnd.base = (double*) malloc(MAX_LEN*sizeof(double));
    optr.base = (char*) malloc(MAX_LEN*sizeof(char));
    *optr.base = '=';
    optr.top = optr.base+1;
    
    c = _getch();    
    while(optr.base != optr.top)
    {
        if(isdigit(c) || c=='.')
        {
            memset(temp, 0, sizeof(temp));
            for(i = 0; isdigit(c) || c=='.'; i++)
            {
                putchar(c);
                temp[i] = c;
                c = _getch();
            }
            *(opnd.top++) = atof(temp);
        }
        else if(isoperator(c))
        {
            switch(compare(*(optr.top-1),c))
//ensure the right order, you can't changed to be "switch(c,compare(*(optr.top-1))) 
//while changing "case -1:" to "case 1:"
            {
            case -1:
                putchar(c);
                *optr.top++ = c;
                c = _getch();
                break;
            case 0:
                putchar(c);
                optr.top--;
                c = _getch();
                break;
            default:
                a = *--opnd.top;
                b = *--opnd.top;
                *opnd.top++ = operate(a, *--optr.top, b);
            }
        }
        else
             c = getch();
    }

    printf("%.4f\n", *(--opnd.top));
}
[color=0000FF]急急急:如果输入的表达式是错误的,而需要程序给出提示:“您输入的表达式有错误。”,那这个程序又是怎么样子的呢???恳请高手调教一下!!![/color]

65 楼

 

66 楼

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
//  2. A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
//  人参加了竞赛:                        //  1为参加,0为没有参加///
//   (1)A参加时,B也参加;                 if(b>=a)  
//   (2)B和C只有一个人参加;               if(b+c==1)
//   (3)C和D或者都参加,或者都不参加;     if(c==d)
//   (4)D和E中至少有一个人参加;           if(d+e>=1)
//   (5)如果E参加,那么A和D也都参加。     if(a>=e)  if(d>=e)
//////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
void main()
{
    int a,b,c,d,e;  //  1为参加,0为没有参加///
    for(a=0;a<2;a++)
        for(b=0;b<2;b++)
            for(c=0;c<2;c++)
                for(d=0;d<2;d++)
                    for(e=0;e<2;e++)
                        if(b>=a)      
                            if(b+c==1)
                                if(c==d)
                                    if(d+e>=1)
                                        if(a>=e)
                                            if(d>=e)
                                                printf("a=%d  b=%d c=%d  d=%d  e=%d\n",a,b,c,d,e);
}////我是一个刚学C++的,我用了  for 和if  把第2题解了,
////在这里我想问一下,我在用  if(b>=a,b+c==1,c==d,d+e>=1,a>=e,d>=e)为什么不可以解出来

    

67 楼

if(b>=a&&b+c==1&&c==d&&d+e>=1&&a>=e&&d>=e)

68 楼

搂主厉害,岂止是入门,简直高手

69 楼

楼主啊!第四道题目明显不对啊!你运行看看结果呢??

70 楼

为什么
有什么问题?
说清楚点

如果你是说运行看不到结果的话,
。。。。。大哥,结果已经保存在文本文件里了

 FILE *fp = fopen("latin.txt", "w")

我来回复

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