#include <stdio.h> //1.求任意10个整数的最大数 int get_max_1(int arr[]) { int i = 0; int max = arr[0]; for(i=1; i<10; i++) { if(max<arr[i]) { max=arr[i]; } } return max; } int get_max_2(int* arr) { int i = 0; int max = *arr; for(i=1; i<10; i++) { if(max < *(arr+i)) { max = *(arr+i); } } return max; } //2.求1/1-1/2+1/3-1/4+1/5...+1/99-1/100的值 double caculation(int n) { double sum = 0.0; int i = 0; int flag = 1; for(i=1; i<=n; i++) { sum += flag*(1.0/i); flag = -flag; } return sum; } //3.实现九九乘法表 void multiplication_table() { int i = 0; int j = 0; for(i=1;i<=9;i++) // line { for(j=1;j<=i;j++) { printf("%d*%d=%-2d ",i,j,i*j); } printf("\n"); } } //4.编写程序数一下1-100的所有数中,出现多少个数字9? int count_9() { int counter = 0; int i = 0; for(i=1;i<=100;i++) { if(i%10 == 9) { counter++; } else if (i/10==9) { counter++; } } return counter; } //5. revert the adcdef string: abcdef-->fedcba int my_strlen(char* arr) { int count = 0; while(*arr != '\0') { count++; arr++; } return count; } //method-1 void revert_string_1(char* arr) { int left = 0; int right = my_strlen(arr) - 1; while(left<right) { char temp = *(arr+left); *(arr+left) = *(arr+right); *(arr+right) = temp; left++; right--; } } //method-2: 递归实现 void revert_string_2(char * arr) { char temp = *arr; int right = my_strlen(arr)-1; *arr = *(arr+right); *(arr+right) = '\0'; if(my_strlen(arr)>1) { revert_string_2(arr+1); } *(arr+right) = temp; } //6.编写一个函数实现n的k次方,用递归实现 // k=0, n**k = 1 // k>0, n**k = n*Pow(n, k-1) // k<0, n**k = 1.0/n*Pow(n, k-1) double Pow(int n, int k) { if(k==0) { return 1.0; } else if(k>0) { return 1.0*n*Pow(n, k-1); } else if (k<0) { return 1.0/Pow(n, -k); } } // 7.给定一个非负整数,求该整数的各位之和; int my_sum(int num) { if(num>9) { return my_sum(num/10)+ my_sum(num%10); } else { return num; } } void main() { //1. int my_arr1[10]={-1,2,4,5,19,45,34,64,100,99}; int max_val_1 = get_max_1(my_arr1); int max_val_2 = get_max_2(my_arr1); printf("The max value is %d\n", max_val_1); printf("The max value is %d\n", max_val_2); //2. double sum = caculation(100); printf("1/1-1/2+1/3...-1/100=%lf\n", sum); //3. multiplication_table(); //4. int count = count_9(); printf("\ncounter of 9 in 1-100 is %d\n", count); //5. char arr[] = "abcdef"; revert_string_2(arr); printf("%s\n", arr); //6. int n = 0; int k = 0; scanf("%d%d", &n, &k); double sum_pow = Pow(n, k); printf("%d 的%d次方:%lf\n", n, k, sum_pow); //7. int num = 123456; int num_sum = my_sum(num); printf("The sum of num is %d\n", num_sum); }