加强赛第二轮题解

2017/3/13
类型:简单题练习


A.POJ 1003 Hangover

题解:先将板所能到达的距离打个表s[],每次传入距离c后寻找第一个比c大的s[i],**i **即为所求答案。

#include "iostream"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define eps 1e-6
double s[10000];
void init(){
  double sum ;
  for(int i=1;i<10000;i++){
    sum = 0;
    for(int j=1;j<=i;j++)
      sum += 1.0/(j+1);
    s[i]=sum;
  }
}
int main(){
  double c;
  init();
  while(scanf("%lf",&c)!=EOF && c){
    int ans;
    for(int i=1;i<10000;i++){
      if(s[i]-c>eps) { ans=i;break; }
    }
    printf("%d card(s)
",ans);
  }
  return 0;
}

B.POJ 2291 Rotten Ropes

题解:不断进行尝试,从第一根绳子开始测试,如果断掉绳子承受的重量比max_w大则更新max_w

#include "iostream"
#include "cstdio"
#include "algorithm"
using namespace std;

int t,n,s[1000+10],max_w;
void solve(){
  sort(s,s+n);
  int tmp = n;
  max_w=s[0]*n;
  for(int i=1;i<n;i++){
    max_w = max(max_w,s[i]*(n-i));
  }
}
int main(){
  scanf("%d",&t);
  while(t--){
    scanf("%d", &n);
    for(int i=0;i<n;i++)  scanf("%d",s+i);
    solve();
    printf("%d
",max_w);
  }
  return 0;
}

C.POJ 3086 Triangular Sums

**题解: **按照公式进行计算即可,为了避免重复计算浪费时间 sum 并不清空。

#include "iostream"
#include "cstdio"
using namespace std;

const int max_n = 310;
int t,n,kase;
long long T[max_n],W[max_n];
void init(){
  long long sum = 0;
  for(int i=1;i<max_n;i++){
    sum = sum + (i*(i+2)*(i+1)/2);
    W[i] = sum;
  }
}
int main(){
  init();
  kase=0;
  scanf("%d",&t);
  while(t--){
    scanf("%d", &n);
    printf("%d %d %lld
",++kase,n,W[n]);
  }
  return 0;
}

D.POJ 1663 Number Steps

题解:看出两个直线的方程,找到其在方程上点所具有的特点进行判断即可。特点是两个方程上如果x为奇数,那么点的step为x+y-1,为偶数则是x+y。

#include "iostream"
#include "cstdio"
using namespace std;

int main(){
  int t,x,y;
  scanf("%d",&t);
  while(t--){
    scanf("%d%d", &x,&y);
    if(x==y || x-2==y){
      if(x==y){
        if(x&1) printf("%d
", 2*x-1);
        else    printf("%d
", 2*x);
      }else{
        if(x&1) printf("%d
", x+y-1);
        else    printf("%d
", x+y);
      }
    }else printf("No Number
");
  }
  return 0;
}

E.POJ 1852 Ants

题解:
1.最短时间:当所有蚂蚁都朝着距离最近的那端爬,便不会相遇,所以最短时间就是这n只蚂蚁中所处位置最靠中间 的那一个爬到两端相对较短的那一端所需的时间。
2.最长时间:如果忽视蚂蚁的区别,当两个蚂蚁相遇时互相穿过,也就是说对于每个蚂蚁的最长时间为max(s[i],L-s[i])。

#include "iostream"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;

const int M = 1e6+10;
int t,n,L,s[M];
int Min , Max , Marki ;

void solve(){
  Min = Max = -M;
  for(int i=0;i<n;i++){
    Min = max(Min,min(s[i],L-s[i]));
    Max = max(Max,max(s[i],L-s[i]));
  }
}
int main(){
  scanf("%d",&t);
  while (t--) {
    scanf("%d%d",&L,&n);
    for(int i=0;i<n;i++)  scanf("%d", s+i);
    solve();
    printf("%d %d
",Min,Max);
  }
  return 0;
}
如要转载请注明转载出处:http://www.cnblogs.com/WArobot
原文地址:https://www.cnblogs.com/WArobot/p/6539112.html