C语言函数function练习题
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <conio.h> 5 6 int main111(void) 7 { 8 #if( 0 ) 9 void avg(void); 10 avg(); 11 #endif //去掉一个最高分和最低分求平均分 12 13 #if( 0 ) 14 void calroot(double a, double b, double c); 15 double x , y , z; 16 printf("输入方程的系数a:"); 17 scanf("%lf",&x); 18 printf("输入方程的系数b:"); 19 scanf("%lf",&y); 20 printf("输入方程的系数c:"); 21 scanf("%lf",&z); 22 calroot(x,y,z); 23 #endif //求一元二次方程的根 24 25 #if( 0 ) 26 double sum(int n),result = 0; 27 int x; 28 printf("input a param\n"); 29 scanf("%d",&x); 30 result = sum(x); 31 printf("%.2lf",result); 32 #endif //求1-1/2+1/3-1/4+...+(-1)^(n+1)/n 33 34 #if( 0 ) 35 //辗转相除法 36 /* 37 int a,b,x,y; 38 int t; 39 scanf("%d %d", &a, &b); 40 x = a , y = b; 41 while (b !=0 ){ 42 t = a%b; 43 a = b; 44 b = t; 45 printf("a=%d b=%d t=%d\n",a , b, t); 46 } 47 printf("最大公约数是%d\n", a); 48 printf("最小公倍数%3d",x*y/a);*/ //最小公倍数=两数的乘积/最大公约(因)数 49 50 //更相减损法 51 /* 52 int n1, n2; 53 54 printf("输入两个数,以空格分隔: "); 55 scanf("%d %d",&n1,&n2); 56 57 while(n1!=n2) 58 { 59 if(n1 > n2) 60 n1 -= n2; 61 else 62 n2 -= n1; 63 } 64 printf("GCD = %d",n1); 65 */ 66 67 68 69 int gcd(int a,int b); 70 int result; 71 result = gcd(319,377); 72 printf("%d",result); 73 #endif //求形参m,n的最大公约数 74 75 #if( 0 ) 76 ① 77 *double x,y,i; 78 int a,b; 79 x = 3.0; 80 do 81 { 82 i = 2.0; 83 do 84 { 85 y = x / i; 86 a = (int)y; 87 if( y != a) 88 { 89 if(i == x-a) 90 { 91 b = (int)x; 92 printf("%d\n",b); 93 } 94 } 95 i++; 96 } while (y != a); 97 x++; 98 } while (x <= 100.0);*/ 99 ② 100 int a=0, i; // 素数的个数 101 int num=0; // 输入的整数 102 printf("输入一个整数:"); 103 scanf("%d",&num); 104 for( i=2;i<num;i++){ 105 if(num%i==0){ 106 a++; // 素数个数加1 107 } 108 } 109 if(a==0){ 110 printf("%d是素数。\n", num); 111 }else{ 112 printf("%d不是素数。\n", num); 113 } 114 ③ 115 int k,i,tag; 116 for(i=2;i<=100;i++) 117 { 118 tag=0; 119 for(k=2;k<i;k++) 120 { 121 if(i%k==0) 122 tag=1; 123 } 124 if(tag==0) 125 printf("%d,",i); 126 } 127 ④ 128 *int m,i,k,h=0,leap=1; 129 for(m=2;m<=100;m++){ 130 k=(int)sqrt((double)m+1); 131 for(i=2;i<=k;i++) 132 if(m%i==0){ 133 leap=0; 134 break; 135 } 136 if(leap){ 137 printf("%d\n",m); 138 } 139 leap=1; 140 */ 141 } 142 #endif //求质数 143 144 #if( 0 ) 145 int k,num;long sum=0; 146 scanf("%d",&num); 147 for(k=1;k<=num;k++) 148 sum=sum+h2(k); 149 printf("(1!)+(1!+2!)+...+(1!+2!+...+%d!)=%ld\n",num,sum); 150 #endif //计算s=(1!)+(1!+2!)+...+(1!+2!...+n!) 151 152 #if( 0 ) 153 int a[10],i, sushu(int x); 154 for ( i = 0; i < 10; i++) 155 { 156 scanf("%d",&a[i]); 157 } 158 printf("sushu are "); 159 for ( i = 0; i < 10; i++) 160 { 161 if(sushu(a[i])) printf("%5d", a[i]); 162 } 163 #endif //输出一个数组里面的质数 164 165 #if( 0 ) 166 int a[10],i, sushu(int x); 167 for ( i = 0; i < 10; i++) 168 { 169 scanf("%d",&a[i]); 170 } 171 printf("sushu are "); 172 for ( i = 0; i < 10; i++) 173 { 174 if(sushu(a[i])) printf("%5d", a[i]); 175 } 176 #endif //在一个一维数组中存放10个正整数,求其中所有的素数 177 178 #if( 0 ) 179 void inverse(char str[]); 180 char str[100]; 181 printf("input string"); 182 gets(str); 183 inverse(str); 184 puts(str); 185 #endif //反序输出一个输入的字符串 186 187 #if( 0 ) 188 void space(char str[]); 189 char a[80]; 190 gets(a); 191 space(a); 192 puts(a); 193 #endif //输入一个数字字符 每两个数字间空一个空格 "1 9 9 0" 194 195 #if( 0 ) 196 void sort(char(*s)[6]); 197 char str[10][6]; 198 char(*p)[6]; 199 int i; 200 printf("input 10 string"); 201 for ( i = 0; i < 10; i++) 202 { 203 scanf("%s",str[i]); 204 } 205 p = str; 206 sort(p); 207 printf("Now,the sequence is:\n"); 208 for ( i = 0; i < 10; i++) 209 { 210 printf("%s\n",str[i]); 211 //} 212 #endif //编写一个程序,在主函数中输入10个!等长!的字符串。用另一个函数sort()对它们排序,要求形参是指向由6个元素组成的一维数组的行指针,然后在主函数输出这10个已排好的字符串。 213 214 #if( 0 ) 215 int fun(int a); 216 int i, sum = 0; 217 for ( i = 1; i <= 100; i++) 218 { 219 sum += fun(i); 220 } 221 printf("和为%d , 平均值:%.2f",sum,sum/100.0); 222 #endif //1,1,2,3,5,8,13,...的前100项的和及平均值 223 224 //int i,j,x; 225 //for ( i = 0; i <=4 ; i++) 226 //{ 227 // for ( j = 1; j <= 4-i; j++) 228 // printf(" "); 229 // for ( j = 0; j <=2*i-1 ; j++) 230 // printf("*"); 231 // printf("\n"); 232 //} 233 //int num[]={3,4,5,6,7,8,9,10,11,12,13,14}; 234 //int i,j,k,x,y; 235 // x=0;y=12-1; 236 // while(x<=y) 237 // {j=num[x];i=2;k=1; 238 // while (i<=j/2&&k) // 判断 j 是否为素数 239 // k=j%i++; 240 // if (k) 241 // printf( "%3d" ,num[x++]); 242 // else //j 不是素数首尾数据互换,尾指针前移 243 // {num[x]=num[y]; 244 // num[y--]=j; 245 // } 246 // } 247 //for (i=0;i<12;i++) 248 //printf( "%5d" ,num[i]); 249 //int a[3][4] = {{2,3,4,6},{3,1,2,3},{0,1,2,3}} , b[4][5] = {{3,-1,1,3,1},{2,0,3,5,4},{1,2,2,3,0},{1,2,3,4,2}}, c[3][5] , i , j , k , s; 250 //for ( i = 0; i < 3; i++) 251 //{ 252 // for ( j = 0; j < 5; j++) 253 // { 254 // for ( k = 0 ,s = 0; k <4; k++) 255 // { 256 // s += a[i][k] * b[k][j] ; 257 // } 258 // c[i][j] = s; 259 // } 260 //} 261 //for ( i = 0; i < 3; i++) 262 //{ 263 // for ( j = 0; j < 5; j++) 264 // { 265 // printf("%3d",c[i][j]); 266 // } 267 // printf("\n"); 268 //} 269 270 //int a[9]={11,23,41,27,42,64,82,59}; 271 //int i=0,x; 272 //scanf("%d",&x); 273 // a[8] = x ; 274 //while(a[i]!=x) i++; 275 //if( i < 8 ) printf("Found! The index is:%d\n",i); 276 //else printf("Can't found!\n"); 277 #if(0) 278 int score[40],n; 279 int ReadScore(int score[]); 280 void PrintScore(int score[],int n); 281 void AscendingSort(int a[],int n); 282 void DescendingSort(int a[],int n); 283 int order; /* 值为1表示升序排序,值为2表示降序排序*/ 284 n = ReadScore(score); 285 printf("Total students are %d\n",n); 286 printf("Enter 1 to sort in ascending order,\n"); 287 printf("Enter 2 to sort in descending order:"); 288 scanf("%d",&order); 289 if(order == 1) 290 { 291 AscendingSort(score,n); //按升序排序 292 printf("Data items in ascending order\n"); 293 } 294 else 295 { 296 DescendingSort(score,n); //按降序排序 297 printf("Data items in descending order\n"); 298 } 299 PrintScore(score,n); 300 #endif //函数实现升序降序 301 302 #if(0) 303 int score[40],n; 304 int ReadScore1(int score[]); 305 void PrintScore1(int score[],int n); 306 void SelectingSort(int a[], int n, int (*compare)(int a,int b)); 307 int AscendingSort1(int a,int b); 308 int DescendingSort1(int a,int b); 309 int order; /* 值为1表示升序排序,值为2表示降序排序*/ 310 n = ReadScore(score); 311 printf("Total students are %d\n",n); 312 printf("Enter 1 to sort in ascending order,\n"); 313 printf("Enter 2 to sort in descending order:"); 314 scanf("%d",&order); 315 if(order == 1) 316 { 317 SelectingSort(score,n,AscendingSort1); //按升序排序 318 printf("Data items in ascending order\n"); 319 } 320 else 321 { 322 SelectingSort(score,n,DescendingSort1); //按降序排序 323 printf("Data items in descending order\n"); 324 } 325 PrintScore1(score,n); 326 #endif //指针函数实现升序降序 327 328 /*char *w[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; 329 char *st; 330 int i , j ,k; 331 printf("排序前\n"); 332 for ( i = 0; i < 7; i++) 333 { 334 printf("%p\t%s\n",w[i],w[i]); 335 } 336 for ( i = 0; i < 7; i++) 337 { 338 k = i; 339 for ( j = i + 1, st = w[k] ; j < 7; j++) 340 { 341 if(strcmp(w[j],st)<0) k = j; 342 } 343 if(k != i) 344 { 345 st = w[i]; w[i] = w[k]; w[k] = st; 346 } 347 } 348 printf("排序后\n"); 349 for ( i = 0; i < 7; i++) 350 { 351 printf("%p\t%s\n",w[i],w[i]); 352 }*/ 353 354 return 0; 355 356 } 357 358 void avg(void) 359 { 360 int score[10],*p,max = 0, min = 0,sum = 0; 361 int i; 362 printf("input score a number of 10\n"); 363 p = score; 364 for ( i = 0; i < 10; i++) 365 { 366 scanf("%d", p++); 367 } 368 p = score; 369 max = min = score[0]; 370 for ( i = 0; i < 10; i++) 371 { 372 if(*(p + i) > max) max = *(p + i); 373 if(*(p + i) < min) min = *(p + i); 374 sum += score[i]; 375 } 376 printf("avg:%.1lf",(sum - max -min)/8.0); 377 } 378 void calroot(double a, double b, double c) 379 { 380 double derta = 0 , x1 = 0, x2 = 0; 381 derta = (b * b) - (4 * a * c); 382 if(derta >= 0) 383 { 384 if(derta > 0) 385 { 386 x1 = -b + sqrt(derta) / (2.0 * a); 387 x2 = -b - sqrt(derta) / (2.0 * a); 388 printf("方程有两个不相等的实根分别为:%3.2lf , %3.2lf ",x1,x2); 389 }else 390 { 391 x1 = (-b) / (2.0 * a); 392 printf("方程有两个相等的实根分别为:%3.2lf ",x1); 393 } 394 } 395 else printf("此方程无解"); 396 397 398 } 399 double sum(int n) 400 { 401 double i, sum = 0; 402 if( n > 5) 403 for ( i = 1; i <=n; i++) 404 { 405 sum += pow(-1,i+1) / i; 406 } 407 return sum; 408 } 409 int getgcd(int a,int b) 410 { 411 int c = 1, t; 412 if(a < b) 413 { 414 t = a; 415 a = b; 416 b = t; 417 } 418 while(c) 419 { 420 c = a % b; 421 a = b; 422 b = c; 423 } 424 425 return a; 426 } 427 int gcd(int a,int b) 428 429 { 430 if(a%b==0) 431 return b; 432 else; 433 return gcd(b,a%b); 434 } 435 long h1(int n) 436 { 437 int product; 438 if(n == 1) product = 1; 439 else product = n * h1(n-1); 440 return product; 441 } 442 long h2(int m) 443 { 444 long s=0; 445 int i; 446 for(i=1;i<=m;i++) 447 s=s+h1(i); 448 return s; 449 } 450 int sushu(int x) //是素数返回1 否则返回0 451 { 452 //int i, k = 1; 453 //if( x == 1 ) k = 0; 454 //for ( i = 2; i <= x/2; i++) 455 //{ 456 // if( x % i == 0) k=0; 457 //} 458 //return k; 459 460 //int m, i,flag = 1; 461 //m = (int)sqrt((double)x); 462 //if( x == 1 ) return 0; 463 //for ( i = 2; i <= m; i++) 464 //{ 465 // if( x % i == 0) 466 // { 467 // flag = 0; 468 // return 0; 469 // } 470 //} 471 //if(flag) return 1; 472 473 int i; 474 for ( i = 2; i < x; i++) 475 if(x % i == 0) break; 476 if(x == 1) return 0; 477 if(i >= x) return 1; 478 else return 0; 479 480 } 481 void inverse(char str[]) 482 { 483 char t; 484 int i, j; 485 for ( i = 0 , j = strlen(str); i < strlen(str)/2; i++, j--) 486 { 487 t = str[i]; 488 str[i] = str[j - 1]; 489 str[j - 1] = t; 490 } 491 } 492 void space(char str[]) 493 { 494 int i ; 495 for ( i = strlen(str); i > 0; i--) 496 { 497 str[2 * i] = str [i]; 498 str[2 * i - 1] = ' '; 499 } 500 } 501 //void sort(char(*s)[6]) 502 //{ 503 // int i,j; 504 // char *t,temp[6]; 505 // t = temp; 506 // for ( i = 0; i < 9; i++) 507 // { 508 // for ( j = 0; j < 9 - i; j++) 509 // { 510 // if(strcmp(s[j],s[j+1])>0) 511 // { 512 // strcpy(t,s[j]); 513 // strcpy(s[j],s[j+1]); 514 // strcpy(s[j+1],t); 515 // } 516 // } 517 // } 518 //} 519 int fun(int x) 520 { 521 int t1 = 1, t2 = 1, t3 = 0 , i; 522 int y = 0; 523 if(x == 1 || x == 2) 524 {y = 1;} 525 else if(x>2) 526 { 527 for ( i = 3; i <= x; i++) 528 { 529 t3 = t1 + t2; 530 t1 = t2 ; 531 t2 = t3; 532 } 533 return t3; 534 } 535 return y; 536 } 537 int del(int a[], int n, int x) 538 { 539 int p ,i ; 540 p = 0; 541 while (x >= a[p] && p<n) 542 p = p + 1 ; 543 for ( i = p - 1; i < n; i++) 544 a[i] = a[i + 1]; 545 n = n - 1; 546 return n; 547 } 548 int ReadScore(int score[]) 549 { 550 int i = -1; 551 do 552 { 553 i++; 554 printf("Input score"); 555 scanf("%d" , &score[i]); 556 } while (score[i] >= 0); 557 return i; 558 } 559 void PrintScore(int score[],int n) 560 { 561 int i; 562 for ( i = 0; i < n; i++) 563 { 564 printf("%3d",score[i]); 565 } 566 printf("\n"); 567 } 568 void AscendingSort(int a[],int n) 569 { 570 int i , j , k ,temp; 571 for ( i = 0; i < n-1; i++) 572 { 573 k = i; 574 for ( j = i+1; j < n; j++) 575 { 576 if(a[j] < a[k]) k = j; 577 } 578 if(k != i) 579 { 580 temp = a[i]; 581 a[i] = a[k]; 582 a[k] = temp; 583 } 584 } 585 586 //for ( i = 0; i < n -1 ; i++) 587 //{ 588 // for ( j = 1; j < n ; j++) 589 // { 590 // if(a[j] < a[j-1]) 591 // { 592 // temp = a[j]; 593 // a[j] = a[j-1]; 594 // a[j-1] = temp; 595 // } 596 // } 597 //} 598 //for ( i = 0; i < n - 1 ; i++) 599 //{ 600 // for ( j = 0 ; j < n - 1 - i; j++) 601 // { 602 // if(a[j] > a[j+1]) 603 // { 604 // temp = a[j + 1]; 605 // a[j+1] = a[j]; 606 // a[j] = temp; 607 // } 608 // } 609 //} 610 } 611 void DescendingSort(int a[],int n) 612 { 613 int i , j , k ,temp; 614 for ( i = 0; i < n-1; i++) 615 { 616 k = i; 617 for ( j = i+1; j < n; j++) 618 { 619 if(a[j] > a[k]) k = j; 620 } 621 if(k != i) 622 { 623 temp = a[k]; 624 a[k] = a[i]; 625 a[i] = temp; 626 } 627 } 628 } 629 int ReadScore1(int score[]) 630 { 631 int i = -1; 632 do 633 { 634 i++; 635 printf("Input score"); 636 scanf("%d" , &score[i]); 637 } while (score[i] >= 0); 638 return i; 639 } 640 void PrintScore1(int score[],int n) 641 { 642 int i; 643 for ( i = 0; i < n; i++) 644 { 645 printf("%3d",score[i]); 646 } 647 printf("\n"); 648 } 649 int AscendingSort1(int a,int b) 650 { 651 return a < b; //为真,则按升序排序 652 } 653 int DescendingSort1(int a,int b) 654 { 655 return a > b; //为真,则按降序排序 656 } 657 void SelectingSort(int a[], int n, int (*compare)(int a,int b)) 658 { 659 int i , j , k ,temp; 660 for ( i = 0; i < n-1; i++) 661 { 662 k = i; 663 for ( j = i + 1; j < n; j++) 664 { 665 if((*compare)(a[j] , a[k])) k = j; 666 } 667 if(k != i) 668 { 669 temp = a[k]; 670 a[k] = a[i]; 671 a[i] = temp; 672 } 673 } 674 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix