hdu2964-Prime Bases
生活随笔
收集整理的這篇文章主要介紹了
hdu2964-Prime Bases
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=2964
題意,給你一個整數(shù)n,現(xiàn)在要你分解成 n = k1 * ( 2 * 3 * ....*x1 ) + k2 * ( 2 * 3 * .... *x2 ) + ........;其中后面均為素數(shù),且是由最小的2遞增相乘;
分析:首先打印素數(shù)表;然后使用數(shù)組a【】來儲存到從第一個素數(shù)2到第幾個素數(shù)乘積。找到第i個小于n,第i +1個大于n的數(shù)值 i ;
然后分解n.使用數(shù)組b【】來儲存a【i】的個數(shù);同理執(zhí)行多次,知道將n分解完畢;
如下的這種輸出格式比較好,大家可以參考下;
參照做的;
?
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> #include<iomanip>using namespace std; const int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,39,41,43,47,51,53,57,59}; __int64 a[21]; int b[21],rem; void init( ) {a[ 0 ] = 1 ;for( int i =1 ; i < 21 ; ++i )a[ i ] = a[ i - 1 ] * prime[ i - 1 ] ; }void deal( int n ) {int i ;for( int i = 0 ; i < 21 ; ++i ){if( a[ i ] <= n && a[ i + 1 ] > n ){rem = i ;break ;}}memset( b , 0 ,sizeof( b ) ) ;for( int i = rem ; i >=0 ; --i ){b[ i ] = n / a[ i ] ;n %= a[ i ] ;} }void output( int n ) {cout << n << " = " ;for( int i = 0 ; i <= rem ; ++i ){if( b[ i ] ){cout << b[ i ] ;for( int j = 0 ; j < i ; ++j )cout << "*" << prime[ j ] ;if( i != rem )cout << " + " ;}}cout << endl ; }int main( ) {init( ) ;int n ;while( cin >> n , n ){deal( n ) ;output( n );}return 0 ; }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/dyllove98/p/3206309.html
總結(jié)
以上是生活随笔為你收集整理的hdu2964-Prime Bases的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css毛玻璃效果白边_使用css模拟vi
- 下一篇: 十五、java的基本数据类型