Life of xhu

About

HOJ_1477 & HOJ_1480 & HOJ_1481

Mar 23, 2014

  |   #HOJ

复习面向对象编程ing,顺便刷几道水题。

1477:题意很简单,就是给两个字符串st,看前者中的字符能不能按顺序从后者中找出来,这个题有一个大坑是给的字符串会很长,我用10000都段错误了,最后干脆设为最大的1000000才搞定。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main(){
    char s[1000000], t[1000000];
    while(scanf("%s %s", s, t) != EOF){
        int lens = strlen(s), lent = strlen(t), i, j;
        for(i = 0, j = 0; j < lent; j++){
            if(t[j] == s[i] && i < lens)
                i++;
        }
        if(i == lens)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}

1480:题意就是给一堆点的坐标,按顺序输出两个点的中点坐标,保留六位小数。

代码如下:

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

int main(){
    double x[10000], y[10000];
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; i++)
            cin >> x[i] >> y[i];
        cout << n;
        for(int i = 0; i < n; i++){
            cout << ' ' << fixed << setprecision(6) << (x[i] + x[(i + 1) % n]) / 2.0 << ' ' << (y[i] + y[(i + 1) % n]) / 2.0;
        }
        cout << endl;
    }
}

1481:这道题其实就是上一道题的逆运算,即根据中点求出两点坐标,很简单,列方程组即可求解。

代码如下:

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

int main(){
    double x[10000], y[10000], init_x, init_y;
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; i++)
            cin >> x[i] >> y[i];
        cout << n;
        init_x = init_y = 0;
        for(int i = 0; i < n; i++){
            if(i % 2){
                init_x -= x[i];
                init_y -= y[i];
            }else{
                init_x += x[i];
                init_y += y[i];
            }
        }
        cout << ' ' << fixed << setprecision(6) << init_x << ' ' << fixed << setprecision(6) << init_y;
        for(int i = 0; i < n - 1; i++){
            init_x = x[i] * 2 - init_x;
            init_y = y[i] * 2 - init_y;
            cout << ' ' << fixed << setprecision(6) << init_x << ' ' << fixed << setprecision(6) << init_y;
        }
        cout << endl;
    }
}