1.A+B for Input-Output Practice (III)
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1001&cid=33820
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
{
if(a==0&&b==0)
break;
printf("%d\n",a+b);
}
return 0;
}
或者:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF&&(a!=0||b!=0))
{
printf("%d\n",a+b);
}
return 0;
}
2Let the Balloon Rise
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1002&cid=33820
一 分析
这道题要解决的问题很明确,也很简单,就是要统计每个字符串出现的次数,然后找出现最多的那一个。
二 算法
每次加入新的元素与之前出现的每一个进行一次比较,如果这个color之前出现过,这个color的数量就+1,如果我们仔细思考可以发现这个方法和我们人工统计的方法其实没有什么区别。
三 数据结构
确定了算法以后,我们就要根据我们设计的算法构造数据结构,根据上面算法的要求,我们需要一个能存放字符串的数组,和一个能存放次数的数组,并且这两个数组能通过其数组下标一一准确对应起来。
四 总结
1.下边的代码用了跟简单的一种方法,先把所有的字符串储存起来,再跟其之前的所有字符串相比较,最后再统计最大的次数
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,t,max,num[1000];
char color[1000][16];
while(scanf("%d",&n)!=EOF){
if(n){
num[0]=0;
scanf("%s",color[0]);
for(int i=1;i<n;i++){
num[i]=0;
scanf("%s",color[i]);
for(int j=0;j<i-1;j++)
if(strcmp(color[i],color[j])==0) //在这个数组中任意两个字符比较两个字符串;设这两个字符串为str1,str2,若str1==str2,则返回零;
num[i]+=1; //计数器加一
}
max=num[0]; //先指定第一个数为最大值
t=0;
for(int i=1;i<n;i++)
if(max<num[i]) //依次把每一个字符统计量拿来比较,找到最大的为第t个字符
{
max=num[i];
t=i;
}
printf("%s\n",color[t]);
}
}
}
3.
题意:完全是高中数学题,要我们求抛物线和直线围成的面积,抛物线方程是y=a*x^2+b*x+c,直线的方程是y=k*x+h,
已知抛物线与直线相交两点和抛物线顶点,顶点P1(-b/(2a), (4ac-b^2)/4a)。
我们已知p1,p2,p3,然后求出 a,b,c,k,h
y1=ax1^2+bx1+c
y2=ax2^2+bx2+c
y1-y2=(x1-x2)(a(x1+x2)+b)
又x1=-b/2a,
所以a=(y2-y1)/(x2-x1)^2
面积用积分公式化简为:
S=a/3*(x3^3–x2^3)+(b-k)/2*(x3^2–x2^2)+(c-h)*(x3-x2);
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n;
double x1,y1,x2,y2,x3,y3,a,b,c,k,h,s;
cin>>n;
while(n--)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3;
a=(y2-y1)/((x2-x1)*(x2-x1));
b=-2*a*x1;
c=y1-a*x1*x1-b*x1;
k=(y3-y2)/(x3-x2);
h=y2-k*x2;
s=a/3*(x3*x3*x3-x2*x2*x2)+(b-k)/2*(x3*x3-x2*x2)+(c-h)*(x3-x2);
printf("%.2f\n",s);
}
}
4.Least Common Multiple
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1004&cid=33820
分析:首先明确两个数的最小公倍数的求法,a,b两个数的最小公倍数即为两数乘积除以他们的最大公约数,那么三个数的最小公倍数即为,其中两个数的最小公倍数与第三个数的最小公倍数,以此类推,以求得多个数的最小公倍数。
一定注意,要先除后乘,以防止溢出
#include<stdio.h>
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int lcm(int a,int b)
{
return a/(gcd(a,b))*b;
}
int main()
{
int t,n,a;
scanf("%d",&t);
while((t--)>0)
{
scanf("%d",&n);
scanf("%d",&a);
while(--n)
{
int temp;
scanf("%d",&temp);
a=lcm(a,temp);
}
printf("%d\n",a);
}
}
5.18岁生日
acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1005&cid=33820
#include<stdio.h>
int leapyear(int year)
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else return 0;
//自定义函数,判断年份是闰年还是平年
}
int main()
{
int t,y,year,month,day;
int sum;
//sum用来统计从出生到18岁所经历的天数
scanf("%d",&t);
while(t--)
{
scanf("%d-%d-%d",&year,&month,&day);
if(month==2&&day==29)
printf("-1\n");
else
{
sum=0;
if(month>=3)
{
for(y=year+1;y<=year+18;y++)
{
if(leapyear(y))
sum+=366;
else
sum+=365;
}
}
else if(month<=2)
{
for(y=year;y<=year+17;y++)
{
if(leapyear(y))
sum+=366;
else
sum+=365;
}
}
printf("%d\n",sum);
}
}
return 0;
}
6.求奇数的乘积
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1006&cid=33820
#include<stdio.h>
#include<string.h>
int main()
{
int n,s[1000],sum;
while(scanf("%d",&n)!=EOF)
{
sum=1;
for(int i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
for(int j=0;j<n;j++)
{
if(s[j]%2!=0)
{
sum=sum*s[j];
}
else continue;
}
printf("%d\n",sum);
}
return 0;
}