寫代碼將三個數按從小到大輸出,代碼以下:算法
int main() { int a = 0; int b = 0; int c = 0; scanf("%d%d%d", &a, &b, &c); //算法實現 //a中放最大值 //b次之 //c中放最小值 if (a < b) { int t = a; a = b; b = t; } if (a<c) { int t = a; a = c; c = t; } if (b < c) { int t = b; b = c; c = t; } printf("%d %d %d", a, b, c); return 0; }
打印1到100之間全部3的倍數,代碼以下:ide
int main() { int i = 0; for (i = 1; i < 100; i++) { if (i%3 == 0) { printf("%d\n", i); } } return 0; } 給定2個數求這2個數的最大公約數,代碼以下: int main()//展轉相除法 { int a = 0; int b = 0; int r = 0; scanf("%d%d", &a, &b); while (r = a%b) { //r = a%b; a = b;//a = 15 b = r;//b = 10 } printf("%d\n", b); return 0; } 打印1000到2000年的閏年,代碼以下: int main() { int year = 0; int count = 0; for (year = 1000; year <= 2000; year++) { //判斷year是否爲閏年 //1.能被4整除而且不能被100整除是閏年 //2.能被400整除是閏年 if (year % 4 == 0 && year % 100 != 0) { printf("%d\n", year); count++; } else if (year % 400 == 0) { printf("%d\n", year); count++; } } printf("count = %d\n", count); return 0; }
打印100到200的素數,代碼以下:函數
int main() { int i = 0; int count = 0; //sqrt - 開平方的數學庫函數 for (i = 100; i <= 200; i ++) //for (i = 101; i <= 200; i+=2) { //判斷i是否爲素數 //素數判斷的規則 //1.試除法 //13 2-12 //產生2->i-1 int j = 0; for (j = 2; j <= i; j++) //for ( j = 2; j <= sqrt(i); j++) { if (i%j == 0) { break; } } if (j == i) //if (j >sqrt(i)) { printf("%d\n", i); count++; } } printf("count = %d\n", count); return 0; }
固然上面由於咱們用了sqrt這個庫函數,上面就要引入math.h的頭文件。學習
數一下1到100的全部整數中出現了多少個數字9,代碼以下:code
int main() { int i = 0; int count = 0; for ( i = 1; i <= 100; i++) { if (i % 10 == 9) count++; if (i / 10 == 9) count++; } printf("count = %d", count); return 0; }
計算1/1-1/2+1/3-1/4+1/5......+1/99-1/100的值,打印出結果:遊戲
int main() { int i = 0; double sum = 0; int flag = 1; for ( i = 1; i <= 100; i++) { sum += flag*1.0 / i; flag = -flag; } printf("sum = %lf\n", sum); return 0; }
求10個整數中的最大值,代碼以下:input
int main() { int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int max = arr[0];//最大值 int i = 0; int sz = sizeof(arr) / sizeof(arr[0]); for ( i = 0; i < sz; i++) { if (arr[i]>max) max = arr[i]; } printf("max = %d", max); return 0; }
在屏幕上輸出9*9乘法口訣表,代碼以下:數學
int main() { int a = 0; //肯定打印9行 for ( a = 1; a <= 9; a++) { //打印一行 int b = 1; for ( b = 1; b <= a ; b++) { printf("%d*%d=%-2d ", a, b, a*b); } printf("\n"); } return 0; }
猜數字遊戲,有興趣能夠玩一下,代碼以下:it
void menu() { printf("**************************\n"); printf("**1.開始遊戲 0.退出遊戲**\n"); printf("**************************\n"); } //RAND_MAX= 32767 void game() { //1.生成一個隨機數 int ret = 0; int guess = 0; //拿時間戳來設置隨機數的生成起始點 //time_t time(time_t *timer) ret = rand()%100+1;//生成1到100之間的隨機數 //printf("%d\n",ret); //2.猜數字 while (1) { printf("猜數字>:"); scanf("%d", &guess); if (guess > ret) { printf("猜大了\n"); } else if (guess<ret) { printf("猜小了\n"); } else { printf("恭喜猜對\n"); break; } } } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇>:"); scanf("%d", &input); switch (input) { case 1: game();//猜數字遊戲 break; case 0: printf("退出遊戲\n"); break; default: printf("選擇錯誤\n"); break; } } while (input); return 0; }
固然這裏咱們用到了time,rand,srand等庫函數,有用咱們就要打招呼,因此引用頭文件
#include<time.h>,#include<stdlib.h>,今天的學習到此結束啦。明天繼續努力。加油奧力給
2021.1.20class