题目传送门
A. Paint the Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of integers a1,a2,…,an. You need to paint elements in colors, so that:
If we consider any color, all elements of this color must be divisible by the minimal element of this color.
The number of used colors must be minimized.
For example, it’s fine to paint elements [40,10,60] in a single color, because they are all divisible by 10. You can use any color an arbitrary amount of times (in particular, it is allowed to use a color only once). The elements painted in one color do not need to be consecutive.
For example, if a=[6,2,3,4,12] then two colors are required: let’s paint 6, 3 and 12 in the first color (6, 3 and 12 are divisible by 3) and paint 2 and 4 in the second color (2 and 4 are divisible by 2). For example, if a=[10,7,15] then 3 colors are required (we can simply paint each element in an unique color).
Input
The first line contains an integer n (1≤n≤100), where n is the length of the given sequence.
The second line contains n integers a1,a2,…,an (1≤ai≤100). These numbers can contain duplicates.
Output
Print the minimal number of colors to paint all the given numbers in a valid way.
Examples
inputCopy
6
10 2 3 5 4 2
outputCopy
3
inputCopy
4
100 100 100 100
outputCopy
1
inputCopy
8
7 6 5 4 3 2 2 3
outputCopy
4
Note
In the first example, one possible way to paint the elements in 3 colors is:
paint in the first color the elements: a1=10 and a4=5,
paint in the second color the element a3=3,
paint in the third color the elements: a2=2, a5=4 and a6=2.
In the second example, you can use one color to paint all the elements.
In the third example, one possible way to paint the elements in 4 colors is:
paint in the first color the elements: a4=4, a6=2 and a7=2,
paint in the second color the elements: a2=6, a5=3 and a8=3,
paint in the third color the element a3=5,
paint in the fourth color the element a1=7.
题意很简单:不要理解错了就可以做;
在这里说一个 ,就是如果是出现1,那么不管有多少全部都为1;
解题思路:我的话是先去重,然后用每一个数与后面的数,如果能被整除就标记,然后每次循环标记过的都记录下来,找一共要多少次,才能把它标记完,这次数就是最后答案;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int t;
int a[120];
int b[120];
int main()
{
cin>>t;
for(int i=0; i<t; i++)
{
cin>>a[i];
}
for(int i=0; i<t-1; i++) //冒泡排序
{
for(int j=0; j<t-1-i; j++)
{
if(a[j]>=a[j+1])
{
int tt=a[j];
a[j]=a[j+1];
a[j+1]=tt;
}
}
}
int bb[102];
int index=1;
bb[0]=a[0];
for(int i=1; i<t; i++) //去重
{
if(a[i]!=a[i-1])
{
bb[index++]=a[i];
}
}
sort(bb,bb+index);//从小到大排序
// for(int i=0;i<index;i++)
// cout<<bb[i]<<endl;
int sum=0;
if(bb[0]==1){
sum++;
cout<<sum<<endl;
}
else{
for(int i=0; i<index; i++)
{
if(b[i]==0&&bb[i]!=1)
{
sum++;
for(int j=0;j<index;j++)
{
if(bb[j]%bb[i]==0)
{
b[j]=1;
}
}
}
}
cout<<sum;
}
return 0;
}