写在前面 sizeof是一种关键字,是一个单目运算发,不是函数,但使用方法类似于函数,输出的数据类型为%ld,用法是sizeof(变量名)
,返回的是内存大小(字节数)。
1.判断奇偶数 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> int main () {int number;scanf ("%d" ,&number);if (number % 2 == 0 ) {printf ("偶数" ); }else printf ("奇数" ); return 0 ; }
2.表达式值的运用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> int main () { char c; int isLowercaseVowel, isUppercaseVowel; printf ("输入一个字母: " ); scanf ("%c" ,&c); isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ); isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); if (isLowercaseVowel || isUppercaseVowel) printf ("%c 是元音" , c); else printf ("%c 是辅音" , c); return 0 ; }
3.内外双层循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> int main () { int i = 0 ; int j = 0 ; for (i=1 ;i<=9 ;i++){ for (j=1 ;j<=i;j++){ printf ("%dx%d=%d\t" ,j,i,i*j); } printf ("\n" ); } }
4.GCD的另一种算法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { int a,b; int t; scanf ("%d %d" , &a, &b); while (b !=0 ){ t = a%b; a = b; b = t; printf ("a=%d b=%d t=%d\n" ,a , b, t); } printf ("最大公约数是%d\n" , a); return 0 ; }
4.阶乘 正序循环: 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 #include <stdio.h> int main () { int n, i; unsigned long long factorial = 1 ; printf ("输入一个整数: " ); scanf ("%d" ,&n); if (n < 0 ) printf ("Error! 负数没有阶乘jiechen" ); else { for (i=1 ; i<=n; ++i) { factorial *= i; } printf ("%d! = %llu" , n, factorial); } return 0 ; }
递归: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> long int multiplyNumbers (int n) ; int main () { int n; printf ("输入一个整数: " ); scanf ("%d" , &n); printf ("%d! = %ld" , n, multiplyNumbers(n)); return 0 ; }long int multiplyNumbers (int n) { if (n > 1 ) return n*multiplyNumbers(n-1 ); else return 1 ; }
5.数位判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> int main () { long long n; int count = 0 ; printf ("输入一个整数: " ); scanf ("%lld" , &n); while (n != 0 ) { n /= 10 ; ++count; } printf ("数字是 %d 位数。" , count); }
6.斐波那契 指定个数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio.h> int main () { int i, n, t1 = 0 , t2 = 1 , nextTerm; printf ("输出几项: " ); scanf ("%d" , &n); printf ("斐波那契数列: " ); for (i = 1 ; i <= n; ++i) { printf ("%d, " , t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return 0 ; }
7.整型数字翻转 循环: 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 #include <stdio.h> int main () { int n, reversedInteger = 0 , remainder, originalInteger; printf ("输入一个整数: " ); scanf ("%d" , &n); originalInteger = n; while ( n!=0 ) { remainder = n%10 ; reversedInteger = reversedInteger*10 + remainder; n /= 10 ; } if (originalInteger == reversedInteger) printf ("%d 是回文数。" , originalInteger); else printf ("%d 不是回文数。" , originalInteger); return 0 ; }
使用字符串: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> #include <string.h> int main () { int a=12021 ; char s[10 ]={'\0' },s1[10 ]={'\0' }; sprintf (s,"%d" ,a); int n=strlen (s); int j=0 ; for (int i=n-1 ;i>=0 ;i--) { s1[j++]=s[i]; } printf ("%s %s\n" ,s,s1); if (!strcmp (s,s1)) printf ("整数%d是回文串" ,a); else printf ("整数%d不是回文串" ,a); return 0 ; }
8.表格形式输出数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main () { int i, j, count; for (i = 1 ; i <= 10 ; i++) { for (j = i; j <=100 ; j += 10 ) printf (" %3d" , j); printf ("\n" ); } return 0 ; }
9.数组拷贝 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> int main () { int original[10 ] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 }; int copied[10 ]; int loop; for (loop = 0 ; loop < 10 ; loop++) { copied[loop] = original[loop]; } printf ("元素数组 -> 拷贝后的数组 \n" ); for (loop = 0 ; loop < 10 ; loop++) { printf (" %2d %2d\n" , original[loop], copied[loop]); } return 0 ; }
约瑟夫环
10.分解某数作为两个素数的和 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 #include <stdio.h> int checkPrime (int n) ;int main () { int n, i, flag = 0 ; printf ("输入正整数: " ); scanf ("%d" , &n); for (i = 2 ; i <= n/2 ; ++i) { if (checkPrime(i) == 1 ) { if (checkPrime(n-i) == 1 ) { printf ("%d = %d + %d\n" , n, i, n - i); flag = 1 ; } } } if (flag == 0 ) printf ("%d 不能分解为两个素数。" , n); return 0 ; } int checkPrime (int n) { int i, isPrime = 1 ; for (i = 2 ; i <= n/2 ; ++i) { if (n % i == 0 ) { isPrime = 0 ; break ; } } return isPrime; }
11.字符串翻转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> void reverseSentence () ; int main () { printf ("输入一个字符串: " ); reverseSentence(); return 0 ; } void reverseSentence () { char c; scanf ("%c" , &c); if ( c != '\n' ) { reverseSentence(); printf ("%c" ,c); } }
12. 冒泡法排序 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 #include <stdio.h> void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; }void bubbleSort (int arr[], int n) { int i, j; for (i = 0 ; i < n - 1 ; i++) { for (j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { swap(&arr[j], &arr[j + 1 ]); } } } }void printArray (int arr[], int n) { int i; for (i = 0 ; i < n; i++) { printf ("%d " , arr[i]); } printf ("\n" ); }int main () { int arr[] = {64 , 34 , 25 , 12 , 22 , 11 , 90 }; int n = sizeof (arr) / sizeof (arr[0 ]); printf ("排序前的数组: " ); printArray(arr, n); bubbleSort(arr, n); printf ("排序后的数组: " ); printArray(arr, n); return 0 ; }
13.矩阵相加 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 #include <stdio.h> int main () { int r, c, a[100 ][100 ], b[100 ][100 ], sum[100 ][100 ], i, j; printf ("输入行数 ( 1 ~ 100): " ); scanf ("%d" , &r); printf ("输入列数 ( 1 ~ 100): " ); scanf ("%d" , &c); printf ("\n输入第一维数组的元素:\n" ); for (i=0 ; i<r; ++i) for (j=0 ; j<c; ++j) { printf ("输入元素 a%d%d: " ,i+1 ,j+1 ); scanf ("%d" ,&a[i][j]); } printf ("输入第二维数组的元素:\n" ); for (i=0 ; i<r; ++i) for (j=0 ; j<c; ++j) { printf ("输入元素 a%d%d: " ,i+1 , j+1 ); scanf ("%d" , &b[i][j]); } for (i=0 ;i<r;++i) for (j=0 ;j<c;++j) { sum[i][j]=a[i][j]+b[i][j]; } printf ("\n二维数组相加结果: \n\n" ); for (i=0 ;i<r;++i) for (j=0 ;j<c;++j) { printf ("%d " ,sum[i][j]); if (j==c-1 ) { printf ("\n\n" ); } } return 0 ; }
14.矩阵转置 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 #include <stdio.h> int main () { int a[10 ][10 ], transpose[10 ][10 ], r, c, i, j; printf ("输入矩阵的行与列: " ); scanf ("%d %d" , &r, &c); printf ("\n输入矩阵元素:\n" ); for (i=0 ; i<r; ++i) for (j=0 ; j<c; ++j) { printf ("输入元素 a%d%d: " ,i+1 , j+1 ); scanf ("%d" , &a[i][j]); } printf ("\n输入矩阵: \n" ); for (i=0 ; i<r; ++i) for (j=0 ; j<c; ++j) { printf ("%d " , a[i][j]); if (j == c-1 ) printf ("\n\n" ); } for (i=0 ; i<r; ++i) for (j=0 ; j<c; ++j) { transpose[j][i] = a[i][j]; } printf ("\n转换后矩阵:\n" ); for (i=0 ; i<c; ++i) for (j=0 ; j<r; ++j) { printf ("%d " ,transpose[i][j]); if (j==r-1 ) printf ("\n\n" ); } return 0 ; }
15.连接字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> int main () { char s1[100 ], s2[100 ], i, j; printf ("输入第一个字符串: " ); scanf ("%s" , s1); printf ("输入第二个字符串: " ); scanf ("%s" , s2); for (i = 0 ; s1[i] != '\0' ; ++i); for (j = 0 ; s2[j] != '\0' ; ++j, ++i) { s1[i] = s2[j]; } s1[i] = '\0' ; printf ("连接后: %s" , s1); return 0 ; }
16.获取字符串长度 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> #include <string.h> int main () { char s[1000 ]; int len; printf ("输入字符串: " ); scanf ("%s" , s); len = strlen (s); printf ("字符串长度: %d" , len); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main () { char s[1000 ], i; printf ("输入字符串: " ); scanf ("%s" , s); for (i = 0 ; s[i] != '\0' ; ++i); printf ("字符串长度: %d" , i); return 0 ; }
17.字符类型的统计 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> int main () { char line[150 ]; int i, vowels, consonants, digits, spaces; vowels = consonants = digits = spaces = 0 ; printf ("输入一个字符串: " ); scanf ("%[^\n]" , line); for (i=0 ; line[i]!='\0' ; ++i) { if (line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U' ) { ++vowels; } else if ((line[i]>='a' && line[i]<='z' ) || (line[i]>='A' && line[i]<='Z' )) { ++consonants; } else if (line[i]>='0' && line[i]<='9' ) { ++digits; } else if (line[i]==' ' ) { ++spaces; } } printf ("元音: %d" ,vowels); printf ("\n辅音: %d" ,consonants); printf ("\n数字: %d" ,digits); printf ("\n空白符: %d" , spaces); return 0 ; }
18.约瑟夫环问题
约瑟夫环(Josephus Problem)是一个经典的数学和计算机科学问题。问题是这样的:有 n个人围成一圈,从某个人开始顺序编号为1,2,3,4,····n 。从编号为 1的人开始报数,报到 m的人出列,然后下一个人重新从 1开始报数,报到 m的人又出列,如此循环,直到所有的人都出列为止。求出列的顺序。
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 #include <stdio.h> void josephus (int n, int m) { int people[n]; for (int i = 0 ; i < n; i++) { people[i] = 0 ; } int count = 0 ; int index = 0 ; int num = 1 ; while (count < n) { if (people[index] == 0 ) { if (num == m) { people[index] = 1 ; printf ("%d " , index + 1 ); count++; num = 1 ; } else { num++; } } index = (index + 1 ) % n; } }int main () { int n = 7 ; int m = 3 ; printf ("出列顺序为: " ); josephus(n, m); return 0 ; }