第五届全国信息水平设计大赛C语言复赛B卷答案

#include <stdio.h>
#include <conio.h>

/*
1、	编程解决如下问题:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。
   百钱买百鸡,问鸡翁,鸡母,鸡雏各几何?(20分)
*/


int main()
{
    int rooster = 0,//公鸡
        hen = 0,    //母鸡
        child = 0;  //鸡雏

    for (rooster = 0; rooster <= 20; rooster++)
    {
        for (rooster = 0; hen <= 33; hen++)
        {
            child = 100 - rooster - hen;
            if (child >= 0 && 100 == (rooster * 5 + hen * 3 + child / 3 * 1))
            {
                printf("鸡翁: %d\t鸡母: %d\t鸡雏: %d\n", rooster, hen, child);
            }
        }
    }
    getch();
    return 0;
}
    int i;
    for (i = 0; i < size; i++)
    {
        if (strcmp(tmp, art[i].word) == 0)
            return i;//返回下标
    }
    return -1;
}

/*
2、	编程实现:有二维数组a[3][3]={{1.3,2.7,3.6},{2,3,4.7},{3,4,1.27}},
将数组a的每一行元素均除以该行上绝对值最大的元素,按行输出新数组。(20分)
*/

#include <stdio.h>
#include <conio.h>

int main()
{
    float a[3][3] = {{1.3, 2.7, 3.6}, {2, 3, 4.7}, {3, 4, 1.27}},
          max;
    int i, j;

    puts("处理前:");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            printf("%6f  ", a[i][j]);
        }
        putchar(10);
    }

    for (i = 0; i < 3; i++)
    {
        max = fabs(a[i][i]);
        for (j = 1; j < 3; j++)
        {
            if (max < fabs(a[i][j]))
                max = a[i][j];
        }
        for (j = 0; j < 3; j ++)
            a[i][j] /= max;
    }

    puts("处理后:");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            printf("%6f  ", a[i][j]);
        }
        putchar(10);
    }


    return 0;
}

/*
3、	编程:设x、y取值为区间[1,10]的整数, f(x,y)=(3x-2y)/(x+y),
求使f(x,y)取最小值的x1、y1,要求使用自定义函数实现f(x,y)功能。(20分)
*/

#include <stdio.h>
#include <conio.h>

double getFx(int x, int y)
{
    return  (3 * x - 2 * y) / (x + y);
}
int main()
{
    double fx = 100000, tmp = 0;
    int x, y;
    int minx, miny;

    for (x = 1, y = 1; x <= 10 && y <= 10; x++, y++)
    {
        tmp = getFx(x, y);
        if (fx - tmp > 1e-7)
        {
            fx = tmp;
            minx = x;
            miny = y;
        }
    }

    printf("%d %d\n", minx, miny);

    return 0;
}

/*
4、	编写函数fun,其功能是:在字符串中所有数字字符
前加一个“*”字符,要求通过指针实现。(20分)
*/

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

int main()
{
    char str[200], *pBegin = str;

    printf("输入一个串:\n");
    gets(pBegin);

    for (; *pBegin != '\0'; pBegin++)
    {
        if (*pBegin >= '1' && *pBegin <= '9')
        {
            putchar('*');
            putchar(*pBegin);
        }
        else
            putchar(*pBegin);
    }
    putchar(10);
    getch();
    return 0;
}

/*
5、	编程:已知学生记录由学号和学习成绩构成,
N名学生的记录已存入结构体数组中,找出成绩最
低的学生,并输出这个学生的信息,已知学生信息如下。(20分)
A01,81;A02,89;A03,66;A04,87;A05,77
A06,90;A07,79;A08,61;A09,80;A10,71

*/

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

typedef struct
{
    char sno[5];
    int score;
}Student;

int main()
{
    Student stu[10] = {{"A01", 81}, {"A02", 89}, {"A03", 66}, {"A04", 87},
                       {"A05", 77}, {"A06", 90}, {"A07", 79}, {"A08", 61},
                       {"A09", 80}, {"A10", 71}};
    int i, min = stu[0].score, index;

    for (i = 1; i < 10; i++)
    {
        if (min > stu[i].score);
        {
            min = stu[i].score;
            index = i;
        }
    }

    printf("成绩最低的学生的信息:\n");
    printf("%-5s %d\n", stu[index].sno, min);  

    getch();
    return 0;
}

/*
5、	编程:已知学生记录由学号和学习成绩构成,
N名学生的记录已存入结构体数组中,找出成绩最
低的学生,并输出这个学生的信息,已知学生信息如下。(20分)
A01,81;A02,89;A03,66;A04,87;A05,77
A06,90;A07,79;A08,61;A09,80;A10,71

*/

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

typedef struct
{
    char sno[5];
    int score;
}Student;

int main()
{
    Student stu[10] = {{"A01", 81}, {"A02", 89}, {"A03", 66}, {"A04", 87},
                       {"A05", 77}, {"A06", 90}, {"A07", 79}, {"A08", 61},
                       {"A09", 80}, {"A10", 71}};
    int i, min = stu[0].score, index;

    for (i = 0; i < 10; i++)
    {
        if (min > stu[i].score)
        {

            min = stu[i].score;
            index = i;
        }
    }

    printf("成绩最低的学生的信息:\n");
    printf("%-5s %d\n", stu[index].sno, stu[index].score);

    getch();
    return 0;
}


/*
6、	附加题:编写一个函数InverseByWord(char *sentence),
实现一个英文句子按单词逆序存放的功能,并给出测试程序。(50分)
如:This is an interesting programme.
逆序后变为:.programme interesting an is This
*/

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

void InverseByWord(char *sentence)
{
    int i, j = strlen(sentence) - 1, wordLen = 0;
    int index = 0, len = j + 1;
    char tmp[201];

    if (sentence[j] == '.')
    {
        tmp[index++] = '.';
        j--;
    }

    while (j >= 0)
    {
        wordLen = 0;
        while (sentence[j] != ' ')
        {
             j --;
             wordLen ++;
        }
        for (i = 0; i < wordLen; i++)
        {
            if (j >= 0)
                 tmp[index++] = sentence[j + i + 1];
        }
        while (sentence[j] == ' ')//去掉空格
        {
             tmp[index++] = sentence[j--];
        }
    }

    strncpy(sentence, tmp, len);
}

int main()
{
    char str[201];
    int i = 0;

    str[0] = ' ';
    printf("输入一个字符串:\n");
    gets(str+1);

    InverseByWord(str);

    printf("逆序后:\n");
    puts(str);

    getch();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值