閱讀405 返回首頁    go 阿裏雲 go 技術社區[雲棲]


第五屆全國信息水平設計大賽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;
}



最後更新:2017-04-02 15:15:17

  上一篇:go ACM STL容器和算法
  下一篇:go 分析稱外圍設備將成提高Win 8體驗重要因素