Basic exercise letter graphics
Basic exercise letter graphics time limit: 1.0 s memory limit: 256.0 MB Problem description
Using letters to make up some beautiful images, the following is an example:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
This is a graph with 5 rows and 7 columns. Please find out the pattern and output a graph with n rows and m columns.
Enter a row in the input format, which contains two integers n and m, indicating the number of rows in the graph you want to output. The output format outputs n rows, each of which is m characters for your graphics. Sample input 5 7 sample output ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC data scale and conventions 1 <= n, m <= 26. By observing the question, I thought it was a string loop, and thought it was the only thing that these letters changed their positions. Error Code
#include
#include
char a[30][30];int main(){int n,m;int i,j,k;while(scanf("%d%d",&n,&m)!=EOF){memset(a,0,sizeof(a));for(i=1;i<=m;i++) a[1][i]='A'+i-1;for(i=2;i<=n;i++){if(i%m==0){for(j=m,k=1;j>=1;j--,k++) a[i][k]=a[1][j];}else{ for(j=i%m,k=1;j>1;j--,k++) a[i][k]=a[1][j]; for(j=1;k<=m;k++,j++) a[i][k]=a[1][j];}}for(i=1;i<=n;i++){for(j=1;j<=m;j++) printf("%c",a[i][j]);printf("\n");}}return 0;}
Example of incorrect output: 6 5 incorrect output result abcde babcd cbabc dcbab edcba abcde correct output result abcde babcd cbabc dcbab edcba fedcb // correct output should be correct code www.bkjia.com
# Include
Char a [30] [30]; int main () {int n, m; int I, j, k, l; while (scanf ("% d ", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) {k = j-I; if (k <0) k =-k; a [I] [j] = 'A' + k; // The letter serial number is related to the absolute value of the difference between two coordinates .}} For (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) printf ("% c ", a [I] [j]); printf ("\ n") ;}} return 0 ;}