杭电OJ刷题

1003 Max Sum
1005 Number Sequence
1008 Elevator
1009 FatMouse’ Trade
1106 排序

1003

#include
using namespace std;
#define LL long long
LL a[100001];
int main() {
    int n;
    cin>>n;
    for(int k=0;k>m;
        for(int i=0;i>a[i];
        }
        for(int i=0;imax) {
                end = i;
                start = flag;
                max = sum;
            }
            if(sum<0) {
                sum = 0;
                flag = i + 1;
            }
            
        }
        cout<<"Case "<

1005

此题关键是找规律,否则,用递归会超内存,用for会超时。根据题意,fn = a*(fn-1)%7+b*(fn-2)%7,由此可知,fn可能的情况有7*7=49种,对于不同的a,b,开始循环的值不同,无论如何,都会在49轮后出现循环,所以只需将n%49即可。

#include
using namespace std;
int fn(int a, int b, int n) {
	n = n % 49;
	if (n ==1 || n == 2) return 1;
	int sum1 = 1, sum2 = 1;
	for(int i=3; i<=n; i++) {
		int sum3 = (a * sum2 + b * sum1) % 7;
		sum1 = sum2;
		sum2 = sum3;
	}
	return sum2;
}
int main() {
	while (1) {
		int a, b, n;
		cin >> a >> b >> n;
		if (a == 0 && b == 0 && n == 0) break;
		cout << fn(a, b, n) << endl;
	}
	return 0;
}

1008

#include
using namespace std;
int main() {
	int sum = 0;
	int n, pos = 0;
	while (cin >> n && n != 0) {
		sum = 0;
		pos = 0;
		for (int i = 0;i < n;i++) {
			int re;
			cin >> re;
			int f = re - pos;
			if (f > 0) {
				sum += f * 6;
				sum += 5;
			}
			else {
				sum += (-f) * 4;
				sum += 5;
			}
			pos = re;
		}
		cout << sum << endl;
	}
	return 0;
}

1009 FatMouse' Trade

#include
#include
#include
using namespace std;
struct node {
	int j;
	int f;
	double per;
};
bool cmp(node x, node y) {
	if(x.per>y.per) return true;
	return false;
}
int main() {
	int m, n;
	while(cin>>m>>n) {
		struct node a[1001];
		double max = 0;
		if(m == -1 && n == -1) break;
		for(int i=0;i>a[i].j>>a[i].f;
			a[i].per = a[i].j*1.0/a[i].f;
		}
		sort(a, a+n, cmp);
		for(int i=0;i=a[i].f) {
				max += a[i].j;
				m -= a[i].f;
			} else {
				max += a[i].per * m;
				m -= a[i].f;
			}
		}
		printf("%.3f\n", max);
	}
	return 0;
}

排序

错误点:不能有连续的5

#include
#include
#include
#include
using namespace std;
int str_to_int(string s) {
	int sum = 0;
	for (int i = 0;i < s.length();i++) {
		sum = sum * 10 + (s[i]-'0');
	}
	return sum;
}
bool cmp(const int &a, const int &b) {
	return a < b;
}
int main() {
	string s;
	while (cin >> s) {
		vector v;
		string portion = "";
		s = s + "5";
		for (int i = 0;i < s.length();i++) {
			if (portion == "" && s[i] == '5') {
				continue;
			}
			if (s[i] == '5') {
				v.push_back(str_to_int(portion));
				portion = "";
			}
			else {
				portion += s[i];
			}
		}
		sort(v.begin(), v.end(), cmp);
		for (int j = 0;j < v.size();j++) {
			if (j != v.size() - 1) {
				cout << v[j] << " ";
			}
			else {
				cout << v[j];
			}
		}
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(杭电OJ刷题)