Delete
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 329 Accepted Submission(s): 197
For each case:
The first line contains one integer N(0<N≤100).
The second line contains N integers a1,a2,...,aN(1≤ai≤N), denoting the integers WLD plays with.
The third line contains one integer K(0≤K<N).
Print one integer. It denotes the maximum of different numbers remain after the deletion.
以上是題目,就是給你n個數要你刪掉k個,讓剩下的數不相同最多。那就是找到能刪掉而不影響最大的答案的有幾個,就是找出有多少個是前面已經有了的,而如果k比能刪的重複數的數目的總和還大,就從n-sum之後再刪,就是刪不同的數,這時候不同數的數目就變了,輸出答案即可。不知爲何竟然錯了,難道是因爲沒有memset?可是我在後來直接定義數組的呀。。不過現在ac了。這道題我在想,要是數據特別多特別大怎麼算呢,當時就想到map,但是map我不會用。。等會兒我去百度百度吧。
#include<iostream>
#include<cstdio>#include<cstring>
using namespace std;
#define maxx 105
int a[maxx];
int main()
{
int n;
while(cin>>n)//忽然在想要是n的輸入是“邪惡的” ,那該怎麼判斷規避,還是現在這樣是不用的呢
{
memset(a,0,sizeof(a));
int sum=0,k,ans=0,x;//sum代表有幾個不一樣的數
//ans代表最終輸出,k代表輸入的k值,x用來存每次輸入的數。n代表輸入有幾個數
int i=n;
while(i--)
{
//scanf("%d",&x);
cin>>x;
if(!a[x])
{
sum+=1;
}
a[x]+=1;
}
cin>>k;
if(n-sum>=k)
printf("%d\n",sum);//其實之前一直習慣於cin、cout。不過printf什麼的更快,想習慣改成這樣。
else
{
//printf("%d\n",sum1+sum2-k);
cout<<n-k<<endl;
}
}
return 0;
}
//=============接下來是原來的代碼,試了試發現問題在於memset。。但是我明明是輸入n之後才定義的數組a啊怎麼就不行呢。==========================
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int a[105],k,sum=0,x;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
cin>>x;
if(a[x]!=1)//就算只有一行代碼,也要有花括號,要寫得規範,便於閱讀。
{
a[x]=1;
}
else
{
sum+=1;
}
}
cin>>k;
if(sum>=k)
{
printf("%d\n",n-sum);
}
else
{
printf("%d\n",n-k);
}
}
return 0;
}