算法班第一节题解--信计182张家政

这篇博客介绍了10个不同的算法题目,包括最小公倍数、A的B次方的后三位、数的个位数等,涉及数学、循环和递归等概念。每个问题提供了问题描述、示例输入输出以及解题思路,旨在帮助读者理解并解决算法问题。
摘要由CSDN通过智能技术生成

1001 最小公倍数

Problem Description
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70

思路:
找到两个数的所有因数,相乘即可

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int ans = 1;

int main(){
   
    int x, y;
    while(scanf("%d %d", &x, &y) == 2){
   
        ans = 1;
        for(int i = 2; i <= min(x,y);){
   
            if(x % i  == 0 || y % i == 0){
   
                if(x%i == 0) x/=i;
                if(y%i == 0) y/=i;
                ans*=i;
            }else i++;//当i不是x, y 公因子的时候,i++
        }
        ans *= x; ans*=y;//x, y没有公因子,ans 最后要和没有除干净的 x, y 相乘
        cout << ans << endl;
    }
    
    return 0;
}

1002 人见人爱A^B

Problem Description
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Sample Input
2 3
12 6
6789 10000
0 0
Sample Output
8
984
1

没什么好说的,循环取模,要在循环过程中取,避免溢出。

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

int main(){
   
    int x, y;
    while(scanf("%d %d", &x, &y) == 2 && x !=0 && y != 0){
   
        y--;int t = x;
        while(y--){
   
            x *= t;
            x %= 1000;
        }
        cout << x << endl;
    }
    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
翻译:给定一个 N(1<=N<=1,000,000,000) 输出 N^N 的个位数。

思路:只取个位数,很明显有规律可循。手写循环节,直接查找即可。

#include <iostream>
#include <algorithm>
using namespace std;
int arr[10][4] = {
   
{
   0},
{
   1},
{
   2,4,8,6},
{
   3,9,7,1},
{
   4,6},
{
   5},
{
   6},
{
   7,9,3,1},
{
   8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值