回 帖 发 新 帖 刷新版面

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

51 楼

48楼 的 g==0  等于是自己先算出来了,第一眼看时,g可能等于0或5。
我的a+1==x  和你的效果一样,没它不行,附加条件。
而且这个是一眼就看出来了,不用判断。

52 楼


[em2]顶一下,本人服了

53 楼

得了吧你,我不吃这套
你要钱要得好凶

54 楼


是你自己说的给钱怪我吗[em2]

55 楼

/*16. 设有8枚硬币a,b,c,d,e,f,g,h,其中有一枚硬币是伪造的。
 真伪硬币的区别仅是重量不同,可能重,可能轻。今要求以天平为工具,用最少的
 比较次数挑出伪造硬币,并鉴定它是重还是轻。*/
#include<iostream.h>
struct{
   int data;
}a,b,c,d,e,f,g,h;

56 楼

void main()
{
    cout<<"input the weight of coins a,b,c,d,e,f,g,h (one different)"<<endl;
    cin>>a.data>>b.data>>c.data>>d.data>>e.data>>f.data>>g.data>>h.data;
    cout<<endl<<"first compare a,b and c,d"<<endl;
    if((a.data+b.data)==(c.data+d.data))
    {
        cout<<"      a,b,c,d is right and the different one is in e,f,g,h "<<endl;
        cout<<"second compare a,b,c and e,f,g"<<endl;
        if((a.data+b.data+c.data)==(e.data+f.data+g.data))
        {
            cout<<"      a,b,c,d,e,f,g is right and the different one is h"<<endl;      
            cout<<"third compare a and h"<<endl;
            if(a.data<h.data)
                cout<<"      h is different and is heavier"<<endl;
            else
                cout<<"      h is different and is lighter"<<endl;
        }
        if((a.data+b.data+c.data)<(e.data+f.data+g.data))
        {
            cout<<"      the different one is in e f g and is heaver"<<endl;
            cout<<"third compare e and f"<<endl;
            if(e.data==f.data)
                    cout<<"      the different one is g and is heaver"<<endl;
            if(e.data<f.data)
                    cout<<"      the different one is f and is heaver"<<endl;
            if(e.data>f.data)
                    cout<<"      the different one is e and is heaver"<<endl;
        }
       if((a.data+b.data+c.data)>(e.data+f.data+g.data))
        {
            cout<<"      the different one is in e f g and is lighter"<<endl;
            cout<<"third compare e and f"<<endl;
            if(e.data==f.data)
                    cout<<"      the different one is g and is lighter"<<endl;
            if(e.data<f.data)
                    cout<<"      the different one is e and is lighter"<<endl;
            if(e.data>f.data)
                    cout<<"      the different one is f and is lighter"<<endl;
        }
    }

57 楼

if((a.data+b.data)<(c.data+d.data))
    {
        cout<<"      e,f,g,h is right and the different one is in a,b,c,d"<<endl;
        
        cout<<"second compare a,b and e f"<<endl;
        
        if((a.data+b.data)==(e.data+f.data))
        {
            cout<<"      a,b,e,f,g,h is right and the different one is in c,d"<<endl;
            cout<<"third compare c and d"<<endl;
            if(c.data<d.data)
                cout<<"      d is different and is heaver"<<endl;
            else 
                cout<<"      c is different and is heaver"<<endl;
        }
        if((a.data+b.data)<(e.data+f.data))
        {
          cout<<"      c,d,e,f,g,h is right and the different one is in a,b"<<endl;
          cout<<"third compare a and b"<<endl;
          if(a.data<b.data)
              cout<<"      a is different and is lighter"<<endl;
          else 
              cout<<"      b is different and is lighter"<<endl;
        }
    }
    if((a.data+b.data)>(c.data+d.data))
    {
        cout<<"      e,f,g,h is right and the different one is in a,b,c,d"<<endl;
        
        cout<<"second compare a,b and e f"<<endl;
        
        if((a.data+b.data)==(e.data+f.data))
        {
            cout<<"      a,b,e,f,g,h is right and the different one is in c,d"<<endl;
            cout<<"third compare c and d"<<endl;
            if(c.data<d.data)
                cout<<"      c is different and is lighter"<<endl;
            else 
                cout<<"      d is different and is lighter"<<endl;
        }
    if((a.data+b.data)>(e.data+f.data))
        {
          cout<<"      c,d,e,f,g,h is right and the different one is in a,b"<<endl;
          cout<<"third compare a and b"<<endl;
          if(a.data<b.data)
              cout<<"      b is different and is heaver"<<endl;
          else 
              cout<<"      a is different and is heaver"<<endl;
        }
    }
}

58 楼


帮忙看看我的对不对呀,我只能想到三次了,不对不准笑

59 楼

这个题目用脑袋想出来就可以了

60 楼

////////////////////////////////////////////////////////////////////////////////
//
// 65. ( NOI'94.1_1 ) 键盘输入一个仅由小写字母组成的字符串,输出以该串中任
//     取M个字母的所有排列及排列总数。
////////////////////////////////////////////////////////////////////////////////
//与48题类似
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define MAX_LEN  80
#define M  6

typedef struct elem
{
    char ch;
    int  n;
}elem;
static int count;

void trial(int n, int array[], elem sub[], int size)
{
    if(n == M)
    {
        printf("\n%d\n", ++count);
        for(n = 0; n < M; n++)
            printf("%c", sub[array[n]].ch);
    }
    else
    {
        for(int i = 0; i < size; i++)
        {
            array[n] = i;
            --sub[i].n;
            if(sub[i].n >= 0)
                trial(n+1, array, sub, size);
            ++sub[i].n;
        }
    }
}

void main()
{
    char str[MAX_LEN+1], ch;
    int i, j, k = 0, array[M] = {0};
    elem sub[M];
    memset(str, 0, MAX_LEN+1), memset(sub, 0, M*sizeof(elem));

    srand((unsigned)time(NULL));
    for(i = 0; i < MAX_LEN; i++)
        str[i] = rand()%26 + 'a';
    printf("%s\n", str);

    for(i = 0; i < M; i++)
    {
        printf("%c\t", ch = str[rand()%MAX_LEN]);
        for(j = 0; j < k; j++)
            if(sub[j].ch == ch)
            {
                sub[j].n++;
                break;
            }
        if(j == k)
        {
            sub[k].ch = ch;
            sub[k++].n  = 1;
        }
    }

    for(i = 0; i < k; i++)
        printf("%c  %d\n", sub[i].ch, sub[i].n);        
    trial(0, array, sub, k);
}

////////////////////////////////////////////////////////////////////////////////
//
// 67. ( NOI'94.1_3 ) 一个实数数列共有N项,已知a(i)=(a(i-1)-a(i+1))/2+d,
//(1〈i〈N)(N<60) , 键盘输入N,d,a(1),a(n),m,输出 a(m)。
////////////////////////////////////////////////////////////////////////////////
//迭代,解一元一次方程
#include "stdio.h"
#define N   7
#define D   3.1

typedef struct elem
{
    float   con;
    int   coe;
}elem;

void main()
{
    elem a[N+1];
    float an, p;
    int i;
    printf("input a1 and an: ");
    scanf("%f %f", &a[1].con, &an);
    a[1].coe = 0, a[2].con = 0, a[2].coe = 1;
    for(i = 3; i <= N; i++)
    {
        a[i].con = a[i-2].con - 2*a[i-1].con + 2*D;
        a[i].coe = a[i-2].coe - 2*a[i-1].coe;
    }
    p = (an - a[N].con)/a[N].coe;
    for(i = 1; i <= N; i++)
    {
        a[i].con += p*a[i].coe;
        printf("%f\t", a[i].con);
    }
}

我来回复

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