您的位置:首页 > 编程语言 > C语言/C++

北航OJ 0031~0037 2015级C++第二次上机

2018-01-30 20:29 1156 查看
0032 - A - cool_breeze的袜子

开心的天数是较小的数b,不开心的天数是a−b2.

#include <stdio.h>
int main(){
int a,b,t;
while(scanf("%d%d",&a,&b)==2){
if(a<b){t=a;a=b;b=t;}
printf("%d %d\n",b,(a-b)>>1);
}
}


0034 - B - 判断三角形

用英文写就算了,担心同学们看不懂老师还写了几个中文注释(- -)||

#include <stdio.h>
int main(){
int a,b,c,t;
while(scanf("%d%d%d",&a,&b,&c)==3){
if(a+b>c&&a+c>b&&b+c>a){
if(!(a-b&am
14366
p;&a-c&&b-c))printf("perfect\n");
else{
if(a>b){t=a;a=b;b=t;}
if(b>c){t=b;b=c;c=t;}
if(a*a+b*b==c*c)printf("good\n");
else printf("just a triangle\n");
}
}
else printf("wrong\n");
}
}


0036 - C - jhljx水水的签到题

这道题其实是检验实数的阿基米德性,没上过数分还不知道这种题是怎么出出来的…

#include <stdio.h>
int main(){
int n,m;
while(scanf("%lld%lld",&n,&m)==2)printf("%lld %lld\n",n/m,m*(n/m));
}


0031 - D - 神奇的桌子

模拟,不用想太多,朴素方法能过。但事实上使用质因数分解,这道题能降低大量的复杂度。

#include <stdio.h>
#include <math.h>
int main(){
int i,n,x,cnt,t;
while(scanf("%d%d",&n,&x)==2){
cnt=0;//t=sqrt(n)+1;
for(i=1;i<=n;i++){
if(!(x%i)&&x/i<=n)cnt++;//if(i*i==x)cnt--;}
}
printf("%d\n",cnt);
}
}


0037 - E - 水水的比较大小

考察精度。

#include <cstdio>
#include <cmath>
int main(){
double a,b;
while(scanf("%lf%lf",&a,&b)==2){
if(fabs(a-b)<=1e-8)printf("nakezhenchun\n");
else if(a>b)printf("woshibukezhanshengde\n");
else printf("wohenbaoqian\n");
}
}


0035 - F - 活着的数

设活着的数a,b,c

c+2=ab+2a+2b+2+2=(a+2)(b+2)

设活着的数c1,c2则:

c+2=(c1+2)(c2+2)=(c3+2)(c4+2)(c5+2)(c6+2)=...=(a+2)p(b+2)q=3p5q,p,q∈N

#include <stdio.h>
int main(){
int n;
while(scanf("%d",&n)==1){n+=2;
while(n%3==0)n/=3;while(n%5==0)n/=5;
if(n==1)printf("Yes\n");else printf("No\n");
}
}


因为没有二分所以搜索次数过多,打表比标准做法还慢…

#include <stdio.h>
int is[134]={1,3,7,13,23,25,43,73,79,123,133,223,241,373,403,623,673,727,1123,
1213,1873,2023,2185,3123,3373,3643,5623,6073,6559,9373,10123,10933,
15623,16873,18223,19681,28123,30373,32803,46873,50623,54673,59047,
78123,84373,91123,98413,140623,151873,164023,177145,234373,253123,
273373,295243,390623,421873,455623,492073,531439,703123,759373,
820123,885733,1171873,1265623,1366873,1476223,1594321,1953123,2109373,
2278123,2460373,2657203,3515623,3796873,4100623,4428673,4782967,5859373,
6328123,6834373,7381123,7971613,9765623,10546873,11390623,12301873,
13286023,14348905,17578123,18984373,20503123,22143373,23914843,
29296873,31640623,34171873,36905623,39858073,43046719,48828123,52734373,
56953123,61509373,66430123,71744533,87890623,94921873,102515623,
110716873,119574223,146484373,158203123,170859373,184528123,199290373,
244140623,263671873,284765623,307546873,332150623,439453123,474609373,
512578123,553584373,732421873,791015623,854296873,922640623,1220703123,1318359373,1423828123,1537734373};
int main(){
int i,n,flag;
while(scanf("%d",&n)==1){
flag=1;
for(i=0;i<134;i++){if(is[i]==n){flag=0;break;}}
if(flag)printf("No\n");
else printf("Yes\n");
}
}


0033 - G - 感受学长的爱意吧(雾)

用高中解析几何的知识,就能解出来,现在看看这个代码写得挺糟,不过懒得改了。

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int cnt=0;
struct point{double x,y;}pt[5];
bool cmp(point a,point b){if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}
void judge(double A1,double B1,double C1,double A2,double B2,double C2){
double u=A1*B2-A2*B1;
if(u!=0){
pt[cnt].x=(B1*C2-B2*C1)/u+1e-4;
pt[cnt].y=(A2*C1-A1*C2)/u+1e-4;
for(int j=0;j<cnt;j++){
if(fabs(pt[j].x-pt[cnt].x)<1e-5&&fabs(pt[j].y-pt[cnt].y)<1e-5)cnt--;
}
cnt++;
}
}
int main(){
double a,b,c,d,e,f,g,h,i;int j;
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f,&g,&h,&i)==9){
cnt=0;
judge(a,b,c,d,e,f);judge(d,e,f,g,h,i);judge(a,b,c,g,h,i);
sort(pt,pt+cnt,cmp);
printf("%d\n",cnt);
if(cnt){
for(j=0;j<cnt;j++)printf("%.2lf %.2lf\n",pt[j].x,pt[j].y);
}
memset(pt,0,sizeof(point)*cnt);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: