zoukankan      html  css  js  c++  java
  • 习题4

    1、  编程实现任意输入一个整数,判断是奇数还是偶数。

    #include <stdio.h>

    int main()

    {        

    int a;

    printf("请输入一个整数:");

    scanf("%d",&a);

    if(a%2==0)

               printf("偶数\n");

    else

               printf("奇数\n");

    }

    2、  编程实现求任意输入的三个整数的最大数。

    #include <stdio.h>

    int main()

    {        

    int a,b,c;

    printf("请输入三个数:");

    scanf("%d%d%d",&a,&b,&c);

    if(a>b && a>c)

               printf("最大为:%d",a);

    else if(b>a && b>c)

               printf("最大为:%d\n",b);

    else if(c>a && c>b)

               printf("最大为:%d\n",c);

    }        

    3、  如果一商场原定自行车每辆350元,网球拍每幅270元,但为了促销,星期一到星期五将网球拍按九五折销售,而星期六和星期日将自行车按九折、网球拍按八五折销售。编写程序,根据输入的星期数,计算自行车和网球拍的实际销售金额。

    #include <stdio.h>

    int main()

    {        

    double a=350,b=270,m,n;//a为自行车,b为网球拍,m自行车实际销售额,n网球拍实际销售额

    int x;

    printf("今天星期几:");

    scanf("%d",&x);

    if(x>6)

    {

               m=350*0.9;

               n=270*0.85;

               printf("星期六、星期日自行车销售额:%lf\n网球拍销售额:%lf\n",m,n);

    }

    else

    {       

               n=270*0.95;

               printf("星期1-5网球拍销售额:%lf\n",n);

    }

    }

    4、猜数游戏:随机产生6位正整数,用户对应位置数字全猜对获得1等奖,猜对5个或4个获得2等奖,猜对3个或2个获得3等奖,猜对1个获得鼓励奖,如果均没猜到,则没有中奖。

    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

    int main()

    {        

    int m;

    srand((unsigned)(time(NULL)));

    m = rand()%900000+100000;

    printf("随机数为%d\n",m);

    int a,b,c,d,e,f;

    a = m / 100000;

    b = m % 100000 / 10000;

    c = m % 10000 / 1000;

    d = m % 1000 / 100;

    e = m % 100 / 10;

    f = m % 10;

    int n;

    printf("请输入你猜的数\n");

    scanf("%d",&n);

    int x1,x2,x3,x4,x5,x6;

    x1 = n / 100000;

    x2 = n % 100000 / 10000;

    x3 = n % 10000 / 1000;

    x4 = n % 1000 / 100;

    x5 = n % 100 / 10;

    x6 = n % 10;

    int count;

    if(a == x1)

    {

               count++;

    }

    if(b == x2)

    {

               count++;

    }

    if(c == x3)

    {

               count++;

    }

    if(d == x4)

    {

               count++;

    }

    if(e == x5)

    {

               count++;

    }

    if(f == x6)

    {

               count++;

    }

    if(count==6)

               printf("一等奖");

    else if(count==4 || count==5)

               printf("二等奖");

    else if(count==3 || count==2)

               printf("三等奖");

    else if(count==1)

               printf("鼓励奖");

    else

               printf("抱歉,您没有中奖");

                         

    }

     

    5、编程实现输入年份和月份,输出该年份和月份的天数。

    (if多分支语句实现,要考虑输入年份和月份的正确性)

    #include <stdio.h>

    int main()

    {    

          int year,month;

          printf("请输出年份和月份:");

          scanf("%d%d",&year,&month);

          if(year<=0)

          {

                   printf("年份是错误的\n");

                   return 0;

          }

          if(month <= 0 || month >12)

          {

                   printf("月份是错误的\n");

                   return 0;

          }

          if(((year%4==0)&&(year%100!=0))||(year%400==0))

          {

                   printf("闰年有:366天\n");

                   if(month==2)

                   printf("月份有:29天\n");

                   if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

                   printf("月份有:31天\n");

                   if(month==4||month==6||month==9||month==11)

                   printf("月份有:30\n");

          }

          else

          {

                   printf("不是闰年有:365天\n");

                   if(month==2)

                   printf("月份有:28天\n");

                   if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

                   printf("月份有:31天\n");

                   if(month==4||month==6||month==9||month==11)

                   printf("月份有:30天\n");

          }

          return 0;

    }

    小胖专属学习分享
  • 相关阅读:
    QT移植详解
    如何构建Qt4.7.0嵌入式开发环境
    Qt触摸屏、键盘的驱动[转]
    【详解】如何编写Linux下Nand Flash驱动
    QT安装 -->Error: register or shift expression expected -- `orr r3,r2,lsl#16'
    QT环境搭建常见的bug解决 -->ld: cannot find –lbootstrap 错误
    SDK支付流程
    IOS与Unity交互
    Android
    [转]个人对AutoResetEvent和ManualResetEvent的理解
  • 原文地址:https://www.cnblogs.com/xpl520/p/15689762.html
Copyright © 2011-2022 走看看