赞
踩
9.3
- 1 #include <stdio.h>
- 2 void Swap(int *p, int *q){
- 3 int t;
- 4
- 5 t = *p;
- 6 *p = *q;
- 7 *q = t;
- 8 }
- 9
- 10 int main()
- 11 {
- 12 int a[5] = {1,2,3,4,5};
- 13 int b[5] = {6,7,8,9,10};
- 14 int i, j, k;
- 15
- 16 for(i = 0; i < 5; i++){
- 17 Swap(&a[i], &b[i]);
- 18 }
- 19 for(j = 0; j < 5; j++){
- 20 printf("a[%d]= %d ", j,a[j]);
- 21 }
- 22
- 23 printf("\n");
- 24
- 25 for(k = 0; k < 5; k++){
- 26 printf("b[%d]= %d ", k, b[k]);
- 27 }
- 28 return 0;
- 29 }
9.4
- 1 #include <stdio.h>
- 2 void Swap(int *p, int *q){
- 3 int t;
- 4
- 5 t = *p;
- 6 *p = *q;
- 7 *q = t;
- 8 }
- 9
- 10 int main()
- 11 {
- 12 int a[10];
- 13 int i, j, k, l, b = 0, d = 0, max1, max2;
- 14 int *p = a;
- 15
- 16 for(i = 0; i < 10; i++){
- 17 scanf("%d", &a[i]);
- 18 }
- 19
- 20 max1 = a[0];
- 21 max2 = a[0];
- 22
- 23 for(j = 1; j < 10; j++){
- 24 if(max1 > a[j]){
- 25 max1 = a[j];
- 26 b = j;
- 27 }
- 28 }
- 29
- 30 for(k = 1; k < 10; k++){
- 31 if(max2 < a[k]){
- 32 max2 = a[k];
- 33 d = k;
- 34 }
- 35 }
- 36
- 37 Swap(p+b,p+d);
- 38
- 39 for(l = 0; l < 10; l++){
- 40 printf("a[%d] = %d ", l, *(p+l));
- 41 }
- 42
- 43 return 0;
- 44
- 45 }
- 46
本体思路就是直接分别找到最大值以及最小值,并借此找到对应的下标,将最大值最小值的下标通过函数进行交换即可;
- 1 #include <stdio.h>
- 2 int Fun(const int arr1[], const int arr2[], const int year, const int month, const int day){
- 3
- 4 int i, j, sum = 0;
- 5
- 6 if((year%4==0&&year%100!=0)||(year%400==0)){
- 7 for(i = 0; i < month-1; i++){
- 8 sum+= arr1[i];
- 9 }
- 10 }else {
- 11 for(j = 0; j < month-1; j++){
- 12 sum+= arr2[j];
- 13 }
- 14 }
- 15 return sum+day;
- 16
- 17 }
- 18 int main()
- 19 {
- 20 int year, month, day, d;
- 21 int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
- 22 int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
- 23
- 24 printf("请输入年月日:\n");
- 25
- 26 scanf("%d %d %d", &year, &month, &day);
- 27
- 28 d = Fun(b, a, year, month, day);
- 29
- 30 printf("该日为 %d 年的 %d 天;\n", year, d);
- 31
- 32 return 0;
- 33 }
- 34
(题目中要求的函数没有输入数组,按要求可以把数组放到函数中在定义就可以)
- 1 #include <stdio.h>
- 2 void Monthday(int year, int yearday, int *pmonth, int *pday){
- 3 int k, l, m, sum = 0;;
- 4 int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
- 5 int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
- 6 if(year%4==0&&year%100!=0||year%400==0){
- 7 for(k = 0; k < 12; k++){
- 8 sum+= b[k];
- 9 if(yearday >= sum&&yearday <= (sum+b[k+1])){
- 10 m = k+1;
- 11 break;
- 12 }
- 13 }
- 14 }else {
- 15 for(l = 0; l < 12; l++){
- 16 sum+= a[l];
- 17 if(yearday >= sum&&yearday <= (sum+a[l+1])){
- 18 m = l+1;
- 19 break;
- 20 }
- 21 }
- 22 }
- 23
- 24 printf("为该年的 %d 月 %d 日 \n", *(pmonth+m), *(pday+yearday-sum-1));
- 25
- 26
- 27 }
- 28
- 29 int main()
- 30 {
- 31 int year, yearday, i, j;
- 32 int month[12], day[31];
- 33 int *pmonth = month;
- 34 int *pday = day;
- 35
- 36 printf("请输入年份及日子:\n");
- 37 scanf("%d %d", &year, &yearday);
- 38
- 39 for(i = 0; i < 12; i++){
- 40 *(pmonth+i) = i+1;
- 41 }
- 42 for(j = 0; j < 31; j++){
- 43 *(pday+j) = j+1;
- 44 }
- 45
- 46 Monthday(year, yearday, pmonth, pday);
- 47
- 48
- 49 return 0;
- 50 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。