| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 961 人关注过本帖
标题:又有问题了,麻烦大家~
只看楼主 加入收藏
悉心
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-7-31
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:10 
又有问题了,麻烦大家~
1.编写程序,根据所输入的年份和月份,计算该月有多少天(题目需要考虑闰年和平年的情况)
2.编写程序,袋中有红、黄、绿、蓝色的球共17个,其中这4个色球的个数分别为2、5、3、7个,现从袋中随意拿出一个球,求娶到各种颜色球的概率
搜索更多相关主题的帖子: 麻烦 
2010-08-01 11:23
carmeloyin
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
帖 子:161
专家分:157
注 册:2008-12-2
收藏
得分:0 
第1题不会,因为我一直搞不明白什么情况下是闰年,鄙视我吧
第2题需要编程吗?
不就是2/17  5/17 3/17  7/17 吗?
2010-08-01 12:20
carmeloyin
Rank: 3Rank: 3
来 自:西安
等 级:论坛游侠
帖 子:161
专家分:157
注 册:2008-12-2
收藏
得分:2 
我去查了查闰年的概念
我的理解是闰年则2月有29天,不是闰年,则2月有28天
按我的理解写了下面的程序
如果我对于闰年的理解有错
那么这个程序就不用看了

#include<iostream.h>
main()
{
    int year,month;
    cout<<"请输入年和月,中间用空格分开:";
    cin>>year>>month;

    if((year%4==0&&year%100!=0)||(year%400==0))   //若为闰年
    {
        switch(month)
        {
        case(1):
        case(3):
        case(5):
        case(7):
        case(8):
        case(10):
        case(12):cout<<"31天"<<endl;break;
        case(4):
        case(6):
        case(9):
        case(11):cout<<"30天"<<endl;break;
        case(2):cout<<"29天"<<endl;break;
        default:cout<<"没这个月份"<<endl;break;
        }
    }
    else         //不是闰年
    {
        switch(month)
        {
        case(1):
        case(3):
        case(5):
        case(7):
        case(8):
        case(10):
        case(12):cout<<"31天"<<endl;break;
        case(4):
        case(6):
        case(9):
        case(11):cout<<"30天"<<endl;break;
        case(2):cout<<"28天"<<endl;break;
        default:cout<<"没这个月份"<<endl;break;
        }
    }
}

2010-08-01 12:37
pbreak
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:83
专家分:558
注 册:2007-5-10
收藏
得分:2 
第一题:
程序代码:
#include "stdafx.h"

bool IsLeapYear(unsigned int year)
{
    if( (year % 4 == 0) && (year % 100 != 0) || (year % 400 ==0) )
        return true;
    return false;
}

unsigned int GetDaysByMonth(unsigned int year,unsigned int month)
{
    if (month < 8 && month > 0)
    {
        if( month == 2)
            if( IsLeapYear(year) )
                return 29;
            else
                return 28;
        return ( month % 2 + 30);
    }
    else
        if(month > 7 && month <= 12)
            return ( (month - 1) % 2 + 30 );
    return -1;
}

int main()
{
    unsigned int year,month;
    printf("please input the year and month:(example: 2010,5)\n");
    fflush(stdin);
    scanf("%d,%d",&year,&month);
    printf("the days of month %d in the year %d is %d\n",month,year,GetDaysByMonth(year,month));
    return 0;
}

 
2010-08-01 13:01
TCsyaoran
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-7-31
收藏
得分:0 
算是见识到了,学习ing!
2010-08-01 14:17
vs_inzaghi
Rank: 5Rank: 5
来 自:湖北
等 级:职业侠客
威 望:1
帖 子:303
专家分:364
注 册:2009-8-17
收藏
得分:0 
原来,c程序也可以写的这么有创意……

我很懒,但我讨厌别人说我懒……
2010-08-01 14:37
TCsyaoran
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-7-31
收藏
得分:0 
我是小白,麻烦问下我没有上面stdafx.h头文件,用TC编译的
2010-08-01 15:33
pbreak
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:83
专家分:558
注 册:2007-5-10
收藏
得分:0 
加上
#include <stdio.h>
#include<stdlib.h>
2010-08-01 15:34
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:4 
第一题:
#include<stdio.h>
#include<stdlib.h>
int leap(int y);
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
int y,m;
scanf("%d%d",&y,&m);
if(leap(y))a[2]=29;
else a[2]=28;
printf("%d年%d月共有%d天\n",y,m,a[m]);
//system("pause");
return 0;
}
int leap(int y){
int i;
if((y%4==0&&y%100!=0)||y%400==0)i=1;
else i=0;
return i;
}
第二题:
不就是2/17  5/17 3/17  7/17
#include <stdio.h>
void main()
{int shuliang,a,b,c,d;
 printf("please input the shuliang:\n");
scanf("%d",&shuliang);
printf("please input the a:\n");
scanf("%d",&a);
printf("please input the b:\n");
scanf("%d",&b);
printf("please input the c:\n");
scanf("%d",&c);
printf("please input the d:\n");
scanf("%d",&d);
a=a/shuliang;
b=b/shuliang;
c=c/shuliang;
d=d/shuliang;
printf("各个取出的几率为:\n");
printf("a=%d%,b=%d%,c=%d%,d=%d%",a,b,c,d);
}

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-08-01 16:42
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:2 
第一道题没有理解题意,又写了一个符合题意的:
#include <stdio.h>
#include <stdlib.h>

static int daytable[2][13] = {  
  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

/* Get the days from year month day */
void get_day ( int year, int *dayofyear, int month, int day )
{
  int i, leap;
  leap = (( year % 4 == 0 ) && ( year % 100 != 0 ) || ( year % 400 == 0 ));
  *dayofyear = 0;
  for (i=1;i< month;i++) {
  *dayofyear = *dayofyear + daytable[leap][i];
  }
  *dayofyear =  *dayofyear + day;  
}

void main()
{
int year,month,day;
int days;

printf("Enter the year month day (for example: 2008 3 1)\n");
scanf("%d %d %d",&year,&month,&day);
(void) get_day ( year, &days, month, day );
printf("the days=%d\n",days);

}

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-08-01 16:44
快速回复:又有问题了,麻烦大家~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012640 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved