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:
                                     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;

        }

这里写图片描述
改进点:

#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;

        }

这里写图片描述

用指针

#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;

        }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wqewq19/article/details/52351738
个人分类: C/C++
想对作者说点什么? 我来说一句

C++练习题C++练习题C++练习题

2009年05月30日 32KB 下载

c++基础上机练习题

2015年08月11日 19KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭