1. 程式人生 > 其它 >CCFCSP-201909-1-小明種蘋果(C++)

CCFCSP-201909-1-小明種蘋果(C++)

技術標籤:CCF-CSPccfcspc++

日誌:

1、algorithm標頭檔案:使用 stable_sort 穩定排序函式,這是做CCF用到的第二次了,真的很方便。
2、math.h標頭檔案:使用 abs 求絕對值函式。
在這裡插入圖片描述

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;


typedef struct AppleTree {
	int id;  // 從1開始
	int start_num;  // 最初的蘋果個數
	int end_num;
// 最終蘋果個數 int trip[1000]; // 至多1000輪 int sum_trip = 0; // 去掉的蘋果總數(負數) }AppleTree; // 因為sum_trip是負數,所以越小,絕對值越大,去掉的蘋果越多 bool cmp(AppleTree a, AppleTree b) { return a.sum_trip < b.sum_trip; } int main() { int n; //幾棵樹 int m; //幾輪 AppleTree trees[1000]; int sum_end_num = 0; cin >> n >> m; for
(int i = 0; i < n; i++) { trees[i].id = i + 1; cin >> trees[i].start_num; for (int j = 0; j < m; j++) { cin >> trees[i].trip[j]; trees[i].sum_trip += trees[i].trip[j]; //注意用加號 } trees[i].end_num = trees[i].start_num + trees[i].sum_trip; sum_end_num += trees[i]
.end_num; } stable_sort(trees, trees + n, cmp); //按照去掉的蘋果的數量絕對值由大到小排序,穩定排序 cout << sum_end_num <<" "<< trees[0].id << " " << abs(trees[0].sum_trip); return 0; }