国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用緩沖區實現“向量分解” - Power of Thor - Episode 1 [CodingGame技巧總結]

2019-11-11 02:53:56
字體:
來源:轉載
供稿:網友

問題描述

Your PRogram must allow Thor to reach the light of power.

題目地址: https://www.codingame.com/ide/puzzle/power-of-thor-episode-1 八個方向

原始解法

通過嵌套的if條件語句來枚舉各類情況:

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;/** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. * --- * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. **/int main(){ int lightX; // the X position of the light of power int lightY; // the Y position of the light of power int initialTX; // Thor's starting X position int initialTY; // Thor's starting Y position cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore(); int currentX = initialTX; int currentY = initialTY; // game loop while (1) { int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line. cin >> remainingTurns; cin.ignore(); // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; // if both offset, then move in diaganol line if (currentX - lightX > 0) { if (currentY - lightY > 0){ cout << "NW" << endl; currentX--; currentY--; } else if (currentY - lightY <0){ cout << "SW" << endl; currentX--; currentY++; cerr << currentX << endl; }else { cerr << "now Y same" <<endl; cout << "W" <<endl; currentX--; } } else if (currentX - lightX < 0) { if (currentY - lightY > 0){ cout << "NE" << endl; currentX++; currentY--; } else if (currentY - lightY <0){ cout << "SE" << endl; currentX++; currentY++; }else { cout << "E" <<endl; currentX++; } } else { cerr << "now X same" <<endl; if (currentY - lightY > 0){ cout << "N" << endl; currentY--; } else if (currentY - lightY < 0){ cout << "S" << endl; currentY++; }else { // in position cerr << "now Y same" <<endl; } } // A single line providing the move to be made: N NE E SE S SW W or NW // cout << "SE" << endl; }}

“向量分解”解法

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;/** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. * --- * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. **/int main(){ int lightX; // the X position of the light of power int lightY; // the Y position of the light of power int initialTX; // Thor's starting X position int initialTY; // Thor's starting Y position cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore(); int dx = lightX - initialTX; int dy = lightY - initialTY; // game loop while (1) { int remainingTurns; cin >> remainingTurns; cin.ignore(); // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; if (dy > 0) { cout << "S"; dy--; } if (dy < 0) { cout << "N"; dy++; } if (dx > 0) { cout << "E"; dx--; } if (dx < 0) { cout << "W"; dx++; } cout << endl; // A single line providing the move to be made: N NE E SE S SW W or NW }}

技巧分析

在物理學中可以對速度向量進行向量分解,最常用的是分解為X、Y方向兩個速度分量。

在C++實現中,雖然我們不能簡單引入速度這個概念,但是通過緩沖區的暫存,可以讓我們通過單獨判定X、Y方向的關系,分別向緩沖區中寫入內容,最后用endl一并輸出。

潛在問題是如果向緩沖區中寫出過多字符,可能因為緩沖區已滿而自動輸出了。應該可以通過臨時變量來解決。

技巧泛化

當輸出結果與判定條件都可以進行某種對應形式的分解時,可以適當利用緩沖區(輸出緩沖區,或者臨時變量)達到分解的目的,從而簡化程序邏輯并提高效率。

參考

CodingGame ,ower of Thor - Episode 1,C++最高票答案,2017.02.07


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大安市| 昆明市| 丹寨县| 彩票| 河池市| 嵊泗县| 长春市| 西乌| 栾川县| 财经| 华亭县| 绵竹市| 林州市| 双辽市| 威海市| 阿城市| 临西县| 葵青区| 英超| 邹城市| 福贡县| 武安市| 措勤县| 太和县| 隆子县| 漳浦县| 通渭县| 吴堡县| 武川县| 平武县| 汉寿县| 名山县| 呈贡县| 无锡市| 南陵县| 南木林县| 浠水县| 武城县| 基隆市| 南陵县| 霞浦县|