ZUST 程序设计算法竞赛基础【1】题解报告

这篇博客是关于ZUST(浙江科技学院)程序设计算法竞赛的基础题解,涵盖了1001到1012共12道题目,涉及最小公倍数、整数指数的最后三位数、爬虫、四则运算、斐波那契数列等多个算法问题。博主提供了每道题目的题目描述、解题思路和代码实现。
摘要由CSDN通过智能技术生成

1001 最小公倍数

题目

Problem Description

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

Input

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

Output

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

Sample Input

10 14

Sample Output

70

题解

求最小公倍数算法:

最小公倍数=两整数的乘积÷最大公约数

求最大公约数算法:

1.辗转相除法

有两整数a和b:
① a%b得余数c
② 若c=0,则b即为两数的最大公约数
③ 若c≠0,则a=b,b=c,再回去执行①

2.相减法

有两整数a和b:
① 若a>b,则a=a-b
② 若a<b,则b=b-a
③ 若a=b,则a(或b)即为两数的最大公约数
④ 若a≠b,则再回去执行①

3.穷举法

有两整数a和b:
① i=1
② 若a,b能同时被i整除,则t=i
③ i++
④ 若 i <= a(或b),则再回去执行②
⑤ 若 i > a(或b),则t即为最大公约数,结束
改进:
① i= a(或b)
② 若a,b能同时被i整除,则i即为最大公约数,
结束
③ i--,再回去执行②
有两整数a和b:
① i=1
② 若a,b能同时被i整除,则t=i
③ i++
④ 若 i <= a(或b),则再回去执行②
⑤ 若 i > a(或b),则t即为最大公约数,结束
改进:
① i= a(或b)
② 若a,b能同时被i整除,则i即为最大公约数,
结束
③ i--,再回去执行②

--------------------- 
作者:iwm_next 
来源:CSDN 
原文:https://blog.csdn.net/iwm_next/article/details/7450424 

代码

#include<stdio.h>
int main()
{
int n1,n2,i,j,temp=0; 
while(scanf("%d %d",&n1,&n2)!=EOF)
    {       
	i=n1>n2?n1:n2;
      	j=n1<n2?n1:n2;
       do{
        temp=j;
        j=i%j;    		
	i=temp;
       }  while(j!=0);
      printf("%d\n",n1/i*n2);
    }   
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

题解

由于只需要输出最后三位,就不需要考虑前面,直接取乘数A的最后三位相乘,再取积的最后三位继续进行计算,就可以比较快捷。
注意:A的位数

代码


#include<stdio.h>
int main(){
	int a,b,i,sum=1;
	while(scanf("%d %d",&a,&b)!=EOF){
		sum=1;
		if(a!=0&&b!=0){	
			a=a%100;
			for(i=0;i<b;i++)sum=a*sum%1000;	
			printf("%d\n",sum);
			}
		}
	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.

题解

这道题与第二题相似,由于在最后一位也可以直接找规律,快速幂求模

以下是快速幂的原理介绍:
在这里插入图片描述

在这里插入图片描述
详细的可以点击下面链接
https://blog.csdn.net/lsgqjh/article/details/45076513

代码

#include<stdio.h>
long long mode(long long a, long long b, long long c)
{
   long long sum = 1;
   a
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值