#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; } 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); }
标签:count,arr,函数,int,max,练习,循环,printf,my From: https://www.cnblogs.com/ananmy/p/17221208.html