分段函数
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float n,m,y;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>m;
if(m>=0&&m<2)
{
y=-m+2.5;
cout<<fixed<<setprecision(1)<<"y="<<y<<endl;
}
else if(m>=2&&m<4)
{
y=2-1.5*(m-3)*(m-3);
cout<<fixed<<setprecision(1)<<"y="<<y<<endl;
}
else if(m>=4&&m<6)
{
y=m/2-1.5;
cout<<fixed<<setprecision(1)<<"y="<<y<<endl;
}
}
}
整数和
#include<iostream>
using namespace std;
int main()
{
int n,m,s;
cin>>n;
for(int i=0;i<n;i++)
{
s=0;
cin>>m;
if(m>=-100&&m<=100)
{
if(m>=0)
{
for(int j=m;j<=2*m;j++)
{
s=s+j;
}
cout<<s<<endl;
}
else
{
for(int j=2*m;j<=m;j++)
{
s=s+j;
}
cout<<s<<endl;
}
}
}
}
阶乘
#include<iostream>
using namespace std;
long long fun(int a)
{
if(a==0||a==1)
return 1;
else
return a*fun(a-1);
}
int main()
{
long long n,m,s;
cin>>n;
for(int i=0;i<n;i++)
{
s=0;
cin>>m;
s=fun(m);
cout<<s<<endl;
}
}
球的计算
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
#define PAI 3.1415926
int main()
{
int a,b,c,x,y,z;
int m;
float r,v;
cin>>m;
for(int i=0;i<m;i++)
{
r=0;
v=0;
cin>>a>>b>>c>>x>>y>>z;
r=(float)sqrt((a-x)*(a-x)+(b-y)*(b-y)+(c-z)*(c-z));
v=(float)4/3*PAI*r*r*r;
cout<<fixed<<setprecision(2)<<r<<" "<<v<<endl;
}
}
学生查询
#include<iostream>
using namespace std;
struct student{
int num;
string name;
string sex;
int old;
};
int main()
{
int n,m,c;
cin>>c;
for(int j=0;j<c;j++)
{
cin>>n;
struct student stu[21];
for(int i=1;i<=n;i++)
{
cin>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].old;
}
cin>>m;
for(int i=1;i<=n;i++)
{
if(m==stu[i].num)
{
cout<<stu[m].num<<" "<<stu[m].name<<" "<<stu[m].sex<<" "<<stu[m].old<<endl;
break;
}
}
}
}
计算天数
#include<iostream>
using namespace std;
struct date{
int year;
int month;
int day;
};
int main()
{
int m;
cin>>m;
struct date a[m];
for(int i=0;i<m;i++)
{
cin>>a[i].year>>a[i].month>>a[i].day;
}
for(int i=0;i<m;i++)
{
if(a[i].year%400==0||((a[i].year%4==0)&&(a[i].year%100!=0)))
{
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int s=0;
for(int j=0;j<a[i].month-1;j++)
{
s=s+b[j];
}
s=s+a[i].day;
cout<<s<<endl;
}
else{
int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int s=0;
for(int j=0;j<a[i].month-1;j++)
{
s=s+b[j];
}
s=s+a[i].day;
cout<<s<<endl;
}
}
}
一元二次方程
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
class time1{
private :
int a;
int b;
int c;
public:
time1(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
}
void find(int a,int b,int c)
{
float x1,x2;
x2=(-b+sqrt(b*b-4*a*c))/(2*a);
x1=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<fixed<<setprecision(2)<<"x1="<<x1<<","<<"x2="<<x2<<endl;
}
void find1(int a,int b,int c)
{
float x;
x=((-b+sqrt(b*b-4*a*c))/(2*a));
cout<<fixed<<setprecision(2)<<"x="<<x<<endl;
}
void find2(int a,int b,int c)
{
float x=-c/b;
cout<<fixed<<setprecision(2)<<"x="<<x<<endl;
}
};
int main()
{
int m;
cin>>m;
int a,b,c;
for(int i=0;i<m;i++)
{
cin>>a>>b>>c;
time1 s(a,b,c);
if(a==0)
{
s.find2(a,b,c);
}
else if(sqrt(b*b-4*a*c)>0)
{
s.find(a,b,c);
}
else if(sqrt(b*b-4*a*c)==0)
{
s.find1(a,b,c);
}
else if((b*b-4*a*c)<0)
cout<<"-1"<<endl;
}
}
多项式的值
#include<iostream>
#include<math.h>
using namespace std;
class item{
private:
int a;
public:
item(int a){
this->a=a;
}
int getvalue()
{
return a;
}
};
int main()
{
int m,n,s,s1[11],x,sum;
cin>>m;
for(int i=0;i<m;i++)
{
sum=0;
cin>>n;
for(int j=0;j<n+1;j++)
{
cin>>s;
s1[j]=s;
}
cin>>x;
item pp=item(x);
for(int j=0;j<n+1;j++)
{
sum=sum+s1[j]*pow(pp.getvalue(),j);
}
cout<<sum<<endl;
}
}
日期类
#include<iostream>
using namespace std;
class date{
private:
int a;
int b;
int c;
public:
date(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
}
void judge(int a,int b,int c)
{
if(b<=9&&c<=9)
{
cout<<a<<"-"<<"0"<<b<<"-"<<"0"<<c<<endl;
}
else if(b<=9&&c>9)
{
cout<<a<<"-"<<"0"<<b<<"-"<<c<<endl;
}
else if(b>9&&c<=9)
{
cout<<a<<"-"<<b<<"-"<<"0"<<c<<endl;
}
else
{
cout<<a<<"-"<<b<<"-"<<c<<endl;
}
}
void judge1(int a,int b,int c)
{
int aa[6]={1,3,5,7,8,10};
int bb[5]={4,6,9,11};
for(int i=0;i<6;i++)
{
if(aa[i]==b)
{
if(c==31)
{
c=1;
b=b+1;
judge(a,b,c);
break;
}
else
{
c=c+1;
judge(a,b,c);
break;
}
}
else if(bb[i]==b)
{
if(c==30)
{
c=1;
b=b+1;
judge(a,b,c);
break;
}
else
{
c=c+1;
judge(a,b,c);
break;
}
}
else if(b==12)
{
if(c==31)
{
c=1;
b=1;
a=a+1;
judge(a,b,c);
break;
}
else
{
c=c+1;
judge(a,b,c);
break;
}
}
else if(b==2)
{
if(c==28)
{
c=1;
b=b+1;
judge(a,b,c);
break;
}
else
{
c=c+1;
judge(a,b,c);
break;
}
}
}
}
};
int main()
{
int m,a,b,c;
date p(a,b,c);
cin>>m;
for(int i=0;i<m;i++)
{
cin>>a>>b>>c;
p.judge1(a,b,c);
}
}
重载运算符
#include<iostream>
#include<math.h>
#include<iomanip>
#define PI 3.1415926
using namespace std;
class angle{
private:
int a;
public:
void seta(int a)
{
this->a=a;
}
int geta()
{
return a;
}
angle operator-(const angle& x)
{
angle aa;
aa.a=this->a-x.a;
return aa;
}
};
int main()
{
int m,a,b;
cin>>m;
angle box1;
angle box2;
angle box3;
float y;
for(int i=0;i<m;i++)
{
cin>>a>>b;
box1.seta(a);
box2.seta(b);
box3=box1-box2;
y=box3.geta()/float(180)*PI;//c++处理的式弧度,要把角度转换为弧度
y=sin(y);
cout<<fixed<<setprecision(2)<<y<<endl;
}
}
复数
#include<iostream>
using namespace std;
class finda{
private:
int a;
int b;
public:
int geta()
{
return a;
}
int getb()
{
return b;
}
void seta(int sa)
{
a=sa;
}
void setb(int sb)
{
b=sb;
}
finda operator+(const finda& x)
{
finda aa;
aa.a=this->a+x.a;
aa.b=this->b+x.b;
return aa;
}
};
int main()
{
int m,a,b,c,d;
cin>>m;
finda box1;
finda box2;
finda box3;
for(int i=0;i<m;i++)
{
cin>>a>>b>>c>>d;
box1.seta(a);
box1.setb(b);
box2.seta(c);
box2.setb(d);
box3=box1+box2;
cout<<box3.geta();
if(box3.getb()>0)
{
cout<<"+"<<box3.getb()<<"i"<<endl;
}
else
{
cout<<box3.getb()<<"i"<<endl;
}
}
}
三角形相加
#include<iostream>
using namespace std;
class CTriangle{
private:
int a;
int b;
public:
CTriangle(int a,int b)
{
this->a=a;
this->b=b;
}
int geta()
{
return a;
}
int getb()
{
return b;
}
};
int main()
{
int a,b;
int sum1=0,sum2=0;
while(1)
{
cin>>a;
if(a==0)
{
break;
}
cin>>b;
CTriangle box(a,b);
sum1=sum1+box.geta();
sum2=sum2+box.getb();
}
cout<<"A"<<"(0,"<<sum1<<")"<<","<<"B(0,0)"<<","<<"C("<<sum2<<",0)"<<endl;
}
弹地小球
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int m,n;
float H;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>H>>n;
int j=0;
float sum=0;
sum=sum+H;
while(j<n-1)
{
H=H/2;
sum=sum+H*2;
j++;
}
cout<<fixed<<setprecision(2)<<sum<<endl;
}
}
点的距离
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class CPoint{
private:
int a;
int b;
public:
void set(int a,int b)
{
this->a=a;
this->b=b;
}
CPoint operator-(const CPoint &x)
{
CPoint cp;
cp.a=this->a-x.a;
cp.b=this->b-x.b;
return cp;
}
int geta()
{
return a;
}
int getb()
{
return b;
}
};
int main()
{
int m,a,b,c,d;
float sum=0;
CPoint aa,bb,dd;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>a>>b>>c>>d;
aa.set(a,b);
bb.set(c,d);
dd=aa-bb;
sum=sqrt(dd.geta()*dd.geta()+dd.getb()*dd.getb());
cout<<fixed<<setprecision(2)<<sum<<endl;
}
}
直角三角形
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
class CTriangle{
private:
int a;
int b;
int c;
int d;
int e;
int f;
public:
CTriangle(int a,int b,int c,int d,int e,int f)
{
this->a=a;
this->b=b;
this->c=c;
this->d=d;
this->e=e;
this->f=f;
}
void judge(int a,int b,int c,int d,int e,int f)
{
float a1,a2,a3,sum;
a1=sqrt((a-c)*(a-c)+(b-d)*(b-d));
a2=sqrt((a-e)*(a-e)+(b-f)*(b-f));
a3=sqrt((c-e)*(c-e)+(d-f)*(d-f));
if((a1*a1+a2*a2==a3*a3)||(a1*a1+a3*a3==a2*a2)||(a2*a2+a3*a3==a1*a1))
{
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
sum=a1+a2+a3;
cout<<fixed<<setprecision(2)<<sum<<endl;
}
};
int main()
{
int m,a,b,c,d,e,f;
cin>>m;
for(int i=0;i<m;i++)
{
cin>>a>>b>>c>>d>>e>>f;
CTriangle bb(a,b,c,d,e,f);
bb.judge(a,b,c,d,e,f);
}
}
编排字符串
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int m;
cin>>m;
int sum=1;
string s;
queue <string> t;
queue <string> r;
string a;
for(int i=0;i<m;i++)
{
cin>>s;
t.push(s);
int j=0;
if(t.size()>4)
{
t.pop();
}
while(j<t.size())
{
cout<<j+1<<"="<<t.back()<<" ";
while(t.size()>1)
{
a=t.front();
r.push(a);
t.pop();
}
j++;
while(r.size()>0)
{
t.push(r.front());
r.pop();
}
}
cout<<endl;
}
}
加法等式
#include<iostream>
using namespace std;
int main()
{
int s;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
for(int x=0;x<10;x++)
{
s=i*100+j*10+x+j*100+x*10+x;
if(s==532)
{
cout<<i<<" "<<j<<" "<<x<<endl;
}
}
}
}
}
完数与盈数
#include<iostream>
using namespace std;
int main()
{
int s;
int a[100],b[100];
int count=0,sum1=0;
for(int i=2;i<=60;i++)
{
s=0;
for(int j=1;j<i;j++)
{
if(i%j==0)
{
s=s+j;
}
}
if(s==i)
{
a[count]=i;
count++;
}
else if(s>i)
{
b[sum1]=i;
sum1++;
}
}
cout<<"E"<<":"<<" ";
for(int i=0;i<count;i++)
{
cout<<a[i]<<" ";
}
cout<<"G"<<":"<<" "<<"2"<<" ";
for(int i=0;i<sum1;i++)
{
cout<<b[i]<<" ";
}
return 0;
}
反序相等
#include<iostream>
using namespace std;
int main()
{
int n,a,b,c,d,i;
for( i=1000;i<10000;i++)
{
a=i/1000;
b=(i%1000)/100;
c=(i%1000)%100/10;
d=i%1000%100%10;
n=d*1000+c*100+b*10+a;
if(n==(i*9))
{
cout<<i<<endl;
}
}
}
对称平方数
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int s,sum,i;
char a[20];
stack <char> b;
for(i=1;i<256;i++)//按题目通过的话0不是对称平方数
{
s=i;
sum=s*s;
sprintf(a, "%d", sum);
for(int j=0;a[j]!='\0';j++)
{
b.push(a[j]);
}
int flag;
for(int j=0;a[j]!='\0';j++)
{
if(b.top()==a[j])
{
flag=1;
}
else
{
flag=0;
break;
}
//cout<<b.top();
b.pop();
}
if(flag==1)
{
cout<<i<<endl;
}
}
return 0;
}
整型存储
#include<iostream>
#include<stack>
using namespace std;
class finda{
private:
int a;
public:
void seta(int a)
{
this->a=a;
}
void reverse(int a)
{
stack <char> s;
char b[20];
sprintf(b,"%d",a);
for(int j=0;b[j]!='\0';j++)
{
s.push(b[j]);
}
cout<<a<<" ";
while(s.size()>0)
{
if(s.top()!='0')
{
cout<<s.top();
}
s.pop();
}
cout<<endl;
}
};
int main()
{
int m,n=0;
finda aa;
while(n!=10&&(cin>>m&&m!=0))
{
aa.seta(m);
aa.reverse(m);
n++;
}
}
邮票
#include<iostream>
#include<set>
using namespace std;
int main()
{
set <float> ss;
float s=0;
set <float>::iterator it;
for(int i=0;i<=5;i++)
{
for(int j=0;j<=4;j++)
{
for(int k=0;k<=6;k++)
{
s=0.8*i+1*j+1.8*k;
ss.insert(s);
}
}
}
cout<<ss.size()-1<<endl;
}