表示9楼,10楼的分析很精辟
我来贴个代码
程序代码:
我来贴个代码
程序代码:/*****************************
Input: (k,N)
| Ctrl+Z to end
Output: ans
| "Error!"
| "Valid!"
| "Exit."
*****************************/
#include <iostream>
#include <bitset>
using namespace std;
typedef unsigned long ulong;
typedef unsigned int uint;
ulong An(uint k = 3, uint N = 100);
int main()
{
uint k = 0, N = 0;
ulong ans = 0;
while(1){
cout << "IN: " << flush;
cin.sync();
cin >> k >> N;
if(!cin){
if(cin.eof()){
cout << "OUT: Exit." << endl;
return 0;
}
else{
cin.clear();
cout << "OUT: Error!" << endl;
continue;
}
}
if(ans = An(k, N)){
cout << "OUT: " << ans << endl;
}
else{
cout << "OUT: Valid!" << endl;
}
}
return 0;
}
ulong An(uint k, uint N)
{
if(!((k >= 3 && k <= 15) && (N >= 10 && N <= 1000)))
return 0;
ulong sum(0), temp(1);
bitset<10> coef(N);
for(int i = 0; i < 10; i++){
if(coef[i])
sum += temp;
temp *= k;
}
return sum;
}

人生是一场错过 愿你别蹉跎

