<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                編寫一個將整數n(1 <= n <= 9999)轉換成羅馬數字。 - 整數n(1<=n<=9999)與羅馬數字表示有以下對應關系 - 1000 - m,有幾個1000就有幾個m對應 - 900 - 兩個字符cm - 500 - 一個字符d - 400 - 兩個字符cd - 100 - 一個字符c,有幾個100就用幾個c表示 - 90 - 兩個字符xc - 50 - 一個字符l - 40 - 兩個字符xl - 10 - 一個字符x,有一個10就用幾個x - 9 - 用兩個字符ix表示 - 5 - 用一個字符v來表示 - 4 - 用兩個字符iv表示 - 1 - 用一個字符i表示,有幾個1就用幾個i 假如說我們有一個數字22,那么它轉換的方式為22-10=12>=0; 則肯定先有一個x,接著12-10=2>=0;則接著在x后面加上x 變為xx;2-1=1>=0;則肯定后面還需要添加一個i變為xxi,1-1=0>=0;后面還要添加一個i變為xxii。 這樣我們可以這樣做,將對應的羅馬數字和對應的數字放到兩個二維數組中一一對應。 ~~~ char *roman[ROW][COLUMN] = { {"m","m","m","m"}, {"cm","d","cd","c"}, {"xc","l","xl","x"}, {"ix","v","iv","i"} }; int num[ROW][COLUMN] = { {1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1} }; ~~~ 這樣每一個數字進入之后,從數字數組的第一個元素開始,進行減法,如果差大于等于0,接著對這個數字進行減法操作,如果小于0了,則進入下一個數字進行同樣的減法操作,每次差大于等于0的時候,在后面添加上對應的羅馬數字。則轉換算法應該是這樣的。 ~~~ /** * @brief toRoman 將數字轉換成羅馬數字保存到數組rom中 * @param number 要被轉換的數字 * @param rom 將轉換后的羅馬數字保存到rom數組中 */ void toRoman(int number,char rom[]){ int temp = number; int i,j; rom[0]='\0'; for(i = 0;i < ROW;i++){ for(j = 0;j < COLUMN;j++){ while((temp-num[i][j]) >= 0){ strcat(rom,roman[i][j]); temp = temp-num[i][j]; } } } } ~~~ 好了,主體函數已經有了,現在附上我的整體的代碼: ~~~ #include <stdio.h> #include <string.h> #define ROW 4 #define COLUMN 4 void toRoman(int number,char rom[]); /** * @brief main 編寫一個將整數(1 <= x <= 9999)轉化成對應的羅馬數字的程序 * @return */ /** * 整數n(1<=n<=9999)與羅馬數字表示有以下對應關系 * 1000 - m,有幾個1000就有幾個m對應 * 900 - 兩個字符cm * 500 - 一個字符d * 400 - 兩個字符cd * 100 - 一個字符c,有幾個100就用幾個c表示 * 90 - 兩個字符xc * 50 - 一個字符l * 40 - 兩個字符xl * 10 - 一個字符x,有一個10就用幾個x * 9 - 用兩個字符ix表示 * 5 - 用一個字符v來表示 * 4 - 用兩個字符iv表示 * 1 - 用一個字符i表示,有幾個1就用幾個i * */ /** * 用兩個二維數組保存整數和羅馬數字的 * 對應關系 */ char *roman[ROW][COLUMN] = { {"m","m","m","m"}, {"cm","d","cd","c"}, {"xc","l","xl","x"}, {"ix","v","iv","i"} }; int num[ROW][COLUMN] = { {1000,1000,1000,1000}, {900,500,400,100}, {90,50,40,10}, {9,5,4,1} }; int main(int argc,char *argv[]) { int low,high; if(argc < 2){ printf("Please enter the range of the numbers.\n"); }else if(argc == 2){ low = 1; high = atoi(argv[1]); }else if(argc == 3){ low = atoi(argv[1]); high = atoi(argv[2]); }else{ printf("There is more params!!\n"); } int i = low; for(i = low;i <= high;i++){ char rom[25]; toRoman(i,rom); printf("%d => %s\n",i,rom); } return 0; } /** * @brief toRoman 將數字轉換成羅馬數字保存到數組rom中 * @param number 要被轉換的數字 * @param rom 將轉換后的羅馬數字保存到rom數組中 */ void toRoman(int number,char rom[]){ int temp = number; int i,j; rom[0]='\0'; for(i = 0;i < ROW;i++){ for(j = 0;j < COLUMN;j++){ while((temp-num[i][j]) >= 0){ strcat(rom,roman[i][j]); temp = temp-num[i][j]; } } } } ~~~ 下面是我的程序的輸出結果: ![這里寫圖片描述](https://box.kancloud.cn/2016-05-24_5743c075916d7.jpg "")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看