c - 如何在 c 编程中添加字符串?
问题描述
最重要的是,该程序将获得一些学生和每个学生绘制的数字(卡片)。例如,输入将是 2 行:
5
0 1 1 3 2
然后输出将是:
4 2 5 3 1
有 5 名学生被叫1,2,3,4,5.
(5 名学生存在于“A”室,依次进入“B”室)
首先:1 号学生进入房间,什么也不做。
其次:2号学生可以减去数字0、1。如果他画1号,线序将是
2 1
。否则,行顺序将是1 2
. (学生之间有一个空格)表示他选择的数字是他可以前进的人数。第三,3号学生可以减去0,1,2号(假设2号学生选择1),如果3号学生选择2号,则行序为
3 2 1
。这样,为什么我错在c = "2 1";
第27行,有没有更好的方法来解决呢?
#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;
}
解决方案
推荐阅读
- php - 无法使用 Wordpress 编辑器编辑 functions.php,但可以编辑其他文件,例如 CSS 和页面
- java - CentOS7 hadoop wordcount 退出代码为 127
- kotlin - 应用插件请求时发生异常 [id: 'org.openjfx.javafxplugin']
- java - 尝试启动 Netbeans 12
- c - 在 qsort 函数中执行指向字符串的正确方法
- python - 我做的相机不好用,播放器移动速度比相机快,为什么?
- javascript - 如何为 axios baseURL 设置 Nuxt.js 的配置以在全球范围内工作?
- javascript - 如何更改数组状态容器中的对象属性?React Hooks 使用状态
- git - 仅当没有其他提交更新由樱桃挑选的提交更新的文件时,如何进行挑选?
- vue.js - 为什么计算属性在 Vuetify 日期选择器上不能作为 v-model 工作?