あみだくじ
Posted feedbacks - C
元の A B C D E をコピーして、それを '-' がでるたびスワップ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int col;
int line;
char *amida[] = {
"A B C D E",
"| | |-| |",
"|-| | |-|",
"| |-| |-|",
"|-| |-| |",
"|-| | | |",
};
char *answer;
int end_line = sizeof(amida) / sizeof(amida[0]);
int end_col = strlen(amida[0]);
/* 元の値をコピー */
answer = malloc(sizeof(char) * (end_col+1));
strcpy(answer, amida[0]);
/* あみだをたどる */
for (line = 1; line < end_line; line++) {
for (col = 1; col < end_col; col += 2) {
if (amida[line][col] == '-') {
/* swap */
int tmp = answer[col-1];
answer[col-1] = answer[col+1];
answer[col+1] = tmp;
}
}
}
/* 出力 */
for (line = 0; line < end_line; line++) {
printf("%s\n", amida[line]);
}
printf("%s\n", answer); return 0;
}
|
色々問題があると思いますが、初投稿なんでお許しを。
実行結果
./a.out 3 5 2 4 0 1
0 1 2 3 4 5
| |-| | | |
|-| |-| |-|
| |-| |-| |
|-| |-| |-|
| |-| |-| |
3 5 2 4 0 1
実行結果
./a.out 3 5 2 4 0 1
0 1 2 3 4 5
| |-| | | |
|-| |-| |-|
| |-| |-| |
|-| |-| |-|
| |-| |-| |
3 5 2 4 0 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <stdio.h>
#include <stdlib.h>
void amida(int data[], int num);
int main (int argc, char *argv[])
{
int data[argc-1];
int i;
if(argc < 3){
printf("error!! : Too few input data!\n");
}
else{
for(i=0; i<argc-1; i++){
data[i] = atoi(argv[i+1]);
printf("%d ",i);
}
puts("");
amida(data,argc-1);
for(i=0; i<argc-1; i++){
printf("%s ", argv[i+1]);
}
}
return 0;
}
void amida(int data[], int num){
int i, j, temp;
int max=num, level=0, hyflag=0;
char hyphen[max][num];
for(i=0; i<max; i++){
for(j=0; j<num; j++){
hyphen[i][j] = ' ';
}
if(i%2 == 0){
for(j=0; j<num; j+=2){
if(data[j] > data[j+1]){
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
hyphen[level][j] = '-';
hyflag = 1;
}
}
}
else{
for(j=1; j<num-1; j+=2){
if(data[j] > data[j+1]){
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
hyphen[level][j] = '-';
hyflag = 1;
}
}
}
if(hyflag != 0){
level++;
hyflag = 0;
}
}
for(level--; level>=0; level--){
for(i=0; i<num-1; i++){
printf("|%c",hyphen[level][i]);
}
puts("|");
}
}
|


greentea #4476() Rating4/6=0.67
[ reply ]