首页 > 解决方案 > 如何在 c 编程中添加字符串?

问题描述

最重要的是,该程序将获得一些学生和每个学生绘制的数字(卡片)。例如,输入将是 2 行:

5
0 1 1 3 2

然后输出将是:

4 2 5 3 1 

有 5 名学生被叫1,2,3,4,5.(5 名学生存在于“A”室,依次进入“B”室)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i;
    printf("How many students??\n");
    scanf("%d", &n);
    int a[n], b[n];
    char c[100];
    char e[100];
    char f[100];

    for (i = 0; i < n; i++) {
        b[i] = i + 1;
    }
    if (n > 100 || n <= 0) {
        // printf("Students number is incorrect\n");
        main();
    }
    else {
        //printf("Write down number card each student get\n");
        for (i = 0; i < n; i++) {
            scanf("%d", a[i]);
        }

        if (a[1] == 1) { //number 2 students enter
            **c = "2 1";
            **
        }
        else {
            c = "1 2";
        }

        if (a[2] == 2) //number 3 student enter
        {
            d = "3 " + c;
        }
        else if (a[2] == 1) {
            d = c[0] + " 3 " + c[2];
        }
        else {
            d = c[0] + " " + c[2] + " 3";
        }
        if (a[3] == 3) // number 3 student enter
        {
            e = "4 " + d;
        }
        else if (a[3] == 2) {
            e = d[0] + " 4" + d[2] + d[3] + d[4]
        }
        else if (a[3] == 1) {
            e = d[0] + " " + d[2] + " 4" + " " + d[4];
        }
        else {
            e = d[0] + " " + d[2] + " " + d[4] + " 4"
        }
        if (a[4] == 4) {
            f = "5" + e;
        }
        if (a[4] == 3) {
            f = e[0] + " 5" + e[2] + " " + e[4] + " " + e[6];
        }
        else if (a[4] == 2) {
            f = e[0] + " " + e[2] + " 5" + " " + e[4] + " " + e[6];
        }
        else if (a[4] == 1) {
            f = e[0] + " " + e[2] + " " + e[4] + " 5" + " " + e[6];
        }
        else {
            f = e[0] + " " + e[2] + " " + e[4] + " " + e[6] + " 5";
        }

        printf("%s", f);
    }

    return 0;
}

标签: csorting

解决方案


推荐阅读