您好,欢迎访问一九零五行业门户网

打印最短路径以在 C 程序中在屏幕上打印字符串

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] != '\0' 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 < n2 print move down cy++ end print destination reached i++step 2 -> in main() declare char str[] = {"hup"} call printpath(str)stop
example的中文翻译为:示例#include <stdio.h>void printpath(char str[]){ int i = 0; // start from character 'a' present at position (0, 0) int cx = 0, cy = 0; while (str[i] != '\0'){ // 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 程序中在屏幕上打印字符串。的详细内容。
其它类似信息

推荐信息