Re: [問題] 九九乘法表不用迴圈是叫我直接從1列到81?

作者: Schottky (順風相送)   2018-07-31 18:43:39
: 推 sarafciel: 平行版XD 不考慮開個特大號char array讓64個一起塞嗎?
: → Schottky: 咦,樓上說的很有道理
這次才是真‧平行版,分配一下使用區域還可以順便做排版
雖說前一篇比較有 OpenMP 當 loop 用的笑點
初版有排版位置 page, row, col, 被乘數 a 和乘數 b 等等變數,比較容易寫,
改成這樣把變數都展開來超難讀,但程式的行數就縮減很多。我故意的。
各位如果只想要整別人不想要整自己可以參考這種兩階段式作法 XDDDD
/* https://ideone.com/9Yp7YX */
#include <stdio.h>
#include <omp.h>
char mt[769];
int main(void)
{
omp_set_num_threads(64);
#pragma omp parallel
{
int n, ofst;
n = omp_get_thread_num();
ofst = n/32*384 + n/8%4*12 + n%8*48;
snprintf(mt+ofst, 12, "%d x %d = %2d",
n/8+2, n%8+2, (n/8+2)*(n%8+2));
mt[ofst+10] = n==31?'\n':' ';
mt[ofst+11] = n/8%4==3?'\n':' ';
}
mt[768] = '\0';
printf("%s\n", mt);
return 0;
}
$ gcc -fopenmp par.c
$ ./a.out
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18
6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27
6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36
6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45
6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54
6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63
6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72
6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
作者: sarafciel (Cattuz)   2018-08-01 19:03:00
推個真·平行版XD

Links booklink

Contact Us: admin [ a t ] ucptt.com