C++ 面向对象- -一些简单练习(十)

目录

1、分解一已知数为其质因数相乘

2、数字间排序的总数

3、continue的使用(跳出循环)

4、break的使用(结束循环)

5、九九乘法表

6、统计键盘输入字符个数

7、持续输入

8、求解标准一元二次方程组

9、分解一已知数为多个连续数之和

10、规律数的计算

11、枚举变量的使用(判断周几)

12、判断一日期为该年中的第几天

13、求砝码质量

14、判断键盘输入字符属性

15、斐波那契数列(数组)


 

 

1、分解一已知数为其质因数相乘

#include <iostream>
using namespace std;
int main()
{
	int n , i ;
	cout <<"输入需要分解的正整数 :";
	cin >> n ;
	cout << n << "="  ;
	for( i=2 ; i<=n ; i++)
	{
		while( n != i)
		{
		if( n%i == 0 )
			{
				cout << i << "*" ;
				n = n/i ;	
			}
		else
			break ;
		}
	}
	cout << n <<endl;
	return 0;
 } 

 

2、数字间排序的总数

#include <iostream>
using namespace std ;
int main()
{
	int i , j , k , n=0 ;
	cout << "数字 1、2、3、4能组成的互不相同且无重复数字的三位数为 :" <<endl ;
	for(i=1 ; i<=4 ; i++)
		for(j=1 ; j<=4 ; j++)
			for(k=1 ; k<=4 ; k++){
				if(i != j && j != k && i != k)
				{
					n++ ;
					cout << i << "," << j << "," << k <<";\t";
					if(n % 6 == 0)
						cout <<endl;
				}
			}
	cout << "一共有" << n <<"个" <<endl; 
	return 0;
 } 

 

3、continue的使用(跳出循环)

#include <iostream>
#define PI 3.14
using namespace std ;				//半径在 1~5 内 
int main()							//求面积在50以上的圆面积 
{
	int r ;
	double area ;
	for(r=1 ; r<=5 ; r++)
	{
		area = PI * r * r ;
		if(area <= 50){
			cout << "半径为 :" << r << "已不符合要求" <<endl; 
			continue ;
		} 
		cout <<"半径是:" << r << '\t' << "面积为 : " << area <<endl; 
	}
	return 0 ;
}

 

4、break的使用(结束循环)

#include <iostream>
#define PI 3.14
using namespace std ;				//半径为 1~10 
int main()							//求面积在100以内的圆面积 
{
	int r ;
	double area ;
	for(r=1 ; r<=10 ; r++)
	{
		area = PI * r * r ;
		if(area > 100)
			break ;
		cout << "面积为 : " << area <<endl; 
	}
	cout << "半径为 :" << r << "已不符合要求" <<endl;
	return 0 ;
}

 

5、九九乘法表

#include <iostream>
using namespace std;
int main()
{
	int i,j;
	for (i=1;i<10;i++)
	{
		for(j=1;j<=i;j++)
			cout << j << '*' << i << '=' << i*j << '\t';
		cout << endl;
	}
	return 0;
}

 

6、统计键盘输入字符个数

#include <iostream>
using namespace std ;
int main()
{
	int n=0 ;
	cout << "input a string :" <<endl;
	while(getchar() != '\n')
		n++ ;
	cout <<"字符个数为 :" << n <<endl;
	return 0;
 } 

 

7、持续输入

#include <iostream>
using namespace std ;
int main()
{
	char c ;
	for(;(c=getchar())!='\n'; ) 
		cout << c ; 
	return 0;
 } 

 

8、求解标准一元二次方程组

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std ;
int main()
{
	float a , b , c , s , x1 , x2 ;
	double t ;
	cout << "输入需要方程的三个系数 :" ;
	cin >>a>>b>>c ;
	if (a == 0){
		if (b != 0)
			cout << "方程只有一个实数解 :" << -c/b <<endl;
		else if(c == 0)
			cout << "方程具有任意实数解" <<endl;
		else
			cout << "方程无实际意义" <<endl;
	}
	else {
		s = b*b - 4*a*c ;
		if(s == 0.0){
			cout << "方程唯一解 :" << -0.5 * b / a <<endl;
		}
		else if(s > 0.0 ){
			t = sqrt(s) ;
			x1 = -0.5 * (b + t) / a ;
			x2 = -0.5 * (b - t) / a ;
			cout << "方程有两个解 :" << '\t' << "1 :" <<x1 << '\t' <<"2 :" << x2 <<endl; 
		}
		else { 
			cout << "方程无实数解" <<endl ;
		} 
	}	
	return 0 ;
 } 

 

9、分解一已知数为多个连续数之和

/*编写一个函数,判断输入的任何一个正整数n是否等于某个连续正整数序列之和,
	若是,输出所有可能的序列,否则输出“不能分解”。
	比如,当输入100时,输出:100=9+10+11+12+13+14+15+16
							100=18+19+20+21+22
*/
#include<iostream>
using namespace std;
void DecomposeInteger(int);
int main()
{
	int n;
	cout << "Input a integer to be decomposed: ";
	cin >> n;
	DecomposeInteger(n);
}

void DecomposeInteger(int D_n)
{
	int i,j,k,sum,flag=0;
	for (i=1;i<=D_n/2;i++)
	{
		sum=0;
		for (j=i;sum<D_n;j++)
			sum+=j;
		if (sum==D_n)
		{
			cout << D_n << '=';
			for(k=i;k<j-1;k++)
				cout << k << '+';
			cout << j-1 << endl;
			flag=1;
		}
	}
	if (!flag)
		cout << D_n << " can not be decomposed." << endl;
}

 

10、规律数的计算

#include <iostream>
#include <math.h>
using namespace std; 
int main()
{
	int i , a=0 ;
	cout << "该规律数为:"<<endl;
	for(i=1 ; i<=51 ; i++)
		{
			cout <<pow(-1,(i+1))*( 2*i - 1 ) << '\t' ;
			a += pow(-1,(i+1))*( 2*i - 1 ) ;
		}
		cout<< endl ;
		cout << "该规律数求和为:" << a <<endl;
 } 

 

11、枚举变量的使用(判断周几)

#include <iostream>
using namespace std;
enum week {sun, mon, tue, wed, thu, fri, sat};
int main()
{
	enum week date ;			 //定义枚举变量
						//如同结构体(struct)和共用体(union)一样
						//枚举变量中,enum是枚举型 union是共用体,成员共用一个变量缓冲区。 
	
						//enum 枚举名{ 枚举值表 };
	
						//1. 枚举值是常量,不是变量。不能在程序中用赋值语句再对它赋值。
						
						//2. 枚举元素本身由系统定义了一个表示序号的数值,
						//从0开始顺序定义为0,1,2…。如在weekday中,sun值为0,mon值为1,sat值为6。
						
						//3.只能把枚举值赋予枚举变量,不能把元素的数值直接赋予枚举变量。
						//如一定要把数值赋予枚举变量,则必须用强制类型转换。
	int num;
	cout << "请输入一个非负整数:";
	cin >> num ;
	if(num<0 || num >6)
		cout << "输入不正确,请重新输入!"<<endl;
	else
		date = (enum week)num ;			//强制类型转换
		switch(date){
			case sun:      	//枚举类型比较
			cout << "星期日" << endl;
			break;
			case mon:
			cout << "星期一" << endl;
			break;
			case tue:
			cout << "星期二" << endl;
			break;
			case wed:
			cout << "星期三" << endl;
			break;
			case thu:
			cout << "星期四" << endl;
			break;
			case fri:
			cout << "星期五" << endl;
			break;
			case sat:
			cout << "星期六" << endl;
			break;
		}
			
}

 

12、判断一日期为该年中的第几天

#include <iostream>
using namespace std;
int isRunnian(int y) ;
int getdays(int x, int y) ;
int main()
{
	int year , month , day , days = 0 ;
	cout << "输入年份,样式如2017 05 31" <<endl;
	cin >> year >> month >> day ; 
	if(month<0 || month>12 && day<0 || day>31)
		cout << "请正确输入!"<<endl;
	days = getdays(month,day) ;
	if(isRunnian(year) && month>2)
		cout << "该日期是对应年份的第" << days+1<< "天。" <<endl;
	else 
		cout << "该日期是对应年份的第" << days<< "天。" << endl;
 } 
 int getdays(int x, int y)
 {
 	int result = y , i ;
 	int num[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
 	for(i=1 ; i<=x-1 ; i++)
 		result += num[i];
 	return result;
 }
int isRunnian(int y)
{
	return 	y%4==0 && y%100!=0 || y%400==0 ;
	
}

 

13、求砝码质量

//忘是哪个题了。。。糗 
#include <iostream>
using namespace std;
int main()
{
	int i=0,weights[10]={1},c_max=0;
	while(i<10)
	{
		c_max+=weights[i];
		weights[++i]=2*c_max+1;
	}
	for(i=0;i<4;i++)
		cout << "第" << i+1 << "块砝码质量为:" << weights[i] << "磅。" << endl;
	
	return 0;
}

 

14、判断键盘输入字符属性

#include <iostream>
using namespace std ;
int main()
{
	char c ;
	cout << "input a character :" ;
	c=getchar() ;    //表示从键盘上获取到一个输入字符
	if(c<32)
		cout << "This is a control character" <<endl ;
	else if(c>='0' && c <='9')
		cout << "This is a digit" << endl ;
	else if(c>='A' && c <='Z')
		cout << "This is a capital letter" <<endl ;
	else if(c>='a' && c<='z')
		cout << "This a samll letter" << endl ;
	else
		cout << "This is an other character" <<endl; 
	return 0;
}

 

15、斐波那契数列(数组)

#include <iostream>
using namespace std;
int main()
{
	int i , f[55] = { 1 , 1} ;
	for(i=2 ; i<55 ; i++)
		f[i] = f[i-1] + f[i-2] ;
	for(i=0 ; i<20 ; i++)
	{
		if(i % 5 == 0)
//			cout << endl;
			cout << f[i]  ;
	}
//	cout << endl;
	return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值