1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <assert.h> 6 int main() 7 { 8 unsigned char a = 200; 9 unsigned char b = 100; 10 unsigned char c = 0; 11 c = a + b; 12 printf("%d %d", a + b , c);//整型提升问题 13 system("pause"); 14 return 0; 15 } 16 17 int main() 18 { 19 //在32位大端模式处理器下变量b等于( ) 20 unsigned int a = 0x1234; 21 unsigned char b = *(unsigned char *)&a; 22 printf("%d\n", b); 23 system("pause"); 24 return 0; 25 } 26 27 int main() 28 { 29 char a[1000] = { 0 }; 30 int i = 0; 31 for (i = 0; i < 1000; i++) 32 { 33 a[i] = -1 - i; 34 } 35 printf("%d\n", strlen(a));//255 36 system("pause"); 37 return 0; 38 } 39 40 杨辉三角形 41 int main() 42 { 43 int arr[10][10] = { 0 }; 44 int i = 0; 45 int j = 0; 46 for (i = 0; i < 10; i++) 47 { 48 for (j = 0; j < 10; j++) 49 { 50 if (j == 0) 51 { 52 arr[i][j] = 1; 53 } 54 if (i == j) 55 { 56 arr[i][j] = 1; 57 } 58 //计算中间的数 59 if (i >= 2 && j >= 1) 60 { 61 arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; 62 } 63 } 64 } 65 //打印 66 for (i = 0; i < 10; i++) 67 { 68 for (j = 0; j <= i; j++) 69 { 70 printf("%d ", arr[i][j]); 71 } 72 printf("\n"); 73 } 74 system("pause"); 75 return 0; 76 } 77 78 猜凶手 79 int main() 80 { 81 int killer = 0; 82 for (killer = 'a'; killer <= 'd'; killer++) 83 { 84 if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3) 85 { 86 printf("killer = %c\n", killer); 87 } 88 } 89 system("pause"); 90 return 0; 91 } 92 93 int main() 94 { 95 int a = 0; 96 int b = 0; 97 int c = 0; 98 int d = 0; 99 int e = 0; 100 for (a = 1; a <= 5; a++) 101 { 102 for (b = 1; b <= 5; b++) 103 { 104 for (c = 1; c <= 5; c++) 105 { 106 for (d = 1; d <= 5; d++) 107 { 108 for (e = 1; e <= 5; e++) 109 { 110 if (((b == 2) + (a == 3) == 1) && 111 ((b == 2) + (e == 4) == 1) && 112 ((c == 1) + (d == 2) == 1) && 113 ((c == 5) + (d == 3) == 1) && 114 ((e == 4) + (a == 1) == 1)) 115 { 116 if (a*b*c*d*e == 120) 117 { 118 printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", a, b, c, d, e); 119 } 120 121 } 122 } 123 } 124 } 125 } 126 } 127 system("pause"); 128 return 0; 129 } 130 131 int main() 132 { 133 //申请空间 134 int *p = (int*)malloc(10 * sizeof(int)); 135 //使用空间 136 137 //释放空间 138 free(p); 139 system("pause"); 140 return 0; 141 } 142 143 int main() 144 { 145 char str1[] = "hello bit."; 146 char str2[] = "hello bit."; 147 char *str3 = "hello bit."; 148 char *str4 = "hello bit."; 149 if (str1 == str2) 150 { 151 printf("str1 and str2 are same\n"); 152 } 153 else{ 154 printf("str1 and str2 are not same\n"); 155 } 156 if (str3 == str4) 157 { 158 printf("str3 and str4 are same\n"); 159 } 160 else{ 161 printf("str3 and str4 are not same\n"); 162 } 163 system("pause"); 164 return 0; 165 } 166 167 int main() 168 169 { 170 int a[5] = { 5, 4, 3, 2, 1 }; 171 int *ptr = (int *)(&a + 1); 172 printf("%d,%d\n", *(a + 1), *(ptr - 1)); 173 system("pause"); 174 return 0; 175 } 176 177 int main() 178 { 179 int aa[2][5] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; 180 int *ptr1 = (int *)(&aa + 1); 181 int *ptr2 = (int *)(*(aa + 1)); 182 printf("%d,%d\n", *(ptr1 - 1), *(ptr2 - 1)); 183 system("pause"); 184 return 0; 185 } 186 187 //字符串左旋(旋转字符串) - 方法一 - 暴力求解法 188 //形参是数组实现 189 void left_move(char arr[],int k) 190 { 191 192 } 193 //形参是指针实现 194 void left_move(char* arr, int k) 195 { 196 assert(arr!=NULL);//断言 197 int i = 0; 198 int len = strlen(arr); 199 for (i = 0; i < k; i++) 200 { 201 //左旋转一个字符 202 char tmp = *arr; 203 int j = 0; 204 for (j = 0; j < len-1; j++) 205 { 206 *(arr + j) = *(arr + j + 1); 207 } 208 *(arr + len - 1) = tmp; 209 } 210 } 211 int main() 212 { 213 char arr[] = "abcdef"; 214 left_move(arr, 2); 215 printf("%s\n", arr); 216 system("pause"); 217 return 0; 218 } 219 220 void reverse(char* left, char* right) 221 { 222 assert(left != NULL); 223 assert(right != NULL); 224 while (left<right) 225 { 226 char tmp = *left; 227 *left = *right; 228 *right = tmp; 229 left++; 230 right++; 231 } 232 } 233 void left_move(char* arr, int k) 234 { 235 assert(arr);//指针断言 236 int len = strlen(arr); 237 assert(k <= len);//断言,防止越界 238 reverse(arr,arr+k-1);//逆序左边 239 reverse(arr+k,arr+len-1);//逆序右边 240 reverse(arr,arr+len-1);//逆序整体 241 } 242 //方法二 - 三步翻转法 243 int main() 244 { 245 char arr[] = "abcdef"; 246 left_move(arr, 2); 247 printf("%s\n", arr); 248 system("pause"); 249 return 0; 250 } 251 252 //判断字符串是否旋转后得到另一个字符串 253 //把所有可能性都列出来 254 int is_left_move(char* s1, char* s2) 255 { 256 int len = strlen(s1); 257 int i = 0; 258 for (i = 0; i < len; i++) 259 { 260 left_move(s1,1); 261 int ret = strcmp(s1, s2); 262 if (ret == 0) 263 { 264 return 1; 265 } 266 } 267 //所有字符比完后再整体判断是否相等 268 return 0; 269 } 270 int main() 271 { 272 char arr1[] = "abcdef"; 273 char arr2[] = "cdefab"; 274 int ret = is_left_move(arr1, arr2); 275 if (ret == 1) 276 { 277 printf("yes\n"); 278 } 279 else{ 280 printf("no\n"); 281 } 282 system("pause"); 283 return 0; 284 } 285 286 287 int is_left_move(char* str1,char* str2) 288 { 289 int len1 = strlen(str1); 290 int len2 = strlen(str2); 291 if (len1 != len2) 292 { 293 return 0; 294 } 295 //1.在str1字符串中追加一个str1字符串 296 strncat(str1, str2, 6); 297 //2.判断str2指向的字符串是否是str1指向的字符串的子串 298 //strstr() - 找字串的函数 299 char * ret = strstr(str1, str2); 300 if (ret == NULL) 301 { 302 return 0; 303 } 304 else{ 305 return 1; 306 } 307 } 308 int main() 309 { 310 char arr1[30] = "abcdef"; 311 char arr2[] = "cdefab"; 312 int ret = is_left_move(arr1,arr2); 313 if (ret == 1) 314 { 315 printf("yes\n"); 316 } 317 else{ 318 printf("no\n"); 319 } 320 system("pause"); 321 return 0; 322 } 323 324 int main() 325 { 326 327 system("pause"); 328 return 0; 329 } 330 331 杨氏矩阵 332 int FindNum(int arr[3][3], int k,int *px,int *py) 333 { 334 int x = 0; 335 int y = *px - 1; 336 while (x<=*py-1 && y>=0) 337 { 338 if (arr[x][y] > k) 339 { 340 y--; 341 } 342 else if (arr[x][y]<k) 343 { 344 x++; 345 } 346 else{ 347 *px = x; 348 *py = y; 349 return 1; 350 } 351 } 352 //找不到 353 *px = -1; 354 *py = -1; 355 return 0; 356 } 357 int main() 358 { 359 int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 360 int k = 7; 361 int x = 3; 362 int y = 3; 363 //返回型参数 364 int ret = FindNum(arr, k, &x, &y); 365 if (ret == 1) 366 { 367 printf("找到了\n"); 368 printf("%d %d\n", x, y); 369 printf("%p %p\n", x, y); 370 } 371 else{ 372 printf("找不到\n"); 373 } 374 system("pause"); 375 return 0; 376 } 377 378 379 380 //1.计数器方法 381 //2.递归方法 382 //3.指针-指针方法 383 384 //size_t my_strlen(const char* str) 385 int my_strlen(const char* str) 386 { 387 int count = 0; 388 assert(str != NULL);//断言,字符串不能为空 389 while (*str != '\0')// == while(*str) 390 { 391 count++; 392 str++; 393 } 394 return count; 395 } 396 int main() 397 { 398 int len = my_strlen("abcdef"); 399 //int arr[] = { 'a', 'b', 'c', 'd', 'e', 'f','\0' }; 400 //int len1 = strlen(arr); 401 printf("%d\n",len); 402 //printf("%d\n", len1); 403 system("pause"); 404 return 0; 405 } 406 407 408 int main() 409 { 410 //size_t = unsigned int = 无符号整型 411 if (strlen("abc") - strlen("abcdef") > 0) 412 { 413 printf("hehe\n"); 414 } 415 else{ 416 printf("haha\n"); 417 } 418 system("pause"); 419 return 0; 420 }