ACM练习
最小公倍数
Problem Description
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数. Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。Sample Input
10 14
Sample Output
70
Code:
#include
using namespace std;
int main(){
int a,b,t,m;
while(cin>>a>>b){
if(a
t=a;
a=b;
b=t;
}
m=a*b;
while(b!=0){
t=a%b;
a=b;
b=t;
}
t=m/a;
cout< } return 0; } Cake Problem Description 一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. Input 每行有两个数p和q. Output 输出最少要将蛋糕切成多少块 Sample Input 2 3 Sample Output 4 Hint 将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求. 当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。 当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。 注意:利用公式 p+q-最大公约数 程序代码: #include using namespace std; int gcd(int a, int b){ while(a!=b){ if(a>b) a=a-b; else b=b-a; } return a; } int main(){ int p,q,t; while(cin>>p>>q){ t=gcd(p,q); cout< return 0; } 又见GCD Problem Description 有三个正整数a,b,c(0 Input 第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。 Output 输出对应的c,每组测试数据占一行 Sample Input 2 6 2 12 4 Sample Output 4 8 程序代码: 方法1:最简单的方法,一个一个的试。 #include using namespace std; int gcd(int m,int n){ while(m!=n){ if(m>n) m=m-n; else n=n-m; } return m; } int main(){ int a,b,i,n; cin>>n; while(n--){ cin>>a>>b; for(i=b+1;i<1000000;i++){ if(gcd(a,i)==b){ cout< break; } } } return 0; } 方法2:分析:已知gcd(a,c)=b,和a,b,求最小的c,其中c!=b。原以为既然c!=b,那么最小的当然是2b啦,可是提交了发先WA。回头分析发现,不是这么简单,原因在于a 可能是c的倍数,比如若a=6b,那么c=2b,3b,4b都不是答案,因为这时候gcd(a,c)=2b,3b,4b。既然如此,只好一个一个试了,从2b开始,一次增加b,直到gcd(a,c)=b。 #include using namespace std; int gcd(int m,int n){ while(m!=n){ if(m>n) m=m-n; else n=n-m; } return m; } int main(){ int a,b,c,n; cin>>n; while(n--){ cin>>a>>b; c=2*b; while(gcd(a,c)!=b) c+=b; cout< } return 0; } How many prime numbers Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input T here are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exc eed 32-bit signed integer, and each of them won’t be less than 2. Output F or each case, print the number of prime numbers you have found out Sample Input 3 2 3 4 Sample Output 2 程序代码: #include #include using namespace std; double a; int sum; int n; bool prime(){ int f,i; f=(int)sqrt(a); for(i=2;i<=f;i++) if((int)a%i==0) return 1; return 0; } int main(){ int i; while(cin>>n){ sum=0; for(i=0;i cin>>a; if(!prime()) sum++; } cout< } return 0; } A hard puzzle Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin. this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise. Input There are mutiple test cases. Each test cases consists of two numbers a and b(0 For each test case, you should output the a^b's last digit number. Sample Input 7 66 8 800 Sample Output 9 6 程序代码: #include using namespace std; int calc(int a, int b) { if(a==0 || a==1 || a==5 || a==6) return a; if(a==2) { if(b%4==1) return 2; if(b%4==2) return 4; if(b%4==3) return 8; if(b%4==0) return 6; } if(a==3) { if(b%4==1) return 3; if(b%4==2) return 9; if(b%4==3) return 7; if(b%4==0) return 1; } if(a==4) { if(b%2==1) return 4; if(b%2==0) return 6; } if(a==7) { if(b%4==1) return 7; if(b%4==2) return 9; if(b%4==3) return 3; if(b%4==0) return 1; } if(a==8) { if(b%4==1) return 8; if(b%4==2) return 4; if(b%4==3) return 2; if(b%4==0) return 6; } if(a==9) { if(b%2==1) return 9; if(b%2==0) return 1; } } int main() { int a,b,s; while(cin>>a>>b) { s=calc(a%10,b); cout< } return 0; } Ignatius's puzzle Problem Description I gnatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal nonegative integer a,make the arbitrary integer x ,65|f(x)if no exists that a,then print "no". Input The input contains several test cases. Each test case consists of a nonegative integer k, More details in the Sample Input Output The output contains a string "no",if you can't find a,or you should output a line contains the a.More details in the Sample Output. Sample Input 11 100 9999 Sample Output 22 no 43 解题思路:数学归纳法f(x)=5*x^13+13*x^5+k*a*x 假设当x=n时65|f(x)成立,那一定有65|f(x+1)成立 那么65|f(x+1)-f(x) 成立通过二项展开可以得到只要18+k*a可以被65整除就可以了。在1~65之间遍历就ok。 程序代码: #include using namespace std; int main(){ int k,a,sum; while(cin>>k){ for(a=1;a<66;a++){ sum=18+k*a; if(sum%65==0) break; }