当前位置:   article > 正文

苏小红c语言第四版课后习题练习9.3--9.4_c语言程序设计苏小红第四版答案9.4

c语言程序设计苏小红第四版答案9.4

 9.3

               

  1. 1 #include <stdio.h>
  2. 2 void Swap(int *p, int *q){
  3. 3 int t;
  4. 4
  5. 5 t = *p;
  6. 6 *p = *q;
  7. 7 *q = t;
  8. 8 }
  9. 9
  10. 10 int main()
  11. 11 {
  12. 12 int a[5] = {1,2,3,4,5};
  13. 13 int b[5] = {6,7,8,9,10};
  14. 14 int i, j, k;
  15. 15
  16. 16 for(i = 0; i < 5; i++){
  17. 17 Swap(&a[i], &b[i]);
  18. 18 }
  19. 19 for(j = 0; j < 5; j++){
  20. 20 printf("a[%d]= %d ", j,a[j]);
  21. 21 }
  22. 22
  23. 23 printf("\n");
  24. 24
  25. 25 for(k = 0; k < 5; k++){
  26. 26 printf("b[%d]= %d ", k, b[k]);
  27. 27 }
  28. 28 return 0;
  29. 29 }

9.4

        

  1. 1 #include <stdio.h>
  2. 2 void Swap(int *p, int *q){
  3. 3 int t;
  4. 4
  5. 5 t = *p;
  6. 6 *p = *q;
  7. 7 *q = t;
  8. 8 }
  9. 9
  10. 10 int main()
  11. 11 {
  12. 12 int a[10];
  13. 13 int i, j, k, l, b = 0, d = 0, max1, max2;
  14. 14 int *p = a;
  15. 15
  16. 16 for(i = 0; i < 10; i++){
  17. 17 scanf("%d", &a[i]);
  18. 18 }
  19. 19
  20. 20 max1 = a[0];
  21. 21 max2 = a[0];
  22. 22
  23. 23 for(j = 1; j < 10; j++){
  24. 24 if(max1 > a[j]){
  25. 25 max1 = a[j];
  26. 26 b = j;
  27. 27 }
  28. 28 }
  29. 29
  30. 30 for(k = 1; k < 10; k++){
  31. 31 if(max2 < a[k]){
  32. 32 max2 = a[k];
  33. 33 d = k;
  34. 34 }
  35. 35 }
  36. 36
  37. 37 Swap(p+b,p+d);
  38. 38
  39. 39 for(l = 0; l < 10; l++){
  40. 40 printf("a[%d] = %d ", l, *(p+l));
  41. 41 }
  42. 42
  43. 43 return 0;
  44. 44
  45. 45 }
  46. 46

 本体思路就是直接分别找到最大值以及最小值,并借此找到对应的下标,将最大值最小值的下标通过函数进行交换即可;

                            

  1. 1 #include <stdio.h>
  2. 2 int Fun(const int arr1[], const int arr2[], const int year, const int month, const int day){
  3. 3
  4. 4 int i, j, sum = 0;
  5. 5
  6. 6 if((year%4==0&&year%100!=0)||(year%400==0)){
  7. 7 for(i = 0; i < month-1; i++){
  8. 8 sum+= arr1[i];
  9. 9 }
  10. 10 }else {
  11. 11 for(j = 0; j < month-1; j++){
  12. 12 sum+= arr2[j];
  13. 13 }
  14. 14 }
  15. 15 return sum+day;
  16. 16
  17. 17 }
  18. 18 int main()
  19. 19 {
  20. 20 int year, month, day, d;
  21. 21 int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  22. 22 int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
  23. 23
  24. 24 printf("请输入年月日:\n");
  25. 25
  26. 26 scanf("%d %d %d", &year, &month, &day);
  27. 27
  28. 28 d = Fun(b, a, year, month, day);
  29. 29
  30. 30 printf("该日为 %d 年的 %d 天;\n", year, d);
  31. 31
  32. 32 return 0;
  33. 33 }
  34. 34

(题目中要求的函数没有输入数组,按要求可以把数组放到函数中在定义就可以) 

 

 

 

  1. 1 #include <stdio.h>
  2. 2 void Monthday(int year, int yearday, int *pmonth, int *pday){
  3. 3 int k, l, m, sum = 0;;
  4. 4 int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  5. 5 int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
  6. 6 if(year%4==0&&year%100!=0||year%400==0){
  7. 7 for(k = 0; k < 12; k++){
  8. 8 sum+= b[k];
  9. 9 if(yearday >= sum&&yearday <= (sum+b[k+1])){
  10. 10 m = k+1;
  11. 11 break;
  12. 12 }
  13. 13 }
  14. 14 }else {
  15. 15 for(l = 0; l < 12; l++){
  16. 16 sum+= a[l];
  17. 17 if(yearday >= sum&&yearday <= (sum+a[l+1])){
  18. 18 m = l+1;
  19. 19 break;
  20. 20 }
  21. 21 }
  22. 22 }
  23. 23
  24. 24 printf("为该年的 %d 月 %d 日 \n", *(pmonth+m), *(pday+yearday-sum-1));
  25. 25
  26. 26
  27. 27 }
  28. 28
  29. 29 int main()
  30. 30 {
  31. 31 int year, yearday, i, j;
  32. 32 int month[12], day[31];
  33. 33 int *pmonth = month;
  34. 34 int *pday = day;
  35. 35
  36. 36 printf("请输入年份及日子:\n");
  37. 37 scanf("%d %d", &year, &yearday);
  38. 38
  39. 39 for(i = 0; i < 12; i++){
  40. 40 *(pmonth+i) = i+1;
  41. 41 }
  42. 42 for(j = 0; j < 31; j++){
  43. 43 *(pday+j) = j+1;
  44. 44 }
  45. 45
  46. 46 Monthday(year, yearday, pmonth, pday);
  47. 47
  48. 48
  49. 49 return 0;
  50. 50 }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/290197
推荐阅读
相关标签
  

闽ICP备14008679号