题目来源:洛谷,入门
目录
心得1:在实际应用题涉及计算时,切勿忽略负数的可能性。
心得2:在输入的时候不可以写"%.2f",只可以写"%f"。在输出的时候可以写"%.2f"。
心得3:精度不够会出错。
题目:苹果和虫子。M个苹果,虫子吃一个苹果耗时T,花S时间吃。吃完剩余NOW个,输入t,s求now。
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
float m,t,s,now;
cin>>m>>t>>s;
if(m>=0,t>=0,s>=0)
{
if(m<=100,t<=100,s<=10000)
{
if(s!=0 && m>=s/t)//本来在此没考虑负数,没有加入限制条件 “m>=s/t”,即:有20个苹果,1分钟吃一个,吃了30分钟,结果不能是-10
{
now=int(m-s/t);
cout<<now<<endl;
}
else
cout<<"0"<<endl;
}
}
}
心得2:
在输入的时候不可以写"%.2f",只可以写"%f"。在输出的时候可以写"%.2f"。
题目:三角形面积。输入三边,利用公式求面积。
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
double a,b,c,s;
scanf("%lf%lf%lf",&a,&b,&c);//不能写作%.2f,不然识别不出来。只能在输出时有%.2f
if(a+b>c && a+c>b && b+c>a && a>0 && a<=1000 && b>0 && b<=1000 && c>0 && c<=1000)
{
double p=1.0*1/2*(a+b+c);
s=sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.1lf",s);
return 0;
}
}
心得3:
精度不够会出错。
题目:对角线。输入n,输出n边形的对角线的焦点个数。
#include<iostream>
using namespace std;
int main()
{
unsigned long long n,ans;//精度问题:最开始使用的int,但输入数据达到四位时,输出就与正确答案不符了。
cin>>n;
ans=n*(n-1)*(n-2)*(n-3)/24;
cout<<ans<<endl;
}