习题复习(一)!

1. 打印任意范围内的素数

#include<iostream>
using namespace std;
int main()
{
	int num,i, j;
	while (cin >> num)
	{
		for (i=0;i<num;i++)
		{
			for (j = 2; j < i; j++)
			{
				if (i%j == 0)
					break;
			}
			if (j==i)
			{
				cout << j << " ";
			}
		}
		cout << endl;
	}
	return 0;
}

2. 输出乘法口诀表

#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 << "  ";
		}
		cout << endl;
	}
	return 0;
}

3. 判断1000年—2000年之间的闰年

#include<iostream>
using namespace std;
int main()
{
	int year;
	while (cin>>year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			cout << year << "是闰年" << endl;
		}
		else
			cout << year << "不是闰年" << endl;
	}
	return 0;
}

4.给定两个整形变量的值,将两个值的内容进行交换
不允许创建临时变量,交换两个数的内容(附加题)

#include<iostream>
using namespace std;
void method1(int &a,int &b)//方法一
{
	int temp = a;
	a = b;
	b = temp;
}
void method2(int &a, int &b)//方法二
{
	a = a + b;
	b = a - b;
	a = a - b;
}
void method3(int &a, int &b)//方法三
{
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
}
int main()
{
	int a, b;
	while (cin >> a >> b )
	{
		cout <<"换前:"<< "a=" << a << "  " << "b=" << b << endl;
		method2(a, b);
		cout <<"换后:"<<"a="<<a<<"  "<<"b="<<b<< endl;
	}
	return 0;
} 

5.求10 个整数中最大值

#include<iostream>
using namespace std;
int main()
{
	int arr[10];
	while (1)
	{
		for (int i = 0; i < 10; i++)
			cin >> arr[i];
		int max = arr[0];
		for (int i=1;i<10;i++)
		{
			if (max < arr[i])
				max = arr[i];
		}
		cout << max << endl;
	}
	return 0;
}

6.将三个数按从大到小输出

#include<iostream>
using namespace std;
int main()
{
	int a, b, c;
	while(cin>>a>>b>>c)
	{
		int arr[3] = { a,b,c };
		for (int i=0;i<2;i++)
		{
			for (int j=0;j<2-i;j++)
			{
				if (arr[j]<arr[j+1])
				{
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}

 - List item

			}
		}
		cout << arr[2] << arr[1] << arr[0] << endl;
	}
	return 0;
}

7.求两个数的最大公约数

#include<iostream>
using namespace std;
int main()
{
	int a, b, c;
	while (cin>>a>>b)
	{
		int size = 0;
		if (a > b)
			size = a;
		else
			size = b;
		for (c = size; c >1; c--)
		{
			if (a%c == 0 && b%c == 0)
			{
				cout <<"最大公约数是:"<< c << endl;
				break;
			}
			if ((c==2&&a%2!=0)||(c == 2 && b % 2 != 0))
			{
				cout << "没有最大公约数!" << endl;
				break;
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值