哈爾濱理工大學計算機20-7、8程式設計基礎實踐1
- Problem_A_判斷公倍數
- Problem_B_包含3的整數個數
- Problem_C_公約數和公倍數
- Problem_D_判斷是一年中的哪一天
- Problem_E_三角形面積計算
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/201435.html
標籤:其他
上一篇:mysql基礎常用陳述句
