![]() |
#2
stophin2012-11-27 22:56
|

问题描述:
输入1个数N(1 <= N <= 5000),表示歌曲数.接下来共有N行,每行包括一个字符串m
(长度小于50字节,可能有中文,一个中文字占2个字节),以及一个数字p(p <= INT_MAX)。
m表示歌曲名,p表这首歌的优先级(1,2,3...),数字越小优先级就越高.优先级<=0的歌
曲是coder不喜欢的.保证不存在两首歌优先级相同的情况.保证不存在两首歌名称相同的情
况.
输出一个k,表示coder同学喜欢听的歌曲的数量.接下去k行,优先级从高到低,每行输出
coder同学喜欢听的音乐名称.
SAMPLE INPUT
2
月亮至上 -1
because of you 1
6
太早 3
不完整的旋律 4
第一个清晨 1
18 and life 2
still'loving 6
我恨你 5
SAMPLE OUTPUT
1
because of you
6
第一个清晨
18 and life
太早
不完整的旋律
我恨你
still'loving
下面是我写的代码,oj上提交是Wrong Answer 不知道哪里错了,求高人看看。。。
#include <iostream>
#include <string>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX 5001
struct TSong
{
char ch[52];
};
struct TLike
{
char st[52];
int p;
};
TSong tsong[MAX];
TLike tlsong[MAX];
void vSort(int n);
bool cmp(const TLike &A,const TLike &B);
void vOut(int n);
void vInput(int n);
int nDevi(int n);
void vInit();
int main()
{
int N,q;
while(cin>>N)
{
vInit();
vInput(N);
q=(nDevi(N))-1;
if(0==q)
cout<<q<<endl;
else
{
vSort(q);
vOut(q);
}
}
return 0;
}
void vInput(int n)
{
int i;
for(i=1;i<=n;i++)
{
fflush(stdin);
gets(tsong[i].ch);
}
}
int nDevi(int n)
{
int i,len,k,j,temp,npow;
k=1;
for(i=1;i<=n;i++)
{
len=strlen(tsong[i].ch);
j=len-1;
temp=0;
npow=0;
while(1)
{
while(tsong[i].ch[j]<='9'&&tsong[i].ch[j]>='0')
{
temp=temp+(tsong[i].ch[j]-'0')*pow(10,npow);
j--;
npow++;
}
if(tsong[i].ch[j]==' ')
{
tlsong[k].p=temp;
for(int q=0;q<j;q++)
{
tlsong[k].st[q]=tsong[i].ch[q];
}
k++;
break;
}
else
break;
}
}
return k;
}
void vOut(int n)
{
int i;
cout<<n<<endl;
for(i=1;i<=n;i++)
{
printf("%s\n",tlsong[i].st);
}
}
void vSort(int n)
{
sort(&tlsong[1],&tlsong[n+1],cmp);
}
bool cmp(const TLike &A,const TLike &B)
{
return A.p<B.p;
}
void vInit()
{
memset(tlsong,'\0',sizeof(tlsong));
}
输入1个数N(1 <= N <= 5000),表示歌曲数.接下来共有N行,每行包括一个字符串m
(长度小于50字节,可能有中文,一个中文字占2个字节),以及一个数字p(p <= INT_MAX)。
m表示歌曲名,p表这首歌的优先级(1,2,3...),数字越小优先级就越高.优先级<=0的歌
曲是coder不喜欢的.保证不存在两首歌优先级相同的情况.保证不存在两首歌名称相同的情
况.
输出一个k,表示coder同学喜欢听的歌曲的数量.接下去k行,优先级从高到低,每行输出
coder同学喜欢听的音乐名称.
SAMPLE INPUT
2
月亮至上 -1
because of you 1
6
太早 3
不完整的旋律 4
第一个清晨 1
18 and life 2
still'loving 6
我恨你 5
SAMPLE OUTPUT
1
because of you
6
第一个清晨
18 and life
太早
不完整的旋律
我恨你
still'loving
下面是我写的代码,oj上提交是Wrong Answer 不知道哪里错了,求高人看看。。。
#include <iostream>
#include <string>
#include<math.h>
#include<algorithm>
using namespace std;
#define MAX 5001
struct TSong
{
char ch[52];
};
struct TLike
{
char st[52];
int p;
};
TSong tsong[MAX];
TLike tlsong[MAX];
void vSort(int n);
bool cmp(const TLike &A,const TLike &B);
void vOut(int n);
void vInput(int n);
int nDevi(int n);
void vInit();
int main()
{
int N,q;
while(cin>>N)
{
vInit();
vInput(N);
q=(nDevi(N))-1;
if(0==q)
cout<<q<<endl;
else
{
vSort(q);
vOut(q);
}
}
return 0;
}
void vInput(int n)
{
int i;
for(i=1;i<=n;i++)
{
fflush(stdin);
gets(tsong[i].ch);
}
}
int nDevi(int n)
{
int i,len,k,j,temp,npow;
k=1;
for(i=1;i<=n;i++)
{
len=strlen(tsong[i].ch);
j=len-1;
temp=0;
npow=0;
while(1)
{
while(tsong[i].ch[j]<='9'&&tsong[i].ch[j]>='0')
{
temp=temp+(tsong[i].ch[j]-'0')*pow(10,npow);
j--;
npow++;
}
if(tsong[i].ch[j]==' ')
{
tlsong[k].p=temp;
for(int q=0;q<j;q++)
{
tlsong[k].st[q]=tsong[i].ch[q];
}
k++;
break;
}
else
break;
}
}
return k;
}
void vOut(int n)
{
int i;
cout<<n<<endl;
for(i=1;i<=n;i++)
{
printf("%s\n",tlsong[i].st);
}
}
void vSort(int n)
{
sort(&tlsong[1],&tlsong[n+1],cmp);
}
bool cmp(const TLike &A,const TLike &B)
{
return A.p<B.p;
}
void vInit()
{
memset(tlsong,'\0',sizeof(tlsong));
}
[ 本帖最后由 newthf 于 2012-11-28 17:56 编辑 ]