数据的格式化输出
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x;
float y;
double z;
cin>>x>>y>>z;
printf("%4d\n%.2f\n%.6lf\n",x,y,z);
return 0;
}
财务管理
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
float s=0,t;
for (int i=0;i<12;i++)
{cin>>t;s+=t;}
s/=12;
printf("$%.2f\n",s);
return 0;
}
计算多项式的值
#include <stdio.h>
using namespace std;
double x,a,b,c,d;
int main()
{
scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d);
printf("%.7lf",a*x*x*x+b*x*x+c*x+d);
return 0;
}
计算线段的长度
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
printf("%.3lf",sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
return 0;
}
三个数的和、平均值、积、最小值、最大值
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
printf("%d %d %d ",a+b+c,(a+b+c)/3,a*b*c);
if (a>b)
if (a>c)
if (b>c)
printf("%d %d",c,a);
else
printf("%d %d",b,a);
else
printf("%d %d",b,c);
else
{
if (b>c)
if (a>c)
printf("%d %d",c,b);
else
printf("%d %d",a,b);
else
printf("%d %d",a,c);
}
system("pause");
return 0;
}
成绩判断
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int m;
cin>>m;
if (m>=95) cout<<1;
else if (m>=90) cout<<2;
else if (m>=85) cout<<3;
else if (m>=80) cout<<4;
else if (m>=70) cout<<5;
else if (m>=60) cout<<6;
else cout<<7;
system("pause");
return 0;
}
计算邮资
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int x,y=8;
char c;
cin>>x>>c;
if (x>1000)
y+=ceil((x-1000)/500.0)*4;
if (c=='y')
y+=5;
cout<<y<<endl;
return 0;
}
相等的整数
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if ((a==b)||(b==c)||(c==a))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
system("pause");
return 0;
}
点和正方形的关系
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
if ((x<=1)&&(x>=-1)&&(y<=1)&&(y>=-1))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
return 0;
}
苹果和虫子2
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int n,x,y,z;
cin>>n>>x>>y;
if (y>=n*x)
cout<<"0"<<endl;
else
cout<<n-ceil(1.0*y/x)<<endl;
return 0;
}
分段函数问题
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
float x;
cin>>x;
if ((0 <=x)&&(x < 5))
printf("%.3f",-x+2.5);
else if ((5 <= x)&&(x < 10))
printf("%.3f",2-1.5*(x-3)*(x-3));
else if ((10 <= x)&&(x <20))
printf("%.3f",x/2-1.5);
return 0;
}
鸡兔同笼
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int m;
cin>>m;
if (m%2==1) cout<<"0 0"<<endl;
else
cout<<ceil(m/4.0)<<" "<<m/2<<endl;
return 0;
}
求一元二次方程的根
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
double a,b,c,delta;
cin>>a>>b>>c;
delta = b*b-4*a*c;
if (a!=0)
{
if (delta==0) printf("x1=x2=%.5f",-b/(2*a));
else if (delta>0) printf("x1=%.5f;x2=%.5f",(-b+sqrt(delta))/(2*a),(-b-sqrt(delta))/(2*a));
else if (b==0) printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",0.0,fabs(sqrt(-delta)/(2*a)),0.0,fabs(sqrt(-delta)/(2*a)));
else printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",-b/(2*a),fabs(sqrt(-delta)/(2*a)),-b/(2*a),fabs(sqrt(-delta)/(2*a)));
}
system( "PAUSE ");
return 0;
}
月之天数
#include <iostream>
using namespace std;
int main()
{
int month[]={31,28,31,30,31,30,31,31,30,31,30,31},mon,year;
cin>>year>>mon;
if (mon!=2)
cout<<month[mon+1]<<endl;
else if ((year%4==0)&&(year%100!=0)||(year%400==0))
cout<<29<<endl;
else
cout<<month[1]<<endl;
return 0;
}
奇偶数判断
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int x;
cin>>x;
if (x%2==0) cout<<"even"<<endl;
else cout<<"odd"<<endl;
system( "PAUSE");
return 0;
}
判断数正负
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
long long x;
cin>>x;
if (x>0) cout<<"positive";
else if (x==0) cout<<"zero";
else cout<<"negative";
system("PAUSE");
return 0;
}