杭州電子科技大學程式設計競賽(2016’12)- 網路同步賽 1002

來源:互聯網
上載者:User

標籤:tac   code   while   遞增   desc   sof   pac   content   nbsp   

遞增數

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

Problem Description若一個正整數 A,相鄰位總是滿足低位大於等於高位,則稱之為遞增數。
例如:1223,667 等都是遞增數。
現在有個正整數 X,請問有多少個正整數 A 滿足 1<=A<=X,且 A 為遞增數。Input輸入資料第一行是一個正整數 T(1<=T<=20),表示測試資料的組數。
接下來 T 行,每行一個正整數 X(1<=X<=100000000)。Output對於每組資料,輸出一行答案,參見輸出範例。Sample Input3520100Sample Output51854 
/** * @Author:      H S-J * @DateTime:    2016-12-24 13:06:57 * @Description: hdu - 2 */#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <cassert>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;long long dp[10][10] = {0};int dig[111];int count(long long m) {    int num = 1;    while (m / 10) {        ++num;        m /= 10;    }    return num;}void count_1(long long m, int flag){    while (m) {        dig[flag] = (m % 10);        ++flag;        m /= 10;    }}    void init(){    for (int i = 1; i < 10; ++i) {        for (int j = 0; j < 10; ++j) {            if (i == 1) {                dp[i][j] = j;            }            else {                if (j == 0) dp[i][j] = (dp[i - 1][9]);                else dp[i][j] = (dp[i][j - 1] + dp[i - 1][9] - dp[i - 1][j - 1]);            }        }    }}int main(){    init();    int n;    cin >> n;    while (n--) {        long long x;        cin >> x;        int num;        num = count(x);        count_1(x, 1);        long long ans = 0;        for (int i = num ; i > 0 ; --i) {            if (i < num && dig[i] < dig[i + 1]) break;            if (i == 1) {                if (num == 1) ans += dig[1];                else ans += (dig[i] - dig[i + 1] + 1);            }            else {                if (i == num) {                    ans += dp[i][dig[i] - 1];                }                else {                    ans    += (dp[i][dig[i] - 1] - dp[i][dig[i + 1] - 1]);                }            }        }        cout << ans << endl;    }    return 0;}

 

杭州電子科技大學程式設計競賽(2016’12)- 網路同步賽 1002

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.