ZUST基础1题解

ZUST 程序设计算法竞赛基础【1】

1001 最小公倍数

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

解题思路:不用for循环一个个乘下去,可能会溢出,
改用*a,b的最小公倍数=a/(a,b的最大公约数)b

代码:

#include<stdio.h>
int main()
{
    int a,b,c,i,j;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        for(i=1;i<=a;i++)
        {
            if(a%i==0&&b%i==0)
            {
                j=i;
            }
        }
        c=a/j*b;
        printf("%d\n",c);
    } 
    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%100),再用for循环依次是后几位相乘。

代码:

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

添加链接描述

解题思路:方法一 利用快速幂,模板如下:

Int ans=1;
a=a%c;
while(b>0)
{
  if(b%2==1)
  { 
ans=(ans*a)%c;
  }
  b=b/2;
  a=(a*a)%c;
}
return ans;

方法二 找规律,会发现2,22,42的结果相同,说明每20为一个周期

代码:
1.快速幂:

#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int a[n],i,t,j,x;
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		t=a[i];
		x=1;
		a[i]=a[i]%10;
		while(t>0)
		{
			if(t%2==1)
			{
				x=(x*a[i])%10;
			}
			t=t/2;
			a[i]=(a[i]*a[i])%10;
		}
		printf("%d\n",x);
	}
	return 0;
}

2.找规律

#include<stdio.h>
#include<stdlib.h>
int main()  
{
    int n,a,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值