21.12.14

用自定义函数做:

目录

因子

全局变量

求因数

美丽数字

数字反转

验证歌德巴赫猜想

求两数的最大公约数

求两数的最小公倍数

一个整数有几个奇数字,几个偶数字

按要求输出$


因子

输入1个整数n。从小到大依次输出n的所有因子。(n<20亿)

输出

升序输出n的所有因子,中间用空格隔开;之后再输出因子总个数。

#include<iostream>
using namespace std;
int sum,i;
long int ans[100000000];          //全局变量

void factor(long int n, long int m) {
	if (n % m == 0) {
		cout << m << ' ';
		ans[i] = n / m;
		i++;
		sum+=2;
	}
		
}
	
int main() {
	long int n;
	cin >> n;
	for (long int k = 2; k*k <= n; k++) {   //一半在根号
		if (k * k == n) {
			cout << k << ' ';
			sum++;
		}
		else
			factor(n, k);
	}
	while(i--) 
		cout << ans[i] << ' ';
	cout << sum << endl;
	return 0;
}

olol

全局变量

函数中,先找局部变量(由内层次到外层)(向前找),再找形参,最后找全局变量

(全局变量名和局部变量名可以重复)

求因数

1.从1到n中判断(时间复杂度o(n))

2.从1到\sqrt{n}中判断(时间复杂度o(\sqrt{n})):若n被一个小于\sqrt{n}的数a整除,

必然有另一个大于\sqrt{n}的因数b=n/a。

美丽数字

 Alice很喜欢6这个数字,如果一个整数能被6整除或者它的任何一位包含6就被认为是美丽数字,输入为一个整数a,请帮Alice计算一下小于等于a的正整数有多少美丽数字,请按行依次输出,最后输出美丽数字的个数。

#include<iostream>
using namespace std;
int sum = 0;

void beatiful(int a) {
	int b = a;
	while (b) {
		if (a % 6 == 0) {
			cout << a << endl;
			sum++;
			break;
		}
		else if (b % 10 == 6) {
			cout << a << endl;
			sum++;
			break;
		}
		else b /= 10;
	}
}

int main() {
	int a;
	cin >> a;
	for (int i = 6; i <= a; i++)
		beatiful(i);
	cout << sum <<endl;
	return 0;
}

相似题

敲七icon-default.png?t=LA92https://blog.csdn.net/weixin_60178940/article/details/121596864?spm=1001.2014.3001.5501#t1

数字反转

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形
式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零

ps: 反转后要求为一个整数

#include<iostream>
using namespace std;
int newn;
void turn(int n) {
	newn = newn * 10 + n % 10;
}

int main() {
	int n;
	cin >> n;
	do {
		turn(n);
	} while (n /= 10);
	cout << newn << endl;
	return 0;
}

若不对反转后为一个整数

#include<iostream>
using namespace std;

void turn(int n) {
	cout << n % 10;
}

int main() {
	int n;
	cin >> n;
	if (n < 0) {
		cout << '-';
		n = -n;
	}
	if (n % 10 == 0)
		n /= 10;
	do {
		turn(n);
	} while (n /= 10);
	return 0;
}

相似题:

数字反转icon-default.png?t=LA92https://blog.csdn.net/weixin_60178940/article/details/121596864?spm=1001.2014.3001.5501#t0

验证歌德巴赫猜想

验证“歌德巴赫猜想”,即:任意一个大于2的偶数均可表示成两个素数之和。(x是偶数,x <= 2000 且 x > 2)

输出这个数的所有分解形式,形式为:
x = y + z
其中x为待验证的数,y和z满足y + z = x,而且 y <= z,y和z均是素数。
如果存在多组分解形式,则按照y的升序输出所有的分解,每行一个分解表达式。

注意数和符号之间隔一个空格。

#include<iostream>
#include<cmath>
using namespace std;

bool pri(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 1;
		else
			continue;
	}
	return 0;
}

int main() {
	int n;
	cin >> n;
	for (int i = 2; i <= n/2; i++) {
		if (!(pri(i)))if (!(pri(n -i))) 
			cout << n << " = " << i << " + " << n-i << endl;
		else
			continue;
		else
			continue;
	}
	//if (pri(n))
	//	cout << "不是";
	//else cout << "是";
	return 0;
}

求两数的最大公约数

输入

两个正整数

输出

最大公约数

#include<iostream>
using namespace std;

int pri(int n,int m) {
	if (n < m) {
		int temp;
		temp = n;
		n = m;
		m = temp;
	}
	while (m) {
		int temp;
		temp = n % m;
		n = m;
		m = temp;
	}
	return n;
}

int main() {
	int n,m;
	cin >> n>>m;
	cout << pri(n,m) << endl;
	return 0;
}

求两数的最小公倍数

#include<iostream>
using namespace std;

int pri(int n,int m) {
	if (n < m) {
		int temp;
		temp = n;
		n = m;
		m = temp;
	}
	while (m) {
		int temp;
		temp = n % m;
		n = m;
		m = temp;
	}
	return n;
}

int main() {
	int n,m,sum;
	cin >> n>>m;
	sum = n * m / pri(n, m);
	cout << sum << endl;
	return 0;
}

一个整数有几个奇数字,几个偶数字

输入

一个整数

输出

奇数字个数
偶数字个数

ps:正数要足够大

#include<iostream>
using namespace std;
int odd=0,even=0;

void Parity(long int n) {
	if (n % 2)
		odd++;
	else
		even++;
}
	
int main() {
	long int a;
	cin >> a;
	if(a==0)
		Parity(a);
	while (a) {
		Parity(a);
		a /= 10;
	}
	cout<<odd<<endl<<even;
	return 0;
}

按要求输出$

从键盘输入正整数n,输出n行如下图形:

$

$$

$$$

$$$$

.......

.......

#include<iostream>
using namespace std;

void mon(int n) {
	for (int i = 1; i <= n; i++) {
		cout << '$';
	}
	cout<<endl;
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		mon(i);
	}
	return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值