4000000000内满足f(n)=n最大的n(f(n)统计0~n中所有1的个数)
生活随笔
收集整理的這篇文章主要介紹了
4000000000内满足f(n)=n最大的n(f(n)统计0~n中所有1的个数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
#include <windows.h> #include <cstdlib> #include <iostream> #include <cstdio> using namespace std;int gTable[10]; const unsigned int gMAX = 4000000000L;int f(int n) {int ret = 0;int ntemp = n;int ntemp2 = 1;int i = 1;while(ntemp) {ret += ((ntemp-1)/10+1) * i;if((ntemp%10) == 1) {ret -= i;ret += ntemp2;}ntemp = ntemp/10;i*=10;ntemp2 = n%i + 1;}return ret; }int count1(int n) {int count = 0;while(n) {if(n%10 == 1)++count;n /= 10;}return count; }int cal(unsigned int number, int nwei, int count1, unsigned int ncount) {int i, n = 1;unsigned int maxcount;if(nwei == 0) {ncount += count1;if(number == ncount) {printf("f(%d)=%d\n", number, number);}return ncount;}for(i=0; i<nwei; ++i)n *= 10;maxcount = ncount + gTable[nwei-1];maxcount += count1*n;if(ncount > (number + (n-1))) {return maxcount;}if(maxcount < number)return maxcount;n /= 10;for(i=0; i<10; ++i) {if(i==1)ncount = cal(number+i*n, nwei-1, count1+1, ncount);elsencount = cal(number+i*n, nwei-1, count1, ncount);}return ncount; }int main() {int i;unsigned int n=1;unsigned int ncount = 0;int nwei = 0;int ncount1;//init gTablefor(i=0; i<10; ++i) {n *= 10;gTable[i] = f(n-1);cout << "gTable[" << i << "] : f(" << n-1 << ") : " << gTable[i] << endl;}n=0;nwei = 0;ncount1 = 0;while(n<gMAX) {unsigned int temp;temp = 1;ncount = cal(n, nwei, ncount1, ncount);for(i=0; i<nwei; ++i) {temp *= 10;}n += temp;if(n/temp/10 == 1)++nwei;ncount1 = count1(n);}printf("ncount1:%d, ncount:%d, nwei:%d\n", ncount1, ncount, nwei);return 0; }
轉載于:https://my.oschina.net/ITHaozi/blog/287251
總結
以上是生活随笔為你收集整理的4000000000内满足f(n)=n最大的n(f(n)统计0~n中所有1的个数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于KNN算法的少量数据分类实验
- 下一篇: 网页设计原理