ACM问题 我的代码哪里错了?? http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1469
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1469我的代码:#include<iostream...
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1469
我的代码:
#include<iostream>
#include<set>
#include<cstdio>
#include<string>
using namespace std;//1469
int main()
{
set <string> st;
set<string>::iterator find;
string s = "";
int i,k,m,n,count=1,sum;
char c;
while(scanf("%d",&k)!=EOF)
{
s="";
st.clear();
sum=0;
count=1;
getchar();
c=0;
while (true)
{
while (c != '\n')
{
while ( (c=getchar()) && ( (c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='\'' ) && c!='\n')
{
if(c>='A'&&c<='Z')
s += c-'A'+'a';
else
s+=c;
}
if (s.length())
st.insert(s);
s = "";
}
if(c=='\n')
break;
}
printf("Output #%d: number of distinct words in paragraph 1: %d\n",count++,st.size());
printf("Output #%d: the %dth smallest distinct in paragraph 1: ",count++,k);
for(find=st.begin(),i=1;find!=st.end();find++,i++)
{
if(i==k)
{
cout<<*find<<endl;
break;
}
}
scanf("%d%d",&m,&n);
s="";
st.clear();
getchar();
c=0;
while (true)
{
while (c != '\n')
{
while ((c = getchar()) && ( (c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='\'' ))
{
if(c>='A'&&c<='Z')
s += c-'A'+'a';
else
s+=c;
}
if (s.length())
st.insert(s);
s = "";
}
if(c=='\n')
break;
}
printf("Output #%d: number of distinct words in paragraph 2: %d\n",count++,st.size());
printf("Output #%d: the %dth smallest distinct in paragraph 2: ",count++,m);
for(find=st.begin(),i=1;find!=st.end();find++,i++)
{
if(i==m)
{
cout<<*find<<endl;
break;
}
}
for(find=st.begin();find!=st.end();find++)
{
if(string(*find).length()==n)
sum++;
}
printf("Output #%d: the number of distinct words in paragraph 2 of length=%d: %d\n",count++,n,sum);
}
return 0;
} 展开
我的代码:
#include<iostream>
#include<set>
#include<cstdio>
#include<string>
using namespace std;//1469
int main()
{
set <string> st;
set<string>::iterator find;
string s = "";
int i,k,m,n,count=1,sum;
char c;
while(scanf("%d",&k)!=EOF)
{
s="";
st.clear();
sum=0;
count=1;
getchar();
c=0;
while (true)
{
while (c != '\n')
{
while ( (c=getchar()) && ( (c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='\'' ) && c!='\n')
{
if(c>='A'&&c<='Z')
s += c-'A'+'a';
else
s+=c;
}
if (s.length())
st.insert(s);
s = "";
}
if(c=='\n')
break;
}
printf("Output #%d: number of distinct words in paragraph 1: %d\n",count++,st.size());
printf("Output #%d: the %dth smallest distinct in paragraph 1: ",count++,k);
for(find=st.begin(),i=1;find!=st.end();find++,i++)
{
if(i==k)
{
cout<<*find<<endl;
break;
}
}
scanf("%d%d",&m,&n);
s="";
st.clear();
getchar();
c=0;
while (true)
{
while (c != '\n')
{
while ((c = getchar()) && ( (c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='\'' ))
{
if(c>='A'&&c<='Z')
s += c-'A'+'a';
else
s+=c;
}
if (s.length())
st.insert(s);
s = "";
}
if(c=='\n')
break;
}
printf("Output #%d: number of distinct words in paragraph 2: %d\n",count++,st.size());
printf("Output #%d: the %dth smallest distinct in paragraph 2: ",count++,m);
for(find=st.begin(),i=1;find!=st.end();find++,i++)
{
if(i==m)
{
cout<<*find<<endl;
break;
}
}
for(find=st.begin();find!=st.end();find++)
{
if(string(*find).length()==n)
sum++;
}
printf("Output #%d: the number of distinct words in paragraph 2 of length=%d: %d\n",count++,n,sum);
}
return 0;
} 展开
2个回答
2011-04-15
展开全部
K
//1.求凸包,这是为了算包含关系,如果凸包点数为4,
//判断4个点是否刚好属于其中一个矩形的,如果是A,说明A包含B,
//如果是B,则B包含A,否则进入重叠判断
//2.判断重叠,穷举每条线段判断是否相交即可
#include<iostream>
#include <algorithm>
#include <CMATH>
using namespace std;
const double eps=1e-10; //允许的误差
struct point //点的结构体
{
double x, y;
friend bool operator!=(const point &l, const point &r)
{
if (l.x!=r.x && l.y!=r.y)
{
return true;
}
return false;
}
friend bool operator < (const point &l, const point &r){
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
};
double min(double a, double b)
double max(double a, double b)
bool inter(point a, point b, point c, point d) //判断线段相交
{
if ( min(a.x, b.x) > max(c.x, d.x) ||
min(a.y, b.y) > max(c.y, d.y) ||
min(c.x, d.x) > max(a.x, b.x) ||
min(c.y, d.y) > max(a.y, b.y) ) return 0;
double h, i, j, k;
h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}
bool mult(point sp, point ep, point op)
{
return (sp.x - op.x) * (ep.y - op.y)
>= (ep.x - op.x) * (sp.y - op.y);
}
point num[8];
point t1[8];
point t2[8];
int graham(point pnt[], int n, point res[]) //求凸包
{
int i, len, k = 0, top = 1;
sort(pnt, pnt + n);
if (n == 0) return 0; res[0] = pnt[0];
if (n == 1) return 1; res[1] = pnt[1];
if (n == 2) return 2; res[2] = pnt[2];
for (i = 2; i < n; i++) {
while (top && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
len = top; res[++top] = pnt[n - 2];
for (i = n - 3; i >= 0; i--) {
while (top!=len && mult(pnt[i], res[top],
res[top-1])) top--;
res[++top] = pnt[i];
}
return top; // 返回凸包中点的个数
}
int main()
{
int i,j,k,l;
while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
&num[0].x,&num[0].y,&num[1].x,&num[1].y,&num[2].x,&num[2].y,
&num[3].x,&num[3].y,&num[4].x,&num[4].y,&num[5].x,&num[5].y,
&num[6].x,&num[6].y,&num[7].x,&num[7].y)!=EOF)
{
for (i=0;i<8;i++)
t2[i]=num[i];
sort(t2+4, t2 + 8);
sort(t2, t2 + 4);
int sum=graham(num,8,t1);
if(sum==4)
{
sort(t1,t1+4);
for (i=0;i<4;i++)
{
if(t2[i+4]!=t1[i])
break;
}
if (i==4)
{
printf("IN\n");
continue;;
}
for (i=0;i<4;i++)
{
if(t2[i]!=t1[i])
break;
}
if (i==4)
{
printf("OUT\n");
continue;;
}
}
else
{
bool judge=false;
for (i=0;i<3;i++)
{
for (j=i+1;j<4;j++)
{
for (k=4;k<8;k++)
{
for (l=k+1;l<8;l++)
{
if (inter(t2[i],t2[j],t2[k],t2[l]))
{
judge=true;
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
printf("OVERLAPPED\n");
}
else
printf("OUT\n");
}
}
return 0;
}
另外,站长团上有产品团购,便宜有保证
//1.求凸包,这是为了算包含关系,如果凸包点数为4,
//判断4个点是否刚好属于其中一个矩形的,如果是A,说明A包含B,
//如果是B,则B包含A,否则进入重叠判断
//2.判断重叠,穷举每条线段判断是否相交即可
#include<iostream>
#include <algorithm>
#include <CMATH>
using namespace std;
const double eps=1e-10; //允许的误差
struct point //点的结构体
{
double x, y;
friend bool operator!=(const point &l, const point &r)
{
if (l.x!=r.x && l.y!=r.y)
{
return true;
}
return false;
}
friend bool operator < (const point &l, const point &r){
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
};
double min(double a, double b)
double max(double a, double b)
bool inter(point a, point b, point c, point d) //判断线段相交
{
if ( min(a.x, b.x) > max(c.x, d.x) ||
min(a.y, b.y) > max(c.y, d.y) ||
min(c.x, d.x) > max(a.x, b.x) ||
min(c.y, d.y) > max(a.y, b.y) ) return 0;
double h, i, j, k;
h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}
bool mult(point sp, point ep, point op)
{
return (sp.x - op.x) * (ep.y - op.y)
>= (ep.x - op.x) * (sp.y - op.y);
}
point num[8];
point t1[8];
point t2[8];
int graham(point pnt[], int n, point res[]) //求凸包
{
int i, len, k = 0, top = 1;
sort(pnt, pnt + n);
if (n == 0) return 0; res[0] = pnt[0];
if (n == 1) return 1; res[1] = pnt[1];
if (n == 2) return 2; res[2] = pnt[2];
for (i = 2; i < n; i++) {
while (top && mult(pnt[i], res[top], res[top-1]))
top--;
res[++top] = pnt[i];
}
len = top; res[++top] = pnt[n - 2];
for (i = n - 3; i >= 0; i--) {
while (top!=len && mult(pnt[i], res[top],
res[top-1])) top--;
res[++top] = pnt[i];
}
return top; // 返回凸包中点的个数
}
int main()
{
int i,j,k,l;
while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
&num[0].x,&num[0].y,&num[1].x,&num[1].y,&num[2].x,&num[2].y,
&num[3].x,&num[3].y,&num[4].x,&num[4].y,&num[5].x,&num[5].y,
&num[6].x,&num[6].y,&num[7].x,&num[7].y)!=EOF)
{
for (i=0;i<8;i++)
t2[i]=num[i];
sort(t2+4, t2 + 8);
sort(t2, t2 + 4);
int sum=graham(num,8,t1);
if(sum==4)
{
sort(t1,t1+4);
for (i=0;i<4;i++)
{
if(t2[i+4]!=t1[i])
break;
}
if (i==4)
{
printf("IN\n");
continue;;
}
for (i=0;i<4;i++)
{
if(t2[i]!=t1[i])
break;
}
if (i==4)
{
printf("OUT\n");
continue;;
}
}
else
{
bool judge=false;
for (i=0;i<3;i++)
{
for (j=i+1;j<4;j++)
{
for (k=4;k<8;k++)
{
for (l=k+1;l<8;l++)
{
if (inter(t2[i],t2[j],t2[k],t2[l]))
{
judge=true;
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
break;
}
}
if (judge)
{
printf("OVERLAPPED\n");
}
else
printf("OUT\n");
}
}
return 0;
}
另外,站长团上有产品团购,便宜有保证
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询