1. 求末n位
题目链接:http://csp.magu.ltd/problem/J1355
【问题描述】
编写程序,求2046个2046的乘积的末n位是多少。
【题解代码】
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
long long n, t = 1, cnt = 1;
cin >> n;
while (n != 0)
{
cnt *= 10;
n--;
}
for (int i = 1; i <= 2046; i++)
{
t = ((t % cnt) * 2046) % cnt;
}
cout << t;
return 0;
}
2.质因数分解
题目链接:http://csp.magu.ltd/problem/J1356
【问题描述】
已知正整数n是两个不同的质数的乘积,编写程序,求出较大的那个质数。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
for (int i = 2; i <= n; ++i)
if (n % i == 0)
{
printf("%d", n / i);
return 0;
}
}
3.求最大公约数
题目链接:http://csp.magu.ltd/problem/J1357
【问题描述】
编写程序,输入n个正整数,求这n个正整数的最大公约数。
【题解代码】
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
int main()
{
int n;
cin >> n;
int a, b;
cin >> a >> b;
n -= 2;
int t = gcd(a, b);
while (n--)
{
int x;
cin >> x;
t = gcd(t, x);
}
cout << t << endl;
return 0;
}
4.最大公约数和最小公倍数问题
题目链接:http://csp.magu.ltd/problem/J1358
【问题描述】
编写程序,输入两个正整数 x 0 、 y 0 x_0、y_0 x0、y0,求出满足下列条件 P 、 Q P、Q P、Q的个数:
- P 、 Q P、Q P、Q 是正整数;
- P 、 Q P、Q P、Q 以 x 0 x_0 x0 为最大公约数,以 y 0 y_0 y0 为最小公倍数。
求满足条件的所有可能的 P 、 Q P、Q P、Q 的个数。
【题解代码】
#include <iostream>
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
long long m, n, ans;
int main()
{
cin >> m >> n;
if (m == n)
ans--;
n *= m; // 鎶婁袱鏁扮殑绉瓨鍏涓?
for (long long i = 1; i <= n/i; i++)
{
if (n % i == 0 && gcd(i, n / i) == m)
ans += 2;
}
cout << ans;
return 0;
}
5.输油管道问题
题目链接:http://csp.magu.ltd/problem/J1359
【问题描述】
某油田有n口油井,石油公司欲修建一条由东向西的笔直的主输油管道,所有油井分布在主输油管道的南北两侧,每口油井都要有一条输油管道沿最短路径(或南或北)与修建的主输油管道相连。
编写程序,输入n口油井的位置,每口油井的坐标由x坐标(东西向)和y坐标(南北向)构成,选择合适的位置修建主输油管道,使各油井到主管道之间的输油管道长度总和最小,输出各油井到主管道之间的输油管道最小长度总和。
【题解代码】
#include <iostream>
#include <algorithm>
using namespace std;
int a[100010];
int main()
{
int n, ans = 0, l;
cin >> n;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x >> a[i];
}
sort(a + 1, a + n + 1);
l = n / 2;
for (int i = 1; i <= l; i++)
{
ans += a[n - i + 1] - a[i];
}
cout << ans;
}