
Given a string, the program must display the shortest path which will print the string over the screen using that shortest path.
Like screen will store alphabets in the format
A B C D EF G H I JK L M N OP Q R S TU V W X YZ
Example
的中文翻译为:
示例
Input: HUPOutput : Move DownMove DownMove Downdestination reachedMove LeftMove LeftMove DownMove DownMove Downdestination reachedMove Updestination reached
这里使用的方法是将字符存储在n x n矩阵中,并执行以下操作 −
If row difference is negative then move upIf row difference is positive then move downIf column difference is negative then go leftIf column difference is positive then we go right
算法
STARTStep 1 -> Declare Function void printpath(char str[]) Declare variable int i = 0 and cx=0 and cy=0 Loop While str[i] != ' ' Declare variable as int n1 = (str[i] - 'A') / 5 Declare variable as int n2 = (str[i] - 'B' + 1) % 5 Loop while cx > n1 Print move up cx— End Loop while cy > n2 Print Move Left Cy— End Loop while cx < n1 Print move down Cx++ End Loop while cy in main() Declare char str[] = {"HUP"} Call printpath(str)STOP
Example
的中文翻译为:
示例
#include void printpath(char str[]){ int i = 0; // start from character 'A' present at position (0, 0) int cx = 0, cy = 0; while (str[i] != ' '){ // find cordinates of next character int n1 = (str[i] - 'A') / 5; int n2 = (str[i] - 'B' + 1) % 5; // Move Up if destination is above while (cx > n1){ printf("Move Up"); cx--; } // Move Left if destination is to the left while (cy > n2){ printf("Move Left
"); cy--; } // Move down if destination is below while (cx < n1){ printf("Move Down
"); cx++; } // Move Right if destination is to the right while (cy < n2){ printf("Move Down
"); cy++; } // At this point, destination is reached printf("destination reached
"); i++; }}int main(int argc, char const *argv[]){ char str[] = {"HUP"}; printpath(str); return 0;}
输出
如果我们运行上面的程序,它将生成以下输出−
Move DownMove DownMove Downdestination reachedMove LeftMove LeftMove DownMove DownMove Downdestination reachedMove Updestination reached
以上就是打印最短路径以在 C 程序中在屏幕上打印字符串。的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444643.html
微信扫一扫
支付宝扫一扫