Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist.
Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minutes long, i.e. the day consists of minutes 1, 2, ..., z. How many artists should be killed so that there are no artists in the room when Ilia calls? Consider that a call and a talk with an artist take exactly one minute.
The only string contains three integers — n, m and z (1 ≤ n, m, z ≤ 104).
Print single integer — the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls.
1 1 10
10
1 2 5
2
2 3 9
1
Taymyr is a place in the north of Russia.
In the first test the artists come each minute, as well as the calls, so we need to kill all of them.
In the second test we need to kill artists which come on the second and the fourth minutes.
In the third test — only the artist which comes on the sixth minute.
题目大意:设一天有z分钟,每隔n分钟需要接一次电话,每隔m分钟有一个艺术家拜访,每次电话一分钟,问需要除掉几个艺术家,才能接电话不受打扰。
题目分析:因为数据较小,直接开个数组记录在某一分钟是否接电话,然后遍历数组,是否刚好在艺术家接电话的时候。ans累增。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int const maxn = 10005;
int a[maxn];
int main(){
int n,m,z;
while((scanf("%d%d%d",&n,&m,&z))!=EOF){
memset(a,0,sizeof(a));
int ans=0;
for(int i=n;i<=z;i=i+n)
a[i]=1;
for(int i=m;i<=z;i=i+m){
if(a[i]==1)
ans++;
}
printf("%d\n",ans);
}
return 0;
}
解决一个有趣的问题:如何确保在特定的时间间隔内接听电话时,不会有艺术家来访造成干扰。通过编程算法来计算需要“移除”的艺术家数量,以实现接听电话时不被打扰。

154

被折叠的 条评论
为什么被折叠?



