python中一些列表知識
阿新 • • 發佈:2022-04-15
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void printText(int line, int col, char text[]); void printSpaces(int n); void printBlankLines(int n); // 函式宣告 int main() { int line, col, i; char text[N] = "hi, May~"; srand(time(0)); // 以當前系統時間作為隨機種子 for (i = 1; i <= 10; ++i) { line = rand() % 25; col = rand() % 80; printText(line, col, text); Sleep(1000); } return 0; } // 列印n個空格 void printSpaces(int n) { int i; for (i = 1; i <= n; ++i) printf(" "); } // 列印n行空白行 void printBlankLines(intn) { int i; for (i = 1; i <= n; ++i) printf("\n"); } // 在第line行第col列列印一段文字 void printText(int line, int col, char text[]) { printBlankLines(line - 1); // 列印n-1行空格 printSpaces(col - 1); // 列印n-1列空格 printf("%s", text); }
程式功能:每間隔1秒在螢幕上打出hi,May~並且每次出現中間隨機間隔0~24行,0~79列
#include <stdio.h> longlong fac(int n); int main() { int i, n; printf("Enter n: "); scanf_s("%d", &n); for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i)); return 0; } long long fac(int n) { static long long p = 1; printf("p = %lld\n", p); p = p * n; return p; }
#include <stdio.h> int func(int, int); int main() { int k = 4, m = 1, p1, p2; p1 = func(k, m); p2 = func(k, m); printf("%d,%d\n", p1, p2); return 0; } int func(int a, int b) { static int m = 0, i = 2; i += m + 1; m = i + a + b; return m; }
區域性static變數的特性:函式退出時,變數始終存在,但不能被其他函式使用,當再次進入該函式時,將儲存上次的結果。
#include <stdio.h> long long fun(int n); int main() { int n; long long f; while (scanf_s("%d", &n) != 0) { f = fun(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long fun(int n) { int s; if (n > 1) s = 2 * fun(n - 1) +1; else if (n == 1) s = 1; return s; }
#include<stdio.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); int count(int n); int main() { unsigned int n; while (scanf_s("%u", &n) != 0) { hanoi(n, 'A', 'B', 'C'); printf("一共移動了%d次\n\n", count(n)); } return 0; } void hanoi(unsigned int n, char from, char temp, char to) { if (n == 1) { moveplate(n, from, to); } else { hanoi(n - 1, from, to, temp); moveplate(n, from, to); hanoi(n - 1, temp, from, to); } } void moveplate(unsigned int n, char from, char to) { printf("第%u個盤子:%c-->%c\n", n, from ,to); } int count(int n) { int i = 1,s; if (n == 1) s = 1; else s = 2 * count(n - 1) + 1; return s; }
#include<stdio.h> #include<math.h> int is_prime(int a); int main() { int n, i; for (n = 4; n <= 20; n += 2) { for (i = 2; i <= n ; i++) { if (is_prime(i) && is_prime(n - i)) { printf("%d=%d+%d\n", n, i, n - i); break; } } } return 0; } int is_prime(int a) { if (a == 2) return 1; else { int i = 0; for (i = 2; i <= sqrt(a); i++) { if (a % i == 0) return 0; } } return 1; }
#include <stdio.h> #include<math.h> long fun(long s); int main() { long s, t; printf("Enter a number: "); while (scanf_s("%ld", &s) != 0) { t = fun(s); printf("new number is: %ld\n\n", t); printf("Enter a number: "); } return 0; } long fun(long s) { long i, j=0,k=0; while (s / 10 != 0||s==1) { i = s % 10; if (i % 2 != 0) { j = i * pow(10,k) + j; k++; } s /= 10; } return j; }