PAT-乙-1070 1070 結繩 (25 分)
程式碼
#include <iostream> #include <algorithm> using namespace std; int main() { int n; cin>>n; double a[n]; for(int i=0; i<n; i++){ cin>>a[i]; } sort(a, a+n); for(int i=0; i<n; i++){ int tmp1 = a[i]; int tmp2 = a[i+1]; a[i+1] = (tmp1+tmp2)/2; } cout<<(int)a[n-1]<<endl; return 0; }
註解
1、準確理解題目意思是關鍵。對摺後成為另一條繩子,所以長度減半。要想繩子最終長度最大,等價於將繩子長度排序,從小到大依次折半(這樣才能使損失的長度最小),最終得到的繩子是最大長度的。
結果
相關推薦
PAT-乙-1070 1070 結繩 (25 分)
程式碼 #include <iostream> #include <algorithm> using namespace std; int main() { int n; cin>>n; double a[n]; for(in
PAT乙級 1070 結繩 (25 分)
給定一段一段的繩子,你需要把它們串成一條繩。每次串連的時候,是把兩段繩子對摺,再如下圖所示套接在一起。這樣得到的繩子又被當成是另一段繩子,可以再次對摺去跟另一段繩子串連。每次串連後,原來兩段繩子的長度就會減半。 給定 N 段繩子的長度,你需要找出它們能串成的繩子的最大長度。 輸入格式:
1070 結繩(25 分)
給定一段一段的繩子,你需要把它們串成一條繩。每次串連的時候,是把兩段繩子對摺,再如下圖所示套接在一起。這樣得到的繩子又被當成是另一段繩子,可以再次對摺去跟另一段繩子串連。每次串連後,原來兩段繩子的長度就會減半。給定 N 段繩子的長度,你需要找出它們能串成的繩子的最大長度。輸入
PAT-乙-1065 1065 單身狗 (25 分)
程式碼 #include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAX = 100000; int main() {
PAT-乙-1015 1015 德才論 (25 分)
程式碼 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std;
PAT-乙-1030 1030 完美數列 (25 分)
程式碼 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N; lon
pat 1060 愛丁頓數(25 分)
從大到小 algorithm 英國 pri sin spa span iostream sel 英國天文學家愛丁頓很喜歡騎車。據說他為了炫耀自己的騎車功力,還定義了一個“愛丁頓數” E ,即滿足有 E 天騎車超過 E 英裏的最大整數 E。據說愛丁頓自己的 E 等於87。 現
PAT-乙-1072 1072 開學寄語 (20 分)
程式碼 #include <iostream> using namespace std; int main() { int N, M; scanf("%d %d", &N, &M); int a[10000] = {0}; for(i
PAT-乙-1067 1067 試密碼 (20 分)
程式碼 #include <iostream> using namespace std; int main() { string s; int n; cin>>s>>n; string t; getline(cin, t);
PAT-乙-1066 1066 影象過濾 (15 分)
程式碼 #include <stdio.h> int main() { int n, m; int a, b, c; scanf("%d %d %d %d %d", &n, &m, &a, &b, &c); int
PAT-乙-1064 1064 朋友數 (20 分)
程式碼 #include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; int main() { i
PAT-乙-1062 1062 最簡分數 (20 分)
程式碼 #include <iostream> #include <algorithm> using namespace std; int maxCommon(int a, int b) { if(a>b) { swap(a, b); }
PAT-乙-1061 1061 判斷題 (15 分)
程式碼 #include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int score[m] = {0}; int ans[m] = {0}; for(int
PAT-乙-1034 1034 有理數四則運算 (20 分)
程式碼 #include <iostream> #include <math.h> #include <stdio.h> using namespace std; struct fenshu { long long int fenzi; l
PAT-乙-1076 1076 Wifi密碼 (15 分)
程式碼 #include <iostream> using namespace std; int main() { int n; cin>>n; string ans; for(int i=0; i<n; i++){ string
【笨方法學PAT】1013 Battle Over Cities(25 分)
一、題目 It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/towa
【笨方法學PAT】1012 The Best Rank(25 分)
一、題目 To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programmin
【笨方法學PAT】1009 Product of Polynomials(25 分)
一、題目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each input
PAT 1082 Read Number in Chinese(25 分)
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative.
PAT 1010 一元多項式求導(25 分) C語言
PAT 1010 一元多項式求導(25 分) #include<stdio.h> int main() { int a, b; int flag=0; //設定flag指示是否已經經過一次求導運算,考慮輸出的空格問題 while(scanf("%