二道c语言题目,二级c语言考试题及答案

发布时间:2023-01-07

本文目录一览:

  1. 2道c语言编程问题
  2. 2道c语言问题求教 !
  3. 两道c语言题
  4. 2道c语言题目
  5. 两道简单的C语言题目

2道c语言编程问题

第一题:

#include iostream
#include stdlib.h
#include stdio.h
#include string.h
using namespace std;
int main()
{
    int num;
    printf("Enter the number of stock value:");
    while(scanf("%d", num) != EOF)
    {
        int res = 0;
        int max = 0;
        if(num == -1)
        {
            return 0;
        }
        if(num == 0)
        {
            printf("Enter the number of stock value:");
            continue;
        }
        int a[num-1];
        for(int i = 0; i < num-1; i++)
        {
            scanf("%d ", a[i]);
        }
        fflush(stdin); //刷新缓冲区
        for(int i = 0; i < num; i++)
        {
            if(max < a[i])
                max = a[i];
            if(i > 0)
            {
                if((a[i] - max) > res)
                    res = a[i] - max;
            }
        }
        printf("%d", res);
        printf("\nEnter the number of stock value:");
    }
    return 0;
}

第二题

#include iostream
#include stdlib.h
#include stdio.h
#include string.h
using namespace std;
void printSelection(int money, int bread, int ice, int coke, int coffee)
{
    if(money >= 0)
    {
        if(money >= 700)
        {
            ice = money / 700;
            money = money % 700;
            printSelection(money, bread, ice, coke, coffee);
            while(ice--)
            {
                if(ice == 0)
                {
                    money = money + 700 - 100;
                    coffee++;
                    printSelection(money, bread, ice, coke, coffee);
                }
            }
        }
        else if(money >= 500)
        {
            bread = money / 500;
            money = money % 500;
            printSelection(money, bread, ice, coke, coffee);
            while(bread--)
            {
                if(bread == 0)
                {
                    money = money + 500 - 100;
                    coffee++;
                    printSelection(money, bread, ice, coke, coffee);
                }
            }
        }
        else if(money >= 400)
        {
            coke = money / 400;
            money = money % 400;
            printSelection(money, bread, ice, coke, coffee);
            while(coke--)
            {
                if(coke == 0)
                {
                    coffee = coffee + 4;
                    printSelection(money, bread, ice, coke, coffee);
                }
            }
        }
        else
        {
            coffee = coffee + money / 100;
            printf("Bread(%d),Icecream(%d),Coke(%d),Coffee(%d).\n", bread + 1, ice + 1, coke + 1, coffee + 1);
        }
    }
}
int main()
{
    int money;
    printf("Enter your money (=2,000):");
    while(scanf("%d", money) != EOF)
    {
        if(money == -1)
        {
            return 0;
        }
        if(money < 2000)
        {
            printf("You must enter more than 2,000.\n");
        }
        else if((money % 100) != 0)
        {
            printf("The monetary unit should be 100 won.\n");
        }
        else
        {
            money = money - (500 * 1 + 700 * 1 + 400 * 1 + 100 * 1);
            printSelection(money, 0, 0, 0, 0);
        }
        printf("Enter your money (=2000):");
    }
    return 0;
}

2道c语言问题求教 !

第一题

第一个循环的时候,把x[i].a(i取0 1 2)分别赋值为1,2,3,x[i].b分别赋值为5,6,7。而x[i].next总是指向下一个数值,最后一个则指向第一个,是一个循环。 第二个循环里输出一个x[0].a,然后输出下一个数x[1].b,然后是下一个x[2].a,然后是x[0].b...以此循环。 为了形象点,我们把x[i].ax[i].b写成两行:

3 4 5 3 4 5
5 6 7 5 6 7

输出就是按这张图的红线依次输出,循环3次,每次输出两个,就是输出6个数,结果是365547。

第二题

是求一个数能不能表示成两个素数相加,pr函数的功能是判断一个数是否是素数,每次循环判断km-k是不是都是素数,都是素数说明该数可以表示成两个素数相加。 输入30,输出就是30=7+23

两道c语言题

第一道:

答案:65535, 0 我们首先要求-1的补码:一个负数的补码,是它对应的正数的补码按位取反并在末位加1。 求-1的补码,先看正1的补码。 正1的二进制数是0000000000000001,它的补码是1111111111111110,然后末位加1,就变成了1111111111111111,这正是65535对应的二进制数(PS:unsigned int型的取值范围是0~65535),因此a输出是65535。 对于b我们知道65536超出了范围,因此装不下的那部分会被截断。

详细解释:

第二道:

答案:123456.123、123.123 首先我们要知道:%5.3f是控制输出格式的:

  • f表示输出浮点数,
  • 5表示最小输出字符宽度为5位数,
  • 3表示浮点数输出小数点后为3位数。 因此答案不难理解了。 有什么不懂得,欢迎追问啊~~~~

2道c语言题目

同学你这是100分的题啊,难怪没人回答。你等等吧,我有空帮你写。

#include "stdio.h"
#include "stdafx.h"
#include iostream
#include iomanip
using namespace std;
float average();
int findfail(int i);
int findgood(int i);
struct student
{
    int no;
    int score[5];
    float vag;
};
student stu[3]; //定义学生人数(本列为3人)
void main()
{
    int i, j;
    cout << "请输入学生学号及其五门成绩!" << endl;
    for(i = 0; i < 3; i++)
    {
        cin >> stu[i].no;
        stu[i].vag = 0;
        for(j = 0; j < 5; j++)
        {
            cin >> stu[i].score[j];
            stu[i].vag += stu[i].score[j];
        }
        stu[i].vag = stu[i].vag / 5;
    }
    cout << "******第一门成绩的平均分******" << endl;
    cout << setprecision(3) << average() << endl << endl << endl;
    cout << "有2门以上(含2门)课程不及格的学生" << endl;
    cout.width(5);
    cout << " 学号 " << "********成绩列********" << " 平均成绩" << endl;
    for(i = 0; i < 3; i++)
        if(findfail(i) != -1)
        {
            cout.width(5);
            cout << stu[i].no;
            for(j = 0; j < 5; j++)
            {
                cout.width(5);
                cout << stu[i].score[j];
            }
            cout.width(10);
            cout << stu[i].vag << endl;
        }
    cout << endl << endl;
    cout << "平均成绩在90分以上或全部课程成绩在85分以上的学生" << endl;
    cout << " 学号 " << endl;
    for(i = 0; i < 3; i++)
        if(findgood(i) != -1)
        {
            cout.width(5);
            cout << stu[i].no << endl;
        }
    cout << endl;
    system("pause");
}
float average()
{
    int i;
    float vag1 = 0;
    for(i = 0; i < 3; i++)
        vag1 += stu[i].score[0];
    vag1 = vag1 / 3;
    return vag1;
}
int findfail(int i)
{
    int j, cl = 0;
    for(j = 0; j < 5; j++)
        if(stu[i].score[j] < 60)
            cl++;
    if(cl >= 2)
        return i;
    else
        return -1;
}
int findgood(int i)
{
    int j, f1 = 0, f2 = 1;
    if(stu[i].vag >= 90)
        f1 = 1;
    for(j = 0; j < 5; j++)
        if(stu[i].score[j] < 85)
            f2 = 0;
    if(f1 || f2)
        return i;
    else
        return -1;
}
#include "stdio.h"
#include "stdafx.h"
#include iostream
#include iomanip
using namespace std;
void multiply();
int a[5][5], b[5][5], c[5][5], m, n;
void main()
{
    int i, j;
    cout << "input m n:" << endl;
    cin >> m >> n;
    cout << "input A(mxn)" << endl;
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            cin >> a[i][j];
    cout << "input B(nxm)" << endl;
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            cin >> b[i][j];
    multiply();
    cout << "C(mxm) is:" << endl;
    for(i = 0; i < m; i++)
    {
        for(j = 0; j < m; j++)
            cout << setw(5) << c[i][j];
        cout << endl;
    }
    system("pause");
}
void multiply()
{
    int i, j, k, sum;
    for(k = 0; k < m; k++)
        for(i = 0; i < m; i++)
        {
            sum = 0;
            for(j = 0; j < n; j++)
                sum += a[k][j] * b[j][i];
            c[k][i] = sum;
        }
}

这是我写好的,不过我是用C写的(我电脑里没有C的环境),你如果有C的话,就可以直接运行了,如果是C的话,就把那些输入输出改一下就行了。比如C里输入用的是cin你就把那一行改成C的scanf,C的输出是用cout,你也是把相应的行改成C的printf就行了。

两道简单的C语言题目

一、首先分析这段函数实现的功能

main()
{
    int op1, op2, res;
    char opertor;
    scanf("%d", op1); // op1为输入的整数
    opertor = getchar(); // opertor为输入的字符
    while(opertor != '=') // 如果输入的字符不是‘=’则继续
    {
        scanf("%d", op2); // op2 为输入的第二个整数
        switch(opertor) // 判断opertor为何种运算,并求出结果,如果opertor不符合4则运算op1=0,重新等待输入运算符,重复以上过程,直到输入‘=’,求出最终结果
        {
            case '+': res = op1 + op2; break;
            case '-': res = op1 - op2; break;
            case '*': res = op1 * op2; break;
            case '/': res = op1 / op2; break;
            default: res = 0;
        }
        op1 = res;
        opertor = getchar();
    }
    printf("%d\n", res);
}

可以看出,这个程序实现的运算是从左到右,而不去分运算优先级,加减和乘除都是平等,从左向右计算即可。

二、分析程序功能

#include stdio.h
#define MAXLEN 80
main()
{
    int k = 0, number = 0;
    char str[MAXLEN];
    while((str[k] = getchar()) != '#') // 读取字符,直到‘#’结束循环
    {
        k++;
        str[k] = '\0'; // 将读取的字符加入到字符数组中,尾部加上‘\0’
        for(k = 0; str[k] != '\0'; k++)
        {
            if(str[k] >= '0' && str[k] <= '9' || str[k] == 'A' || str[k] == 'B') // 12进制数转换为10进制,注意A B大写,小写的不合法
            {
                if(str[k] >= '0' && str[k] <= '9')
                    number = number * 12 + str[k] - '0';
                else if(str[k] == 'A' || str[k] == 'B')
                    number = number * 12 + str[k] - 'A' + 10;
            }
            else
                break; // 字符串中含有不合法的字符,即,不是0——9,A,B,则跳出循环
        }
        printf("%d\n", number);
    }
}

你的程序少打了一些{},我填上去了。你如果会12进制转换为10进制的方法,那么上面的转换过程就很简单了。 程序理解了,题目就很容易了。