欧拉计划 1~10

T1 3或5的倍数
#include <bits/stdc++.h>
using namespace std;

int main() {
	int sum = 0;
	sum += (3 + 999) * (999 / 3) / 2;
	sum += (5 + 995) * (995 / 5) / 2;
	sum -= (15 + 990) * (990 / 15) / 2;

	cout << sum;
	
	return 0;
}
T2 偶斐波那契数
#include <bits/stdc++.h>
using namespace std;

int main() {
	int a = 1, b = 2, c;
	int sum = 2;
	
	while (true) {
		c = a + b;
		if (c > 4000000) break;
		if (c % 2 == 0) sum += c;
		
		a = b;
		b = c;
	}
	cout << sum;
		
	return 0;
}
T3 最大质因数
#include <bits/stdc++.h>
using namespace std;

int main() {
	long long n = 600851475143, ans;
	 
	for (int i = 2; i <= n / i; i ++) {
		while (n % i == 0) {
			ans = i;	//满足条件的 i 一定是个质数 
			n /= i;
		}
	}
	if (n > 1) ans = n;
	cout << ans;

	return 0;
}

/*
这条语句是必须的。
if (n > 1) ans = n;

举例n = 14,则循环是从2到sqrt(14),也就是从2到3
循环结束时n = 7, 

对于某些数字,比如8, 循环结束时n = 1 
*/
T4 最大回文乘积
#include <bits/stdc++.h>
using namespace std;

int main() {
	for (int i = 999; i >= 100; i --) {
		//构造一个6位回文数 
		int x = i * 1000 + i % 10 * 100 + i / 10 % 10 * 10 + i / 100;
		
		for (int j = 999; j >= 100; j --) {
			if (x % j == 0) {
				int y = x / j;
				if (y >= 100 && y <= 999) {
					cout << x;
					return 0;
				}
			}
		}		
	}	

	return 0;
}
T5 最小公倍数
#include <bits/stdc++.h>
#define LL long long
using namespace std;

int gcd(int x, int y) {
	return y ? gcd(y, x % y) : x;
}

int main() {
	int ans = 1;
	for (int i = 2; i <= 20; i ++) {
		int t = gcd(ans, i);
		ans = ans / t * i;
	}
	cout << ans;

	return 0;
}
T6 和的平方与平方的和之差
#include <bits/stdc++.h>
using namespace std;

int main() {
	int s1 = 0, s2 = 0;
	for (int i = 1; i <= 100; i ++) {
		s1 += i;
		s2 += i * i;
	}
	cout << s1 * s1 - s2;

	return 0;
}
T7 第10001个质数
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
int a[N];
int p[N], n;

int main() {
	for (int i = 2; i <= 200000; i ++) {
		if (!a[i]) {
			p[++ n] = i;
			for (int j = 2; i * j <= 200000; j ++) {
				a[i * j] = 1;
			}
		}
	}
	cout << p[10001];

	return 0;
}

/*
当n很大时,正整数 1~n 范围内的质数个数,约等于 n / ln(n)
*/
T8 连续数字最大乘积
#include <bits/stdc++.h>
using namespace std;

int a[1000];

int main() {
	string s;
	cin >> s;
	for (int i = 0; i < 1000; i ++) a[i] = s[i] - '0';
	
	long long maxx = 0;	
	for (int i = 12; i < 1000; i ++) {
		long long res = 1;
		for (int j = i - 12; j <= i; j ++) res *= a[j];
		maxx = max(maxx, res);
	}
	cout << maxx;
	
	return 0;
}
T9 特殊毕达哥拉斯三元组
#include <bits/stdc++.h>
using namespace std;

int main() {
	for (int i = 1; i <= 1000; i ++) {
		for (int j = i + 1; j <= 1000; j ++) {
			int k = 1000 - i - j;
			if (i * i + j * j == k * k) {
				cout << i * j * k;
				return 0;
			}
		}
	}
	
	return 0;
}
T10 质数求和
#include <bits/stdc++.h>
using namespace std;

const int N = 2e6 + 10;
bool a[N];

int main() {
	long long s = 0;
	for (int i = 2; i < 2000000; i ++) {
		if (!a[i]) {
			s += i;
			
			for (int j = i; j < 2000000; j += i) {
				a[j] = true;
			}
		}
	}
	cout << s;

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值