Description
Input
Output
Sample Input
2
1
2
3
4
INPUT DETAILS:
The 5 cows are given the numbers 2, 1, 2, 3, and 4, respectively.
Sample Output
0
2
1
3
OUTPUT DETAILS:
The first cow pats the second and third cows; the second cows pats no cows;
etc.
【解析】
直接看代碼吧……
(這是暴力^_^)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110000],b[110000],s[110000];
int cmp(const void*xx,const void*yy)
{
int n1=*(int *)xx;
int n2=*(int *)yy;
if(n1>n2) return 1;
if(n1<n2) return -1;
return 0;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}
qsort(b+1,n,sizeof(int ),cmp);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(b[j]>a[i]) break;
if(a[i]%b[j]==0) s[i]++;
}
}
for(int i=1;i<=n;i++) printf("%d\n",s[i]-1);
return 0;
}
(這是正解)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110000],s[1100000],v[1100000];
int main()
{
int n,maxx=-1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
v[a[i]]++;
maxx=max(maxx,a[i]);
}
for(int i=1;i<=maxx;i++)
{
if(v[i]!=0)//有i這個數
{
for(int j=i;j<=maxx;j+=i)
{
s[j]+=v[i];//j能除i就累計j的答案
}
}
}
for(int i=1;i<=n;i++) printf("%d\n",s[a[i]]-1);//自己也能除自己,把自己減掉
return 0;
}