POJ 3617 Best Cow Line

有點技巧的貪心

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int main() {
    int n;
    char a[2005];
    string str;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int i = 0, j = n - 1;
    while (i <= j) {
		bool left = false;  //  布爾變量,判斷相等的下一個,避免遞歸調用
		for (int x = 0; i + x <= j; x++) {
			if (a[i + x ] < a[j - x]) {
				left = true;
				break;
			} else if (a[i + x ] > a[j - x]){
				left = false;
				break;
			}
		}
        if (left) {
            str.push_back(a[i++]);
        } else {
            str.push_back(a[j--]);
        }
    }
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
		printf("%c", str[i]);
		count++;
		if (count == 80) {
			count = 0;
			printf("\n");
		}
    }
    if (count) {
		printf("\n");
    }
}


發佈了64 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章