第一次小组总结
程序1:判断闰年
描述
判断某年是否是闰年。
输入
输入只有一行,包含一个整数 a(0 < a < 3000)
输出
一行,如果公元a年是闰年输出 Y,否则输出N
样例输入
2006
样例输出
N
提示
公历纪年法中,能被 4整除的大多是闰年,但能被 100整除而不能被400整除的年份不是闰年, 能被3200 整除的也不是闰年,如 1900年是平年,2000 年是闰年, 3200年不是闰年。
代码:
/*公历纪年法中,能被4整除的大多是闰年,但能被100整除而不能被400整除的年份不是闰年,
能被3200整除的也不是闰年,如1900年是平年,2000年是闰年,3200年不是闰年。*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int t;
printf("input number:\n");
scanf("%d",&t);
if(t%4==0)
{
if(t%100==0&&t%400!=0||t%3200==0)
printf("N");
else
printf("Y");
}
else
printf("N");
system("pause");
}
程序二:鸡兔同笼
描述
一个笼子里面关了鸡和兔子(鸡有 2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数 a,问笼子里面至少有多少只动物,至多有多少只动物
输入
第1 行是测试数据的组数 n,后面跟着n 行输入。每组测试数据占 1行,每行一个正整数a (a < 32768)
输出
输出包含n 行,每行对应一个输入 ,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开
如果没有满足要求的答案,则输出两个 0。
样例输入
2
3
20
样例输出
0 0
5 10
/*
一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。
已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,
至多有多少只动物
*/
#include<stdio.h> /**c语言标准输入输出库***/
#include<stdlib.h>
main()
{
int i=0,n,*p;
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
for (i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
{
if (p[i]%2!=0)
printf("0 0\n");
else if (p[i]%4==2)
printf("%d %d\n",p[i]/4+1,p[i]/2);
else printf("%d %d\n",p[i]/4,p[i]/2);
}
system("pause");
}
程序三:求平均年龄
描述
班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。
输入
第一行有一个整数n( 1<= n <= 100),表示学生的人数。其后 n行每行有1 个整数,取值为 15到25 。
输出
输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。
样例输入
2
18
17
样例输出
17.50
提示
要输出浮点数、双精度数小数点后 2位数字,可以用下面这种形式:
printf("%.2f", num);
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i;int *a;int sum=0;
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("%.2f",(float)sum/n);
}
Hangover
描述
How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs the third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.
输入
The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.
输出
For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.
样例输入
1.00
3.71
0.04
5.19
0.00
样例输出
3 card(s)
61 card(s)
1 card(s)
273 card(s)
分析:
卡片
可延伸出桌面长度之和(top——>bottom)
1
1/2
2
1/2+1/3
n
1/2+1/3+1/4+...+1/(n+1)
是个递归问题,可以看作每增加一张卡片将其放入已有卡片的最底下,可延伸出长度为1/(n+1)
代码:
#include<stdio.h>
#include<stdlib.h>
float len(int n)
{
if(n>1)
return (len(n-1)+1.0/(n+1));
else
return 0.5;
}//递归函数
int main()
{
float c;int i;
while(scanf("%f",&c)!=EOF)
{
if(c==0)
break;
for (i=1;i<300;i++)
{
if(len(i)>=c) //调用递归函数
{
printf("%d card(s)",i);
break;
}
}
}
return 0;
}
装箱问题
描述
一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6。这些产品通常使用一个 6*6*h 的长方体包裹包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量。他们很需要有一个好的程序帮他们解决这个问题从而节省费用。现在这个程序由你来设计。
输入
输入文件包括几行,每一行代表一个订单。每个订单里的一行包括六个整数,中间用空格隔开,分别为1*1至6*6这六种产品的数量。输入文件将以6个0组成的一行结尾。
输出
除了输入的最后一行6个0以外,输入文件里每一行对应着输出文件的一行,每一行输出一个整数代表对应的订单所需的最小包裹数。
样例输入
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
样例输出
2
1