国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1049. Counting Ones (30) 從1到n整數中1出現的次數

2019-11-11 03:52:57
字體:
來源:轉載
供稿:網友

劍指offer,leetcode,PAT

轉載地址(下面的博文講解的很清楚,容易明白) http://blog.csdn.net/yi_afly/article/details/52012593

這里寫圖片描述

PAT題目聯系 https://www.patest.cn/contests/pat-a-PRactise/1049 ac代碼

#include <cstdio>#include <memory>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> using namespace std;#define INF 0x7fffffffconst int N = 505;int main(){ //freopen("in.txt", "r", stdin); int n; while(scanf("%d", &n) != EOF) { if(n<1) { printf("0/n"); continue; } int base = 1; int count = 0; int round = n; while(round > 0) // 依次遍歷 每一位的數字 { int weight = round%10; // 當前位的值 round/=10; // 去除當前位,前面的數字值 count += round*base; /** * 比如 514 ,十位是1 10個數: 010 011 012 ... 019 10個數: 110 111 112 ... 119 10個數: 210 211 212 ... ... 10個數: 410 411 ... 5個數: 510 511 512 513 514, 最后一輪需要看個位數是多少了 */ if(weight == 1) // 當前位的值就是1,需要看當前位后面的數是多少 { count += (n%base) + 1; } else if(weight > 1) // 當前位值大于1 那么最后一輪還是base個數 { count += base; } base*=10; } printf("%d/n", count); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉新县| 宝应县| 孝昌县| 石景山区| 莎车县| 柞水县| 长春市| 邯郸市| 宝兴县| 镇宁| 铜山县| 宜昌市| 锦屏县| 大理市| 宣威市| 阜平县| 丹棱县| 濮阳县| 繁峙县| 台安县| 望江县| 古浪县| 嘉定区| 白河县| 吴旗县| 施甸县| 边坝县| 平邑县| 鲁甸县| 彰化市| 裕民县| 阿勒泰市| 大方县| 砚山县| 张家界市| 棋牌| 阿拉善盟| 高台县| 周至县| 墨脱县| 翁源县|