程序设计算法竞赛基础——练习1解题报告

这篇博客是关于ACM算法竞赛的基础练习解题报告,涵盖了1001到1012共12道题目,包括最小公倍数、整数幂的后三位、数位计算等不同类型的算法问题。博主详细介绍了每个问题的描述、输入输出格式、解题思路及代码实现,旨在帮助读者理解和解决算法竞赛中的基础问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序设计算法竞赛基础——练习1解题报告

1001 最小公倍数

问题描述:

给定两个正整数,计算这两个数的最小公倍数。

输入

输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数。

输出

对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。

样本输入

10 14

样本输出

70

资源

POJ

解题思路

设两个数为x和y,两数最大公因数为T,最小公倍数为P。

P = x * y / T

代码实现

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

//GYS用于查找最大公约数
int GYS(const int a, const int b) {
    if(a % b != 0) {
        return GYS(b, a % b);
    }
    return b;
}

//GBS查找最大公倍数
int GBS(const int a, const int b) {
    if(a != b)return (a * b) / GYS(a, b);
    else return b;
}

int main () {
    int x, y;
    while(cin >> x >> y) {//输入
        cout << GBS(x, y) << "\n";//输出
    }
    return 0;
}

1002 人见人爱A^B

问题描述

求甲^B的最后三位数表示的整数。

说明:甲^B的含义是”A的乙次方“

输入

输入数据包含多个测试用例,每个实例占一行,由两个正整数A和B组成(1 <= A, B <= 10000) ,如果A=0, B=0,

则表示输入数据的结束,不做处理。

输出

对于每个测试实例,请输出甲^B的最高后三位表示的整数,每个输出占一行。

样本输入

2 3
12 6
6789 10000
0 0

样本输出

8
984
1

作者

LCY

资源

ACM程序设计期末考试(2006/06/07)

解题思路

由于只需要最后三位数,所以只需边计算边对于1000取模即可。

代码实现

#include<iostream>
using namespace std;

int main(){
	int a, b, s;//s保存输入结果
	while((cin >> a >> b) && (a != 0 && b !=0)){
		s = 1;//s初始化为1
		while(b--){
			s = s * a % 1000;//每乘一次a取余一次,避免数据过大
		}
		cout << s << "\n";
	}
	return 0;
}

1003 Rightmost Digit

Problem Description

Given a positive integer N, you should output the most right digit of N^N.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number

of test cases. T test cases follow.

Each test case contains a single positive integer N (1 <= N <= 1,000,000,000).

Output

For each test case, you should output the rightmost digit of N^N.

Sample Input

2
3
4

Sample Output

7
6

Hint

In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.

In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

Author

Ignatius.L

解题思路

输出只取N^N的个位数,所以只需找出0~9的N次方的规律即可。

代码实现

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;

//定义一个不定长数组分别存放0~9N次方后的规律
vector<int> a[10];

//CSH()找出0~9的N次方后的规律
void CSH() {
	int j;
	for(int i = 0; i < 10; i++) {
		a[i].push_back(i);
		j = 0;
		while(a[i][j] * i % 10 != a[i][0]) {
			int t = a[i][j] * i % 10;
			a[i].push_back(t);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值