gpt4 book ai didi

c - 将数组中的 2 个连续整数计算为字符

转载 作者:行者123 更新时间:2023-11-30 16:35:23 24 4
gpt4 key购买 nike

我希望程序做的是计算一组数组中的两个连续整数,然后将这些整数转换为相应的字符。

例如,如果我有 array[10] = {4 2 3 2 5 3 5 3 6 3},则前 2 个整数 4 2 将转换为“H”,3 2 = E, 5 3 = L 等等,直到打印出 HELLO 一词。该程序应该接受整数列表。

这就是我到目前为止所做的..

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

int main() {
int i=0, j=0, k=0;
int array[1000];
char space;

printf("Input integers to convert into a string: ");
do {
scanf("%d%c", &array[i], &space);
i++;
} while(space != '\n');


for(/*what should I include here?*/)
if (array[0] == 2 && array[1] == 1)
printf("A");
else if (array[0] == 2 && array[1] == 2)
printf("B");
/* and so may else ifs*/


}

最佳答案

像这样的东西应该可以工作。与其一次执行一个 int 的 for 循环,不如一次执行两个 int 。

for(int i = 0; i < 1000; i += 2){
if (array[i] == 2 && array[i + 1] == 1)
printf("A");
else if (array[i] == 2 && array[i + 1] == 2)
printf("B");
/* and so may else ifs*/
}

关于c - 将数组中的 2 个连续整数计算为字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com