15. #include<stdio.h> #include<string.h> int main() { char arr1[] = "welcome to beijing!!!!"; char arr2[] = "######################"; int left = 0; //int right = sizeof(arr1) / sizeof(arr1[0]) - 2; int right = strlen(arr1)-1; while (left<=right) { ?? ?arr2[left] = arr1[left]; ?? ?arr2[right] = arr1[right]; ?? ?printf("%s\n", arr2); ?? ?left++; ?? ?right--; } return 0; }
16.
#include<stdio.h> int main() { ?? ?int arr[] = { 1,2,3,4,5,6,7,8,9,10 };//下标 [0,1,2,3,4,5,6,7,8,9] ?? ?int k = 9; ?? ?int left = 0;//左下标 ?? ?int sz = sizeof(arr) / sizeof(arr[0]); ?? ?int right = sz - 1;//右下标 ?? ?int mid = (left + right) / 2; ?? ?while (left<=right) ?? ?{ ?? ??? ?int mid = (left + right) / 2; ?? ??? ?if (arr[mid] > k) ?? ??? ?{ ?? ??? ??? ?right = mid - 1;//右边向左靠 ?? ??? ?} ?? ??? ?else if (arr[mid] < k) ?? ??? ?{ ?? ??? ??? ?left = mid + 1;//左边像右靠 ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("找到了,下标是:%d\n", mid); ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?if (left>right) ?? ?{ ?? ??? ?printf("找不到了\n"); ?? ?} ?? ?return 0; }
17. #include<stdio.h> int main() { ?? ?int ?arr[] = { 1,2,3,4,5,6,7,8,9,10 }; ?? ?int i = 0; ?? ?int k = 7; ?? ?int sz = sizeof(arr) / sizeof(arr[0]); ?? ?for (int i = 0; i < sz; i++) ?? ?{ ?? ??? ?if (k == arr[i]) ?? ??? ?{ ?? ??? ??? ?printf("找到了,下标是:%d\n", i); ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?if (i == sz) ?? ?{ ?? ??? ?printf("找不到\n"); ?? ?}
?? ?return 0; }
18. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<Windows.h>
int main() { ?? ?int i = 0; ?? ?char password[20] = { 0 }; ?? ?for ( ?i = 0; ?i <=3; ?i++) ?? ?{ ?? ??? ?printf("请输入密码:"); ?? ??? ?scanf_s("%s", &password); ?? ??? ?if (strcmp(password,"123456")==0) ?? ??? ?{ ?? ??? ??? ?printf("登录成功\n"); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("密码错误\n"); ?? ??? ?} ?? ?} ?? ?if (i == 3) ?? ?{ ?? ??? ?printf("三次密码均错误,程序已退出\n"); ?? ?} return 0; }
19. #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { ?? ?int num = rand() % 100 + 1; ?? ?int count = 0; ?//用的次数 ?? ?int a = 0; ? //输入的值 ?? ?printf("我已经想好0-100之间的数"); ?? ?do { ?? ??? ?scanf_s("%d", &a); ?? ??? ?if (a > num) { ?? ??? ??? ?printf("你的值偏大了,再试试吧。"); ?? ??? ?}else if(a<num){ ?? ??? ??? ?printf("你的值偏小了再试试吧。"); ?? ??? ?} ?? ??? ??? ??? ?count++; ?? ?} while (a != num); ?? ?printf("恭喜你答对了%d,用了%d次", num,count);? ?? ??? ?return 0; }
|