C/C++之练习题4-5

时间:2023-02-23 23:55:20
#include<stdio.h>
#include<stdlib.h>
#include<math.h>


    int main(){
           /*1.大小月
           *2.闰年否? 
           */

            int year,month,day,leap=2,sum=0,number = 30;
             printf("请输入年月日\n");
              printf("公元\n");
             scanf("%d",&year ); 
             printf("年\n");
             scanf("%d",&month,&day);
             printf("月\n"); 
             scanf("%d",&day); 
               printf("日\n"); 
              if(year%400==0 ||((year%4==0)&&(year%100)!=0)){
                             leap = 1;
                             }

                  switch(month)
                  {
                               case 1:
                                    sum = day;
                                    break;
                               case 2:
                                    sum = day+31;
                                    break;
                                case 3:
                                     sum =(month-1)*30-leap+day+1;
                                     break;   
                                case 4:
                                     sum =(month-1)*30-leap+day+2;
                                     break;  
                                case 5:
                                     sum =(month-1)*30-leap+day+2;
                                     break;  
                                case 6:
                                     sum =(month-1)*30-leap+day+3;
                                     break;   
                                case 7:
                                     sum =(month-1)*30-leap+day+3;
                                     break; 
                                 case 8:
                                     sum =(month-1)*30-leap+day+4;
                                     break;   
                                  case 9:
                                     sum =(month-1)*30-leap+day+5;
                                     break;
                                 case 10:
                                     sum =(month-1)*30-leap+day+5;
                                     break; 
                                 case 11:
                                     sum =(month-1)*30-leap+day+6;
                                     break; 
                                 case 12:
                                     sum =(month-1)*30-leap+day+6;
                                     break;                                    
                          }


              printf("为公元%d年%d月%d日的第%d天\n",year,month,day,sum);
               system("pause");
                return 0;

        }

C/C++之练习题4-5
改进点:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


    int main(){
           /*1.大小月
           *2.闰年否? 
           */

            int year,month,day,leap=2,sum=0,number = 30;
             printf("请输入年月日\n");
              printf("公元\n");
             scanf("%d",&year ); 
             printf("年\n");
             scanf("%d",&month,&day);
             printf("月\n"); 
             scanf("%d",&day); 
               printf("日\n"); 
              if(year%400==0 ||((year%4==0)&&(year%100)!=0)){
                             leap = 1;
                             }

                  switch(month)
                  {
                               case 1:
                                    sum = day;
                                    break;
                               case 2:
                                    sum = day+31;
                                    break;
                                case 3:
                                     sum =(month-1)*30-leap+day+1;
                                     break;   
                                case 4:

                                case 5:
                                     sum =(month-1)*30-leap+day+2;
                                     break;  
                                case 6:

                                case 7:
                                     sum =(month-1)*30-leap+day+3;
                                     break; 
                                 case 8:
                                     sum =(month-1)*30-leap+day+4;
                                     break;   
                                  case 9:

                                 case 10:
                                     sum =(month-1)*30-leap+day+5;
                                     break; 
                                 case 11:

                                 case 12:
                                     sum =(month-1)*30-leap+day+6;
                                     break;                                    
                          }


              printf("为公元%d年%d月%d日的第%d天\n",year,month,day,sum);
               system("pause");
                return 0;

        }

【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


    int main(){
          /** 1.主要是比较如何比较最好~ */
           int x,y,z;
          printf("请输入是三个数\n");
    // scanf("%d,%d,%d",&x,&y,&z);

          /*开始比较大小 */
          int a[3],i;
          for(i= 0;i<3;i++){
                  scanf("%d",&x);
                  a[i]= x;
                 }
                 int tep;
           for(i= 1;i<3;i++){
                for(j=i;j<3;j++){
                 if( a[i-1]>a[j]){
                    tep = a[i-1];
                     a[i-1]=a[j];
                     a[j] = tep;
                     }
                     }

                 }
           for(i= 0;i<3;i++){
               printf("%d\t",a[i]);
                 }


               system("pause");
                return 0;

        }

C/C++之练习题4-5

用指针

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

    int funtion(int *a[]){

         int *tep,i,j;
         for(i=1;i<3;i++){
               for(j = i;j<3;j++){
                 if(* a[i-1]>*a[j]){
                    *tep = *a[i-1];
                    * a[i-1]=*a[j];
                    * a[j] =* tep;
                     }
                     }
                 }

                 return **a;
        }

    int main(){
          /** 1.主要是比较如何比较最好~ */
           int x,y,z;
          printf("请输入是三个数\n");
    //      scanf("%d,%d,%d",&x,&y,&z);

          /*开始比较大小 */
          int a[3],i;
          int *ptr[3],*t[3];
          for(i= 0;i<3;i++){
                  scanf("%d",&x);
                  a[i]= x;
                   ptr[i]= &a[i];
                 }

                funtion(ptr);

           for(i= 0;i<3;i++){
               printf("%d\t",*ptr[i]);
                 }

           printf("\n");
               system("pause");
                return 0;

        }