日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

跟随者数字解码_跟随模式的数字

發布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 跟随者数字解码_跟随模式的数字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

跟隨者數字解碼

Problem statement:

問題陳述:

Given a pattern containing only I's and D's. 'I' stands for increasing and 'D' for decreasing. Devise an algorithm to print the minimum number following that pattern. Digits are from 1-9 and digits can't repeat.

給定一個僅包含I和D的模式 。 “ I”代表增加, “ D”代表減少。 設計一種算法以按照該模式打印最小數量。 數字為1-9 ,數字不能重復。

Example:

例:

Input:IIDDIDD Output:12543876

Solution

The pattern & number to be generated

生成的圖案和編號

  • Length of minimum number = string length+1

    最小數目 L的ength =字符串長度+ 1

    Hence, maximum string length possible is 8, since we can construct only with different digits (1-9)

    因此,最大字符串長度為8,因為我們只能用不同的數字(1-9)進行構造

  • 'I' means the next digit will be 1 greater than the current & 'D' means the next digit will be 1 less than the current digit

    “ I”表示下一個數字比當前數字大1, “ D”表示下一個數字比當前數字小1。

    "II" → 123

    “ II”→123

    "DD" → 321

    “ DD”→321

  • The problem can be used with help of stack. The concept is to create stack with consecutive number same as depth of a local contiguous sequence of 'D'.

    該問題可以在堆棧的幫助下使用。 概念是創建具有與本地連續序列“ D”的深度相同的連續數字的堆棧。

    Prerequisite:

    先決條件:

  • Input pattern, string s

    輸入模式,字符串s

  • Stack st to store the digits

    堆疊st以存儲數字

  • Function findMinFromPattern(string s)1. Declare a stack st; 2. FOR i=0 : pattern lengthEnQueue (st, i+1); //push i+1 at first, i+1 becuase i is 0-indexed IF (entire pattern is processed || s[i] =='I')While(stack is not empty){ Pop and printEnd WhileEND IFEND FOREND FUNCTION

    C++ Implementation

    C ++實現

    #include <bits/stdc++.h> using namespace std;void findMinFromPattern(string s){stack<int> st; //stack declared using STLfor(int i=0;i<=s.length();i++){//push i+1 at first, i+1 becuase i is 0-indexed st.push(i+1); //when string length is processed or pattern in Iif(s.length()==i || s[i]=='I' ){ //pop and print until stack is emptywhile(!st.empty()){ cout<<st.top();st.pop();}}}cout<<endl; }int main(){cout<<"enter pattern\n"; string s;cin>>s;if(s.length()>8){cout<<"Not possible,length>8\n";}else{cout<<"The minimum number generated is:\n";findMinFromPattern(s);//function to print}return 0; }

    Output

    輸出量

    First run: enter pattern IIDDIDD The minimum number generated is: 12543876Second run: enter pattern IIIIIIIIDDDDIII Not possible,length>8 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    Example with explanation:

    帶有說明的示例:

    Pattern string: IIDDIDD0 th iteration i=0 EnQueue(st,i+1) Stack status: 1 S[i]=='I' So pop and print until stack becomes empty Thus printing: 1 Output up to now: 1 Stack status; Empty stack -------------------------------------------------------------1st iteration i=1 EnQueue(st,i+1) Stack status: 2 S[i]=='I' So pop and print until stack becomes empty Thus printing: 2 Output up to now: 12 Stack status; Empty stack -------------------------------------------------------------2nd iteration i=2 EnQueue(st,i+1) Stack status: 3 S[i]=='D' Nothing to do Output up to now: 12 Stack status; 3 -------------------------------------------------------------3rd iteration i=3 EnQueue(st,i+1) Stack status: 3 4(top) S[i]=='D' Nothing to do Output up to now: 12 Stack status; 3 4(top) -------------------------------------------------------------4th iteration i=4 EnQueue(st,i+1) Stack status: 3 4 5(top) S[i]=='I' So pop and print until stack becomes empty Thus printing: 5, then 4,then 3 Output up to now: 12543 Stack status; Empty stack -------------------------------------------------------------5th iteration i=5 EnQueue(st,i+1) Stack status: 6 S[i]=='D' Nothing to do Output up to now: 12543 Stack status; 6 -------------------------------------------------------------6th iteration i=6 EnQueue(st,i+1) Stack status: 6 7(top) S[i]=='D' Nothing to do Output up to now: 12543 -------------------------------------------------------------7th iteration i=7 EnQueue(st,i+1) Stack status: 6 7 8(top) Entire string is processed Pop and print until stack becomes empty Print: 8, then 7, then 6 Output up to now: 12543876 Exit: Minimum no is: 12543876

    翻譯自: https://www.includehelp.com/icp/number-following-the-pattern.aspx

    跟隨者數字解碼

    總結

    以上是生活随笔為你收集整理的跟随者数字解码_跟随模式的数字的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。