计算机20-7、8程序设计基础实践1

Problem_A_判断公倍数

Description
输入一个正整数,判断其是否为2和3的公倍数,若是输出Yes; 否则,输出No;
——————————————
Input
输入一个正整数
——————————————
Output
输出Yes或No
——————————————
Sample Input
6
5
——————————————
Sample Output
Yes
No

思路分析:
这道题直接按照题目的意思来即可,输入一个数,我们判断一下它对 2 和 3 取余数后是否都为零,如果都为零,输出 Yes ,否者输出 No 即可

#include<cstdio>
using namespace std;
int main()
{
    
    
	int n;
	while(~scanf("%d",&n))
	{
    
    
		if(n%3==0 && n%2==0)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

Problem_B_包含3的整数个数

Description
给定两个正整数a和b(a<b),计算a和b之间所有包含3的数之和。例如:2 15,包含3的数字有3和13,所以该组数据的和为16。
——————————————
Input
两个正整数
——————————————
Output
满足条件的数据的和
——————————————
Sample Input
2 15
10 24
——————————————
Sample Output
16
36

思路分析:
我们只需要枚举给定区间的每一个数,并对每一个数的每一位去判断一次,如果这个数在某一位出现了 3,我们只需要将这个数加入到ans这个里面
最终输出ans的值即可

#include<cstdio>
using namespace std;

bool check(int x)
{
    
    
	int temp;
	while(x)
	{
    
    
		temp=x%10;
		if(temp==3)	return true;
		x/=10;
	}
	return false;
}

int main()
{
    
    
	int l,r;
	while(~scanf("%d%d",&l,&r))
	{
    
    
		int ans=0;
		for(int i=l;i<=r;i++)
			if(check(i))
				ans+=i;
		printf("%d\n",ans);
	}
}

Problem_C_公约数和公倍数

Description
求两个正整数的最大公约数和最小公倍数
——————————————
Input
输入两个整数
——————————————
Output
输出公约数和公倍数
——————————————
Sample Input
2 3
21 9
——————————————
Sample Output
1
6
3
63

思路分析:
这一道题的难点在于求两个gcd(最小公因数,不会的百度)
求出gcd后,我们可以很简单的证明最小公倍数就是a*b/gcd
于是有了这一份代码

#include<cstdio>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;

int gcd(int a,int b)
{
    
    
	if(a%b==0)
		return b;
	return gcd(b,a%b);
}

int main()
{
    
    
	int a,b;
	while(~scanf("%d%d",&a,&b))
	{
    
    
		int Gcd=gcd(max(a,b),min(a,b));
		printf("%d\n%d\n",Gcd,a*b/Gcd);
	}
}

Problem_D_判断是一年中的哪一天

Description
输入某年某月某日,判断这一天是这一年的第几天?
——————————————
Input
输入一个日期
——————————————
Output
输出是这一年的第几天
——————————————
Sample Input
2010 1 6
2012 3 2
——————————————
Sample Output
6
62

思路分析:
这也是一道水题,首先我们要清楚,闰年是首先被判断的,因为它会直接影响二月的天数
在知道二月天数后,我们是需要用一个循环,将在当前月份前面的月份的天数加起来(应为在这之前每个月都是完整的),然后再加上这个月的天数就能得到最终答案了

#include<cstdio>
using namespace std;
int mon[15]={
    
    0,31,0,31,30,31,30,31,31,30,31,30,31};

bool isr(int Y)
{
    
    
	if((Y%4==0 && Y%100!=0) || Y%400==0)
		return true;
	else	return false;
}

int main()
{
    
    
	int y,m,d;
	while(~scanf("%d%d%d",&y,&m,&d))
	{
    
    
		int cnt=0;
		
		if(isr(y))	mon[2]=29;
		else		mon[2]=28;
		
		for(int i=1;i<m;i++)
			cnt+=mon[i];
		cnt+=d;
		printf("%d\n",cnt);
	}
	return 0;
}

Problem_E_三角形面积计算

Description
给出三角形的三边,计算其面积
——————————————
Input
三角形的三边长度(单精度)
——————————————
Output
三角形的面积(保留2位小数)
——————————————
Sample Input
3 4 5
2 6 6
3.5 7.83 6.178
——————————————
Sample Output
6.00
5.92
10.46

思路分析:
这个题目就更水了,因为我们有了三角形的三边长度,所以直接海伦公式出答案(注意保留三位小数)

#include<cmath>
#include<cstdio>
using namespace std;

int main()
{
    
    
	double a,b,c,p;
	while(~scanf("%lf%lf%lf",&a,&b,&c))
	{
    
    
		p=(a+b+c)/2;
		printf("%.2f\n",sqrt(p*(p-a)*(p-b)*(p-c)));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Holo__/article/details/109448112