
杭电acm1160 一直runtime error
ProblemDescriptionFatMousebelievesthatthefatteramouseis,thefasteritruns.Todisprovethi...
Problem Description
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
#include <iostream>
#include <algorithm>
using namespace std;
struct speed
{
int w;
int s;
int num;
}a[1003];
bool cmp(speed a,speed b)
{
if(a.w!=b.w)
return a.w>b.w;
else
return a.s<b.s;
}
void main()
{
int n=0,i,j,b[1003],c[1003];
while(scanf("%d",&a[n].w)!=EOF)
{
scanf("%d",&a[n].s);
a[n].num=n+1;
n++;
}
sort(a,a+n,cmp);
b[0]=1;
intmax=1,k=0;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
if(a[j].s<a[i].s&&a[j].w>a[i].w&&b[j]>b[i]-1)
{
b[i]=b[j]+1;
}
if(b[i]>max)
{
k=i;
max=b[i];
}
}
cout<<max<<endl;
c[0]=a[k].num;
int h=k;
j=1;
for(i=k-1;max>1;i--)
{
if(b[i]=max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
{
c[j++]=a[i].num;
h=i;
max--;
}
}
for(i=0;i<j;i++)
cout<<c[i]<<endl;
} 展开
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
#include <iostream>
#include <algorithm>
using namespace std;
struct speed
{
int w;
int s;
int num;
}a[1003];
bool cmp(speed a,speed b)
{
if(a.w!=b.w)
return a.w>b.w;
else
return a.s<b.s;
}
void main()
{
int n=0,i,j,b[1003],c[1003];
while(scanf("%d",&a[n].w)!=EOF)
{
scanf("%d",&a[n].s);
a[n].num=n+1;
n++;
}
sort(a,a+n,cmp);
b[0]=1;
intmax=1,k=0;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
if(a[j].s<a[i].s&&a[j].w>a[i].w&&b[j]>b[i]-1)
{
b[i]=b[j]+1;
}
if(b[i]>max)
{
k=i;
max=b[i];
}
}
cout<<max<<endl;
c[0]=a[k].num;
int h=k;
j=1;
for(i=k-1;max>1;i--)
{
if(b[i]=max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
{
c[j++]=a[i].num;
h=i;
max--;
}
}
for(i=0;i<j;i++)
cout<<c[i]<<endl;
} 展开
1个回答
展开全部
1 while 循环结束条件错误。
2 判断条件错误
if(b[i]=max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
这里不用使用=号,
修改后:
#include <iostream>
#include <algorithm>
using namespace std;
struct speed
{
int w;
int s;
int num;
}a[1003];
bool cmp(speed a,speed b)
{
if(a.w!=b.w)
return a.w>b.w;
else
return a.s<b.s;
}
void main()
{
int n=9,i,j,b[1003]={0},c[1003]={0};
while(1)
{
scanf("%d",&a[n].w);
if(a[n].w == 0)
{
break;
}
scanf("%d",&a[n].s);
a[n].num=n+1;
n++;
}
sort(a,a+n,cmp);
b[0]=1;
int max=1,k=0;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
{
if(a[j].s<a[i].s&&a[j].w>a[i].w&&b[j]>b[i]-1)
{
b[i]=b[j]+1;
}
if(b[i]>max)
{
k=i;
max=b[i];
}
}
}
cout<<max<<endl;
c[0]=a[k].num;
int h=k;
j=1;
for(i=k-1;max>1;i--)
{
if(b[i]==max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
{
c[j++]=a[i].num;
h=i;
max--;
}
}
for(i=0;i<j;i++)
{
cout<<c[i]<<endl;
}
}
2 判断条件错误
if(b[i]=max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
这里不用使用=号,
修改后:
#include <iostream>
#include <algorithm>
using namespace std;
struct speed
{
int w;
int s;
int num;
}a[1003];
bool cmp(speed a,speed b)
{
if(a.w!=b.w)
return a.w>b.w;
else
return a.s<b.s;
}
void main()
{
int n=9,i,j,b[1003]={0},c[1003]={0};
while(1)
{
scanf("%d",&a[n].w);
if(a[n].w == 0)
{
break;
}
scanf("%d",&a[n].s);
a[n].num=n+1;
n++;
}
sort(a,a+n,cmp);
b[0]=1;
int max=1,k=0;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
{
if(a[j].s<a[i].s&&a[j].w>a[i].w&&b[j]>b[i]-1)
{
b[i]=b[j]+1;
}
if(b[i]>max)
{
k=i;
max=b[i];
}
}
}
cout<<max<<endl;
c[0]=a[k].num;
int h=k;
j=1;
for(i=k-1;max>1;i--)
{
if(b[i]==max-1&&a[h].s>a[i].s&&a[h].w<a[i].w)
{
c[j++]=a[i].num;
h=i;
max--;
}
}
for(i=0;i<j;i++)
{
cout<<c[i]<<endl;
}
}

2023-05-24 广告
电化学工作站chi760e连接不上的原因和解决方法如下:1. 网络问题:请检查您的网络连接是否正常,尝试重新连接或者更换网络环境,例如使用其他WiFi或者移动网络。2. 服务器故障或维护:如果电化学工作站使用的是远程服务器,可能是由于服务器...
点击进入详情页
本回答由GamryRaman提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询