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