![]()
#include<stdio.h>
#include<math.h>
int GCF(int a, int b)
{
if (a < b)
{
int x = b;
b = a;
a = x;
}
for (int i = b; i >= 1; i--)
{
if (a % i == 0 && b % i == 0)
{
return i;
}
}
return 1;
}
int LCM(int a, int b)
{
return (a * b) / GCF(a, b);
}
int main()
{
int a, b;
printf("Input a,b:");
scanf("%d,%d", &a, &b);
if (a > 0 && b > 0) {
printf("Least Common Mutiple of %d and %d is %d\n", a, b, LCM(a, b));
} else {
printf("Input number should be positive!\n");
}
return 0;
}
正确答案:
程序语言 C/C++
#include<stdio.h>
#include<stdlib.h>
int Lcm(int a,int b);
int main()
{
int a,b,x;
printf("Input a,b:");
scanf("%d,%d",&a,&b);
x=Lcm(a,b);
if(x!=-1)
printf("Least Common Mutiple of %d and %d is %d\n",a,b,x);
else
printf("Input number should be positive!\n");
//system("pause");
return 0;
}
//函数功能:计算a和b的最小公倍数,输入负数时返回-1
int Lcm(int a,int b)
{
int i;
if(a<=0 || b<=0)
return -1;
for(i=1;i<b;i++)
{
if(i*a%b==0)
return i*a;
}
return b*a;
}
用例1:
输入
16,24
输出
Input a,b:Least Common Mutiple of 16 and 24 is 48
用例2:
输入
-16,24
输出
Input a,b:Input number should be positive!
![]()
#include<stdio.h>
#include<math.h>
int GCD(int a, int b)
{
if (a < b)
{
int x = b;
b = a;
a = x;
}
for (int i = b; i >= 1; i--)
{
if (a % i == 0 && b % i == 0)
{
return i;
}
}
return 1;
}
int main()
{
int a, b;
printf("Input a,b:");
scanf("%d,%d", &a, &b);
if (a > 0 && b > 0) {
printf("Greatest Common Divisor of %d and %d is %d\n",a,b,GCD( a, b));
} else {
printf("Input number should be positive!\n");
}
return 0;
}
正确答案:
程序语言 C/C++
#include<stdio.h>
#include<stdlib.h>
int Gcd(int a,int b);
int main()
{
int a,b,x;
printf("Input a,b:");
scanf("%d,%d",&a,&b);
x=Gcd(a,b);
if(x!=-1)
printf("Greatest Common Divisor of %d and %d is %d\n",a,b,x);
else
printf("Input number should be positive!\n");
//system("pause");
return 0;
}
//函数功能:计算a和b的最大公约数,输入负数时返回-1
int Gcd(int a,int b)
{
int i,t;
if(a<=0 || b<=0)
return -1;
t=a<b?a:b;
for(i=t;i>0;i--)
{
if(a%i==0 && b%i==0)
return i;
}
return 1;
}
用例1:
输入
16,24
输出
Input a,b:Greatest Common Divisor of 16 and 24 is 8
用例2:
输入
-16,24
输出
Input a,b:Input number should be positive!
![]()
我的答案:
#include<stdio.h>
#include<math.h>
long Fact(int n)
{
int i;
long result=1;
for(i=2;i<=n;i++)
{
result*=i;
}
return result;
}
int main()
{
int n,i;
int m=0;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
m+=Fact(i);
}
printf("1!+2!+…+%d!=%d\n",n,m);
return 0;
}
正确答案:
程序语言 C/C++
#include<stdio.h>
#include<stdlib.h>
int FactSum(int n);
int main()
{
int i,n,sum;
printf("Input n:");
scanf("%d",&n);
sum=FactSum(n);
printf("1!+2!+…+%d!=%d\n",n,sum);
//system("pause");
return 0;
}
//函数功能:计算1!+2!+3!+4!+5!+…+n!
int FactSum(int n)
{
int i,p=1,s=0;
for(i=1;i<=n;i++)
{
p=p*i;
s=s+p;
}
return s;
}
用例1:
输入
10
输出
Input n:1!+2!+…+10!=4037913
![]()
#include <stdio.h>
#include<math.h>
int is_prime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i <= n/2; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n;
printf("Input n:");
scanf("%d", &n );
for(int a=2; a<=n; a++) {
if (is_prime(a)) {
printf("%d\t", a);
}
}
return 0;
}
正确答案:
程序语言 C/C++
#include<stdio.h>
#include<stdlib.h>
int IsPrime(int x);
int main()
{
int i,n;
printf("Input n:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(IsPrime(i))
printf("%d\t",i);
}
printf("\n");
//system("pause");
return 0;
}
//函数功能:判断x是否是素数,是素数返回1,否则返回0
int IsPrime(int x)
{
int i;
if(x<=1)
return 0;
for(i=2;i<x;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
用例1:
输入
10
输出
Input n:2 3 5 7