39. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<cstring> int add(int?? ?x, int y);//声明函数 int main() { ?? ?int a = 10; ?? ?int b = 20; ?? ?int sum = add(a,b);//函数调用 ?? ?printf("%d", sum); ?? ?return 0; } int add(int x, int y) { ?? ?int z = x + y; ?? ?return z; }
40. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<cstring> int choose(int arr[],int sz,int k) { ?? ? ?? ?int left = 0; ?? ?int right = sz - 1;//右下标 ?? ?int mid = (left + right) / 2; ?? ?while (left<=right) ?? ?{ ?? ??? ?int mid = (left + right) / 2; ?? ??? ?if (arr[mid] < k) ?? ??? ?{ ?? ??? ??? ?left++; ?? ??? ?} ?? ??? ?else if (arr[mid] > k) ?? ??? ?{ ?? ??? ??? ?right--; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return mid; ?? ??? ?} ?? ??? ? ?? ?} ?? ?return 1; } int main() { ?? ?int?? ?arr[20] = { 1,2,3,4,5,6,7,8,9,10 }; ?? ?int left = 0; ?? ?int k = 11; ?? ?int sz = sizeof(arr) / sizeof(arr[0]); ?? ?int right = sz - 1;//右下标 ?? ?int mid = (left + right) / 2; ?? ?int c = choose(arr,sz,k); ?? ?if (c==1) ?? ?{ ?? ??? ?printf("找不到"); ?? ?} ?? ?else ?? ?{ ?? ??? ?printf("下标是 % d\n", c); ?? ?} ?? ?return 0; }
41. #include<stdio.h> #include<string.h> int contact(int a, int b, int c) { ?? ?int m = a > b ? (a > c ? a : c) : (b > c ? b : c); ?? ?return m; } int main() { ?? ?int a = 230; ?? ?int b = 30; ?? ?int c = 40; ?? ?int max = 0; ?? ?max = contact(a, b, c); ?? ?printf("%d", max); ?? ?return 0; } ?
|