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

1001.最小公倍数

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

解题思路:先算出两数的最大公约数,再两数相乘除以最大公约数得到最小公倍数

#include<stdio.h>
int gys(int m,int n);//使用函数能使整体看起来更简单
int main()
{
   
	int t,m,n;
	while(scanf("%d%d",&m,&n)==2)
	{
   
		t=m*n/gys(m,n);
		printf("%d\n",t);
	}
	return 0;
} 

int gys(int m,int n)
{
   
	int t,a;
	if(m>n)//令n恒大于m,便于下面的计算
	{
   
		t=m;
		m=n;
		n=t;
	}
	while(n%m!=0)//错位相除法
	{
   
		a=n;
		n=m;
		m=a%m;
	}
	return (m);
}

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
Author
lcy
Source
ACM程序设计期末考试(2006/06/07)

解题思路:因为A和B特别大,所以不能直接A^B,计算机也很累的。所以我们可以人为的把每一次循环中AA的后三位给提出来(因为是乘法,所以这样做并无大碍)。*

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

#include<stdio.h>
int main()
{
   
	int n,i,a,j,m,t;
	scanf("%d",&a);
	for(i=1;i<=a;i++)
	{
   
		scanf("%d",&n);
		if(n>=1&&n<=1000000000)
		{
   
			m=1;
			t=n%10;
			for(j=0;j<=(n-1)%4;j++)//因为会有n%4=0的情况,所以循环从0开始,到(n-1)%4结束
			{
   
				m=m*t;
			}
		}
		printf("%d\n",m%10);
	}
	return 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值