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

ZUST 程序设计算法竞赛基础【1】1001 最小公倍数 Problem Description 给定两个正整数 计算这两个数的最小公倍数 Input 输入包含多组测试数据 每组只有一行 包括两个不大于 1000 的正整数 Output 对于每个测试用例 给出这两个数的最小公倍数 每个实例输出一行 Sample Input 10 14 Sample Output 70 Source POJ 解题思路

大家好,我是讯享网,很高兴认识大家。

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<=) { 
    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
小讯
上一篇 2025-01-06 14:45
下一篇 2025-03-25 10:52

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/74206.html