[ACM] hdu 5038 Grade,acmhdu5038grade
Grade
Problem DescriptionTed is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushrooms and ask him to grade each mushroom according to its weight. Suppose the weight of a mushroom is w, then it’s grade s is
s = 10000 - (100 - w)^2
What’s more, Ted also has to report the mode of the grade of these mushrooms. The mode is the value that appears most often. Mode may not be unique. If not all the value are the same but the frequencies of them are the same, there is no mode.
InputThe first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
The first line of each test cases contains one integers N (1<=N<=10^6),denoting the number of the mushroom.
The second line contains N integers, denoting the weight of each mushroom. The weight is greater than 0, and less than 200.
OutputFor each test case, output 2 lines.
The first line contains "Case #x:", where x is the case number (starting from 1)
The second line contains the mode of the grade of the given mushrooms. If there exists multiple modes, output them in ascending order. If there exists no mode, output “Bad Mushroom”.
Sample Input
36100 100 100 99 98 1016100 100 100 99 99 1016100 100 98 99 99 97
Sample Output
Case #1:10000Case #2:Bad MushroomCase #3:9999 10000
Source2014 ACM/ICPC Asia Regional Beijing Online
解題思路:
有n個蘑菇,重量為w[i],根據公式為這n個蘑菇分等級(給出一個分數),其中出現頻率最多的那個分數成為mode,要求輸出這個mode, 注意mode 可能不唯一,按升序輸出符合是mode的分數。如果打出的分數每個都可以成為mode,那麼則輸出 Bad Mushroom.
可能上邊說的不是很明白,為每個蘑菇打出分數後,有三種情況,舉例子說明,假設有6個蘑菇:
1. 打出的分數為 2 2 2 1 1 6
其中2 出現的次數最多,mode唯一,輸出 2
2. 打出的分數為 4 4 2 2 1 3
其中2,4出現的次數最多,mode不唯一,升序輸出 2 4
3.打出的分數為 2 2 3 3 3 2
其中2,3出現的次數最多,但沒有其它的分數了,也就是打出的分數每個都是mode,輸出 Bad Mushroom.
其中第三種情況要特別注意一下,還要判斷mode出現了幾種,上面例子是出現了兩種2,3,如果只出現一種,比如打出的分數為 2 2 2 2 2 2,要輸出2,不能輸出Bad Mushroom,所以要特判一下。
代碼:
#include <iostream>#include <stdio.h>#include <map>#include <string.h>#include <cmath>#include <algorithm>using namespace std;const int maxn=1000020;int w[maxn];//重量map<int,int>mp;//first 為求出的mode, second 為該mode出現的次數int output[maxn];//輸出int n;int main(){ int t;cin>>t; int c=1; while(t--) { mp.clear(); scanf("%d",&n); bool one=0;//只有一種mode for(int i=1;i<=n;i++) { scanf("%d",&w[i]); int temp=10000-(100-w[i])*(100-w[i]); mp[temp]++; if(mp[temp]==n) one=1; } int maxn=-1;//找mode出現最多的次數 map<int,int>::iterator i; for(i=mp.begin();i!=mp.end();i++) { if((i->second)>maxn) maxn=i->second; } int len=0; i=mp.begin(); bool same=1;//判斷mode出現的次數是否相同 int flag=i->second; for(i=mp.begin();i!=mp.end();i++) { if((i->second)==maxn) output[len++]=i->first; if((i->second)!=flag) same=0; } cout<<"Case #"<<c++<<":"<<endl; if(same&&!one)//當出現的mode的次數相同且mode的種類不唯一時 { cout<<"Bad Mushroom"<<endl; continue; } for(int i=0;i<len-1;i++) cout<<output[i]<<" "; cout<<output[len-1]<<endl; } return 0;}
ACM hdu 1711(KMP),可以出來結果,怎就AC不了?
#include <iostream>
using namespace std;
int m,n,str1[1000005],str2[10005],next[10005];
void getnext(int str2[])
{
int i=0,j=-1;
next[0]=-1;
while(i<m-1)
{
if(j==-1 || str2[i]==str2[j])
{
i++;
j++;
if(str2[i]!=str2[j])
next[i]=j;
else
next[i]=next[j];
}
else j=next[j];
}
}
int kmp(int str1[], int str2[],int pos)
{
int i=pos,j=0;
while(i<n&&j<m)
{
if(j==-1 || str1[i]==str2[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j>=m)
return i-m+1;
else
return -1;
}
int main()
{
int t,i;
cin>>t;
while(t--)
{
//if(t==0) 你這樣會miss最後一組輸入的
//return 0;
cin>>n>>m;
for(i=0;i<n;i++)
cin>>str1[i];
for(i=0;i<m;i++)
cin>>str2[i];
getnext(str2);
cout<<kmp(str1,str2,0)<<endl;
}
return 0;
}
hdu acm 2032
我發我的ac代碼給你看吧,肯定是輸出時多了什麼空格什麼的,好好看看吧,加油
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[31][31];
int i,j,k;
for(i=1;i<=30;i++)
a[i][1]=a[i][i]=1;
if(n==1)printf("1\n");
if(n>=2)printf("1\n1 1\n");
for(i=3;i<=n;i++)
{
printf("1 ");
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][i]);
}
printf("\n");
}
}