标签:ali mono 而且 递增 字符 需要 har 偶数 部分
1 /**************************************************** 2 PAT B1027 打印沙漏 3 ****************************************************/ 4 #include <iostream> 5 #include <cmath> 6 7 using namespace std; 8 9 int main() { 10 char symbol; 11 int store = 0; 12 13 cin >> store >> symbol; 14 15 int maxEdgeSize = int(sqrt(2.0 * (store + 1))) - 1; //最大边的符号个数 16 maxEdgeSize = maxEdgeSize % 2 == 0 ? maxEdgeSize - 1 : maxEdgeSize; //最大边的符号个数肯定是奇数 17 18 int usedCont = 0; //使用的符号个数 19 int triLevel = (maxEdgeSize - 1) / 2; //沙漏上部三角形的层数 20 //输出沙漏上不部分的倒三角 21 for (int i = 0; i <= triLevel; ++i) { 22 for (int j = 0; j < i; ++j) cout << ‘ ‘; 23 for (int j = 0; j < maxEdgeSize - 2 * i; ++j) { 24 cout << symbol; 25 usedCont++; 26 } 27 cout << endl; 28 } 29 30 //输出沙漏剩余的部分 31 for (int i = 1; i <= triLevel; ++i) { 32 for (int j = 0; j < triLevel - i; ++j) cout << ‘ ‘; 33 for (int j = 0; j < 1 + 2 * i; ++j) { 34 cout << symbol; 35 usedCont++; 36 } 37 cout << endl; 38 } 39 40 //输出剩余符号个数 41 cout << store - usedCont; 42 43 return 0; 44 }
1 //获取1100以内所有能组成的沙漏包含的符号数 2 int cost = 1; 3 4 cout << cost << ", "; 5 for (int i = 1; cost < 1100; ++i) { 6 cost += 2 * (2 * i + 1); 7 8 cout << cost << ", "; 9 }
1 /**************************************************** 2 PAT B1027 打印沙漏 3 ****************************************************/ 4 #include <iostream> 5 6 using namespace std; 7 8 //1100个字符时所有可能组成的沙漏的消耗 9 const int COST[23] = { 1, 7, 17, 31, 49, 71, 97, 127, 161, 199, 241, 287, 337, 391, 449, 511, 577, 647, 721, 799, 881, 967, 1057 }; 10 11 //获取最大可输出漏斗的下三角的层数 12 int myFind(int left, int right, int num) { 13 int mid = 0; 14 while (left < right) { 15 mid = (left + right) / 2; 16 if (right - left == 1) { 17 return left; 18 } 19 else if (COST[mid] == num) { 20 return mid; 21 } 22 else { 23 COST[mid] > num ? right = mid : left = mid; 24 } 25 } 26 } 27 28 int main() { 29 char symbol; 30 int store = 0; 31 32 cin >> store >> symbol; 33 34 //获取最大可输出漏斗的下三角的层数 35 int triLevel = myFind(0, 22, store); 36 37 //输出漏斗上面的部分 38 for (int i = 0; i < triLevel; ++i) { 39 for (int j = 0; j < i; ++j) cout << ‘ ‘; 40 for (int j = 0; j < 2 * (triLevel - i) + 1; ++j) cout << symbol; 41 cout << endl; 42 } 43 44 //输出漏斗下面的三角形 45 for (int i = 0; i <= triLevel; ++i) { 46 for (int j = 0; j < triLevel - i; ++j) cout << ‘ ‘; 47 for (int j = 0; j < 2 * i + 1; ++j) cout << symbol; 48 cout << endl; 49 } 50 51 //输出剩余符号数 52 cout << store - COST[triLevel]; 53 54 return 0; 55 }
标签:ali mono 而且 递增 字符 需要 har 偶数 部分
原文地址:https://www.cnblogs.com/mrdragon/p/11401993.html