c - 阿尔法数弗洛伊德三角形

标签 c

我想要一个字母数字弗洛伊德三角形,但它显示一些错误..也许是逻辑错误..

我使用的代码是

#include<stdio.h>

int main(){
    int i, j;
    char a[11]="ABCDEFGHIJ";
    int n[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    int index = 0;

    for(i=0; i<5; i++){
        for(j=0; j<i; j++){
            if(i%2==0)
                printf(" %d ",a[index]);
            else
                printf(" %d ",n[index]);
            index++;
        }
        printf("\n");
    }
    return 0;
}

但是我得到的输出是:

1
66 67
4 5 6
71 72 73 74

我想要的输出是这样的:

1
B C
4 5 6
G H I J

让我知道我的不足在哪里..

最佳答案

改变

            printf(" %d ",a[index]);

            printf(" %c ",a[index]);

这会将 a[index] 格式化为字符而不是 ASCII代码。

顺便说一句,您实际上并不需要数组。您可以简单地根据索引计算字母和数字:

        if(i%2==0)
            printf(" %c ", 'A' + index);
        else
            printf(" %d ", index + 1);

关于c - 阿尔法数弗洛伊德三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207217/

相关文章:

c++ - 2 昏暗数组和双指针

c - 重新分配无效的旧大小

c - SPOJ : The day of the competitors

c - 使用 splice 系统调用没有输出

c - 为什么在结构数组中使用字符元素会出现运行时错误

c - Pebble 应用程序中的静态函数与非静态函数

c - 如何将 256 色转换为最接近匹配的 RGB 颜色?

C 管道 : Parent to read from child before child ends

c - 获取ARP数据包的大小

c - 从 PCap 文件中读取并在 c 中打印出 IP 地址和端口号,但我的结果似乎是错误的