c、c++考试代码

这篇博客包含了多个C/C++编程练习题目,包括根据成绩输出等级,判断闰年,求二次方程的根,检查整数的奇偶性和正负,计算不同购货金额的折扣,以及实现不同排序算法和数组操作。同时讨论了求阶乘和求最大公约数、最小公倍数的方法。
摘要由CSDN通过智能技术生成

输入学生成绩,要求输出成绩等级,90分及以上为’A’,80-90分为‘B’,70-79分为‘C’,60-69分为‘D’,60分以下为‘E’

#include<iostream>
using namespace std;
void main()
{
   
   int s;
   cout<<"input s:"<<endl;
   cin>>s;
   if(s<0||s>100)
	  cout<<"error!"<<endl;
   else
   if(s>=90)
      cout<<"A"<<endl;
   else if(s>=80)
	      cout<<"B"<<endl;
        else if(s>=70)
	        	cout<<"C"<<endl;
	    	else if(s>=60)
	            	cout<<"D"<<endl;
	        	else
				    cout<<"E"<<endl;
}
	  
  

#include<iostream>
using namespace std;
void main()
{
   
  int s;
  cout<<"input s:"<<endl;
  cin>>s;
  if(s<0||s>100)
    cout<<"error!"<<endl;
  else
    if(s<90)
	  if(s<80)
	    if(s<70)
		   if(s<60)
		     cout<<"E"<<endl;
		   else
		     cout<<"D"<<endl;
		else
		  cout<<"C"<<endl;
	  else
	    cout<<"B"<<endl;
	else
	  cout<<"A"<<endl;

}

习题2.
判断某一年是闰年

#include<iostream>
using namespace std;
void main()
{
   
  int y,flag;
  cout<<"input y."<<endl;
  cin>>y;
  if(y%4==0)
     if(y%100==0)
	    if(y%400==0)
		   flag=1;
		else
		  flag=0;
	 else
	   flag=1;
  else
    flag=0;
  if(flag==1)
	  cout<<y<<"年是闰年。"<<endl;
  else
	  cout<<y<<"年是平年。"<<endl;
}
#include<iostream>
using namespace std;
void main()
{
   
  int y,flag;
  cout<<"input y:"<<endl;
  cin>>y;
  if(y%4!=0)
	  flag=0;
  else if(y%100!=0)
	  flag=1;
  else if(y%400!=0)
	  flag=0;
       else
	  flag=1;
 if(flag==1)
	  cout<<y<<"是闰年。"<<endl;
 else
	  cout<<y<<"是平年。"<<endl;
}

习题3.
求方程ax^2+bx=c=0的根

#include<iostream>
#include<math.h>
using namespace std;
void main()
{
   
  float a,b,c,dt,x1,x2,p,q;
  cout<<"input a,b,c"<<endl;
  cin>>a>>b>>c;
  if(a==0)
  {
   
      x1=-c/b;
	  cout<<"x="<<x1<<endl;

  }
  else
  {
   
     dt=b*b-4*a*c;
	 p=-b/(2*a);
	 q=sqrt(fabs(dt))/(2*a);

	 if(dt>0)
	 {
   
	   x1=p+q;
	   x2=p-q;
	   cout<<"x1="<<x1<<endl;
	   cout<<"x2="<<x2<<endl;

	 }
	 else if(dt==0)
	      {
   
		     x1=p;
			 cout<<"x1=x2="<<x1<<endl;
		  }
		  else
		  {
   
		     cout<<"x1="<<p<<"+"<<q<<"*i"<<endl;     //cout<<"x1=p+q*i"<<endl;提前写好要输出的语句
			 cout<<"x2="<<p<<"-"<<q<<"*i"<<endl;    //cout<<"x2=p-q*i"<<endl;

		  }
  }

}

习题4.
判断整数的正负性跟奇偶性

先判断奇偶后正负性

#include<iostream>
using namespace std;
void main()
{
   
  int x;
  cout<<"input x:"<<endl;
  cin>>x;

  if(x%2==0)
    {
   
	  if(x>0)
	    cout<<x<<"是正偶数。"<<endl;
	  else if(x<0)
	         cout<<x<<"是负偶数。"<<endl;
	       else
	         cout<<"x=0"<<endl;
	}
  else
	{
   
	   if(x>0)
	     cout<<x<<"是正奇数。"<<endl;
	   else
	     cout<<x<<"是负奇数。"<<endl;
	}
  

}

先判断正负性后奇偶性

#include<iostream>
using namespace std;
void main()
{
   
  int x;
  cout<<"input x:"<<endl;
  cin>>x;

  if("x%2==0")
    {
   
	  if(x>0)
	    cout<<x<<"是正偶数。"<<endl;
	  else if(x<0)
	         cout<<x<<"是负偶数。"<<endl;
		   else
		     cout<<"x=0"<<endl;
	}
  else
    {
   
	  if(x>0)
	    cout<<x<<"是正奇数。"<<endl;
	  else
	    cout<<x<<"是负奇数。"<<endl;
	}
}

例2:商店打折售货,购货金额数量越大,折扣越大,具体标准为(m:购货金额,d:折扣率)
m<250(元) d=0%
250<=m<500 d=5%
500<=m<1000 d=7.5%
1000<=m<2000 d=10%
m>=2000 d=15%
从键盘输入购货金额,计算实付的金额。

#include<iostream>
using namespace std;
void main()
{
    
  float m,f,d;  //m:购货金额 d:折扣率 f:实付金额
  int c;
  cout<<"input m:"<<endl;  //f算出来d已给值,只需输入m值
  cin>>m;
  if(m>=0)  //if  else  情况你也可能不给人家钱
   {
   
  c=m/250;
  switch(c)  //switch(必须是整数)写法1:  switch(m/250) m实形,导致最终c不是整型写法2: switch(int (m/250) )强制转换,可以
   {
   
    case 0:d=0;break;
	case 1:d=0.05;break;  //输入d需注意转换%
	case 2:
	case 3:d=0.075;break;
	case 4:
	case 5:
	case 6:
	case 7:d=0.1;break;
	 default:d=0.15;
}
   f=m*(1-d);
   cout<<"f="<<f<<endl;
  }
  else 
    cout<<"error!"<<endl;
}

习题1:

从键盘输入一个年份和月份,判断该月有多少天。

月份多少天:
31天: 1,3,5,7,8,10,12
30天: 4,6,9,11
2月 :闰年 29天 ; 平年28天
判断是否为闰年
y%4 == 0 不成立,平年
y%100 == 0 不成立,闰年
y%400==0 成立,闰年;不成立平年

#include<iostream.h>
void main()
{
   
  int y,m,d;
  cout<<"input y,m:"<<endl;
  cin>>y>>m;
  switch(m)
  {
   
    case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:d=31;break;
	case 4:
	case 6:
	case 9:
	case 11:d=30;break;
	case 2:
	      {
   
		    if(y%4==0)
			  if(y%100==0)
				 if(y%400==0)
					d=29;
				 else
				   d=28;
			  else
				d=29;
			else
			  d=28;
		  }break;
	default:cout<<"input error!"<<endl;
  }
  cout<<y<<"年"<<m<<"月有"<<d<<"天。"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值