| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1192 人关注过本帖
标题:大佬们新人求助
只看楼主 加入收藏
miop
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-8
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:0 
大佬们新人求助
#include<stdio.h>
int main()
{
    int i,x,a,b,c,d,e,f,g,k,l,w,q,t,s,o,z;
    int n;
    scanf("%d",&n);
    int r[n][n];
    for(o=0;o<n;o++){
        for(z=0;z<n;z++){
            r[o][z]=0;
        }
    }
    a=(n-1)/2;
    b=a;
    i=0;
    r[a][b]=1;
    for(x=2,c=1,g=0;g<2*n;g++,i++){
        if(i%2==0){
            for(f=1;f<=c;f++){
                b=b+1;
                r[a][b]=x;
                x++;
            }
            for(e=1;e<=c;e++){
                a=a-1;
                r[a][b]=x;
                x++;
            }
            c++;
        }else{
            for(f=1;f<=c;f++){
                b=b-1;
                r[a][b]=x;
                x++;
            }
            for(e=1;e<=c;e++){
                a=a+1;
                r[a][b]=x;
                x++;
            }
            c++;
        }
    }
    k=0;l=0;
    while(k<n){
        if(k==n-1&&l==n-1){
            printf("%d\n",r[k][l]);
            goto end;
        }else{
        printf("%d ",r[k][l]);
    }
        if(l==n-1){
            putchar(10);
            k++;
            l=-1;
        }
        l++;
    }
    end:
    for(w=0,q=0;w<n;w++){
        q=q+r[w][w];
    }
    for(t=0,d=0,s=n-1;t<n;t++,s--){
        d=d+r[t][s];
    }
    printf("%d\n",d+q-1);
    return 0;
}
rt这是一个输出蛇形矩阵的代码 问题出在 我前面一部分是设置数组
后面是从1开始 2 3 蛇形增加 形成类似
5 4 3
6 1 2
7 8 9的结构
但是现在这个代码输出的数据很奇怪 找不到原因
求大佬们指教
搜索更多相关主题的帖子: c++ int printf for 输出 
2021-09-15 22:02
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:10 
online debug: https://www.
user mannual: https://www.

good luck!
2021-09-16 06:25
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

#define POS 0
#define NEG 1
#define CLK_DIR NEG

int main(int argc, char *argv[])
{
    int i, j;
    char r[9];
    void *pr;
#if CLK_DIR == POS
    char pos[9] = {
        0, +1, +4, +3, +2, -1, -4, -3, -2
    };
#elif CLK_DIR == NEG
    char pos[9] = {
        0, +1, -2, -3, -4, -1, +2, +3, +4
    };
#endif
#define CENTER_POS 4
    for(i = 0; i < 9; i++) r[CENTER_POS + pos[i]] = i + 1;
typedef char(*vt)[3];
#define CAST2AE(x, p, v) ((vt)(x))[p][v]
    for(pr = r, i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++)
            printf("%d", CAST2AE(pr, i, j));
#define CRLF puts("")
        CRLF;
    }

    return 0;
}


[此贴子已经被作者于2021-9-16 21:45编辑过]

2021-09-16 21:43
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

#define POS 0
#define NEG 1
#define CLK_DIR NEG
#define MIN_WIDTH 3
#define USR_WIDTH (MIN_WIDTH + 2)
#define USR_W 2 << USR_WIDTH
#define BASE 0
#define UP BASE - (USR_WIDTH + 1)
#define DN BASE + (USR_WIDTH + 1)
#define LF BASE - 1
#define RT BASE + 1

int main(int argc, char *argv[])
{
    int i, j;
    char r[USR_WIDTH * USR_WIDTH] = {0};
    void *pr;
#if CLK_DIR == POS
    char pos[2][USR_W] = {
        {0, RT, DN, DN+LF, DN+LF+LF, LF, UP, UP+RT, UP+RT+RT},
        {0, RT, RT+RT, UP+RT+RT+RT, UP+UP+RT+RT+RT+RT, UP+UP+RT+RT+RT,
        UP+UP+RT+RT, UP+UP+RT, UP+UP, UP+LF, LF+LF, DN+LF+LF+LF,
        DN+DN+LF+LF+LF+LF, DN+DN+LF+LF+LF,DN+DN+LF+LF, DN+DN+LF, 
        DN+DN, DN+RT}
    };
#elif CLK_DIR == NEG
    char pos[2][USR_W] = {
        {0, RT, UP+RT+RT, UP+RT, UP, LF, DN+LF+LF, DN+LF, DN},
        {0, RT, RT+RT, DN+RT, DN+DN, DN+DN+LF, DN+DN+LF+LF, 
        DN+DN+LF+LF+LF, DN+DN+LF+LF+LF+LF, LF+LF+LF, LF+LF, UP+LF,
        UP+UP, UP+UP+RT, UP+UP+RT+RT, UP+UP+RT+RT+RT,
        UP+UP+RT+RT+RT+RT, UP+RT+RT+RT}
    };
#endif
enum loop {INN = 0, OTT};
#define CENTER_POS ((USR_WIDTH * USR_WIDTH) >> 1)
    for(i = 0; i < 9; i++) r[CENTER_POS + pos[INN][i]] = i + 1;
    for(i = 0; i < 18; i++) r[CENTER_POS + pos[OTT][i]] = (i + 1) % 10;

typedef char(*vt)[USR_WIDTH];
#define CAST2AE(x, p, v) ((vt)(x))[p][v]
    for(pr = r, i = 0; i < USR_WIDTH; i++) {
        for(j = 0; j < USR_WIDTH; j++)
            printf("%d", CAST2AE(pr, i, j));
#define CRLF puts("")
        CRLF;
    }

    return 0;
}

output sample:
34567
25438
16123
07894
98765
2021-09-17 11:10
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
//online parser: https://www.bccn.net/run/
to be continued...
程序代码:
#include <stdio.h>

#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"

#define POS 0
#define NEG 1
#define CLK_DIR POS
#define MIN_WIDTH 3
#define USR_WIDTH (MIN_WIDTH + 4)
#define USR_W 1 << USR_WIDTH
#define BASE 0
#define UP BASE - (USR_WIDTH + 1)
#define DN BASE + (USR_WIDTH + 1)
#define LF BASE - 1
#define RT BASE + 1
#define CENTER_POS ((USR_WIDTH * USR_WIDTH) >> 1)
#define MIN_LOOP 9
#define CAST2A(a, v) ((char *)(a))[v]
typedef char(*vp)[32];
typedef char(*vr)[USR_WIDTH];
#define CAST2AP(x, p, v) ((vp)(x))[p][v]
#define CAST2AR(x, p, v) ((vr)(x))[p][v]

int cn_matrix(void *pr)
{
    int i, j;
    
#define LOOP_CNT(_c, _c0) (_c - _c0)
    for(i = 0; i < USR_WIDTH; i++) {
        for(j = 0; j < USR_WIDTH; j++)
            if(CENTER_POS == i * USR_WIDTH + j) {
                j = (int)&CAST2AR(pr, i, 0);
                i = (int)&CAST2A(pr, CENTER_POS);
                printf("loop_cnt = %d\n", i - j);
                return LOOP_CNT(i, j);
            }
    }
}

void re_matrix(void *vr, void *vos, int cnt)
{
    int i, j;
    
enum LOOP_E {INN = 0, OTT};
    for(i = 0, j = MIN_LOOP; i < j; i++)
        CAST2A(vr, CENTER_POS + CAST2AP(vos, INN, i)) = i + 1;
    for(i = 0, j = MIN_LOOP * cnt; i < j; i++)
        CAST2A(vr, CENTER_POS + CAST2AP(vos, OTT, i)) = (i + 1) % 10;
}

void pt_matrix(void *pr)
{
    int i, j;
    
    for(i = 0; i < USR_WIDTH; i++) {
        for(j = 0; j < USR_WIDTH; j++)
            printf("%d", CAST2AR(pr, i, j));
#define CRLF puts("")
        CRLF;
    }
}

int main(int argc, char *argv[])
{
    int i, j, cnt;
    char r[USR_WIDTH * USR_WIDTH] = {0};
    void *pr;
#if CLK_DIR == POS
    char pos[][32] = {
        {0, RT, DN, DN+LF, DN+LF+LF, LF, UP, UP+RT, UP+RT+RT},
        {0, RT, RT+RT, UP+RT+RT+RT, UP+UP+RT+RT+RT+RT, UP+UP+RT+RT+RT,
        UP+UP+RT+RT, UP+UP+RT, UP+UP, UP+LF, LF+LF, DN+LF+LF+LF,
        DN+DN+LF+LF+LF+LF, DN+DN+LF+LF+LF,DN+DN+LF+LF, DN+DN+LF, 
        DN+DN, DN+RT}
    };
#elif CLK_DIR == NEG
    char pos[][32] = {
        {0, RT, UP+RT+RT, UP+RT, UP, LF, DN+LF+LF, DN+LF, DN},
        {0, RT, RT+RT, DN+RT, DN+DN, DN+DN+LF, DN+DN+LF+LF, 
        DN+DN+LF+LF+LF, DN+DN+LF+LF+LF+LF, LF+LF+LF, LF+LF, UP+LF,
        UP+UP, UP+UP+RT, UP+UP+RT+RT, UP+UP+RT+RT+RT,
        UP+UP+RT+RT+RT+RT, UP+RT+RT+RT}
    };
#endif
    re_matrix(r, pos, cn_matrix(r));
    pt_matrix(r);

    return 0;
}
2021-09-17 17:22
快速回复:大佬们新人求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022073 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved