poj.org 部分题目答案

1000

#include 

using namespace std;

int main()
{
    int a,b;
    cin >> a >> b;
    cout << a+b << endl;
    return 0;
}

在这里插入图片描述

1005

#include
const double pi = 3.14;
using namespace std;
int main()
{
	int n;
	cin >> n;
	double r;
	double** cnt = new double* [n + 1];
	for (int i = 1; i <= n; i++)
	{
		cnt[i] = new double[2];
		cin >> cnt[i][0] >> cnt[i][1];
		r = cnt[i][0] * cnt[i][0] + cnt[i][1] * cnt[i][1];
		for (int k = 0; ; k++)
		{
			if (r < (100.0 * k) / pi) {
				printf("Property %d: This property will begin eroding in year %d.\n", i, k);
				break;
			}
		}
	}
	cout << "END OF OUTPUT.";
}

请添加图片描述

1075

#include
using namespace std;
// 注意减枝
class Solution {
public:
	void init()   // 初始化
	{
		char str[5];
		  int a = 0;
		for (int i = 0; i < 4; i++) {
			cin >> str;
			for (int k = 0; k < 4; k++) {
				cnt[a++] = str[k] == 'b' ? 0 : 1;
			}
		}

		min = 20;
		number = 0;
	}
	int check() {   // 检查是否全部同色
		for (int i = 1; i < 16; i++) {
			if (cnt[0] != cnt[i])  return 0;
		}  return 1;
	}
	void flip(int i) {   // 进行翻转
		int  x = i / 4;
		int y = i % 4;
		cnt[i] = !cnt[i];
		if (x > 0)  cnt[i - 4] = !cnt[i - 4];
		if (x < 3) cnt[i + 4] = !cnt[i + 4];
		if (y > 0) cnt[i - 1] = !cnt[i - 1];
		if (y < 3) cnt[i + 1] = !cnt[i + 1];
	}
	void fun(int i) {    // i表示处理到第几个棋子的下标
		if (i >= 16) {
			if(check())  // 最后的时候才检查
			min = min < number ? min : number;
			return;  // 函数的出口
		}
		else {
			fun(i + 1);   // 优先进行不翻转
			number++; int flag = 0;
			if (number < min) {
				flip(i);
				flag = 1;
				fun(i + 1);
			}
			if (flag) {
				flip(i);
			}
			number--;
		}
	}
	void out_print() {
		if (min == 20) cout << "Impossible"<< endl;
		else cout << min  << endl;
	}

private:
	int cnt[16];
	int min;   // poj 不允许在类中初始化,在init 中初始化
	int number;  

};

int main() {
	Solution answer;
	answer.init();
	answer.fun(0);
	answer.out_print();
	return 0;
}

在这里插入图片描述

3259

#include
#include
#include
using namespace std;
class Solution {
private:
    string s;
    int cur;
    char a[5];

    void Change(char i) {
        for (int j = 0; j != 5; j++)
            a[j] = ((i >> j) & 00000001);
    }

    bool Cal() {
        char c = s[cur];
        bool w, x;
        cur++;
        switch (c) {
        case 'p':
            return a[0];
        case 'q':
            return a[1];
        case 'r':
            return a[2];
        case 's':
            return a[3];
        case 't':
            return a[4];
        case 'N':
            return !Cal();
        case 'K':
            w = Cal();
            x = Cal();
            return (w && x);
        case 'A':
            w = Cal();
            x = Cal();
            return (w || x);
        case 'C':
            w = Cal();
            x = Cal();
            return (w <= x);
        case 'E':
            w = Cal();
            x = Cal();
            return (w == x);
        default:
            return 0;
        }
    }

public:
    void evaluateExpressions() {
        int f;
        while (1) {
            cin >> s;
            if (s == "0")
                break;
            f = 1;
            for (char i = 0; i != 32; i++) {
                cur = 0;
                Change(i);
                if (!Cal()) {
                    f = 0;
                    break;
                }
            }
            if (f == 1)
                cout << "tautology" << endl;
            else
                cout << "not" << endl;
        }
    }
};

int main() {
    Solution solution;
    solution.evaluateExpressions();
    return 0;
}

在这里插入图片描述

你可能感兴趣的:(算法,算法)