1010.进位制 **
题意是给定一个数的进制枚举另一个数可能与他相等的进制 注意这个进制有下界没有上界 即如果最大数字是9 那么他至少是十进制,最大是zzz...zzz(10个z)
能表示的最大数的进制 应该是36 ^ 10-1~=3 ^15
longlong 可以存下20位左右
这样顺序枚举进制一定会超时,所以我们使用二分的方法
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
int get ( char c) {
if ( c<= '9' ) return c- '0' ;
else return c- 'a' + 10 ;
}
LL calc ( string s, LL radix) {
LL res= 0 ;
for ( int i= 0 ; i< s. size ( ) ; i++ ) {
if ( ( ( double ) res* radix+ get ( s[ i] ) ) > 1e16 ) return 1e18 ;
res= res* radix+ get ( s[ i] ) ;
}
return res;
}
int main ( ) {
string s1, s2;
int tag, rad;
cin>> s1>> s2>> tag>> rad;
if ( tag== 2 ) swap ( s1, s2) ;
LL target= calc ( s1, rad) ;
LL l= 0 , r= max ( target, 36LL ) ;
for ( auto it: s2) l= max ( l, get ( it) + 1LL ) ;
while ( l< r) {
LL mid= l+ r>> 1 ;
if ( calc ( s2, mid) >= target) r= mid;
else l= mid+ 1 ;
}
if ( calc ( s2, r) != target) cout<< "Impossible" ;
else cout<< r;
}
1015.可逆质数
需要特判一下1不是质数 注意可以用到cin>>n,n>0
逗号表达式简化代码 10进制转其他进制可能会存不下,用longlong 存数,转最多的二进制大概在15位左右
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
vector< int > ten_radix ( int n, int radix) {
vector< int > res;
while ( n) {
res. push_back ( n% radix) ;
n/ = radix;
}
if ( n) res. push_back ( n) ;
return res;
}
int radix_ten ( vector< int > n, int rad) {
int res= 0 ;
for ( int i= 0 ; i< n. size ( ) ; i++ ) res= res* rad+ n[ i] ;
return res;
}
bool check ( int n) {
if ( n== 1 ) return false ;
for ( int i= 2 ; i<= n/ i; i++ ) {
if ( n% i== 0 ) return false ;
}
return true ;
}
int main ( ) {
int n1, rad;
while ( cin>> n1>> rad, n1>= 1 ) {
vector< int > n2= ten_radix ( n1, rad) ;
int target= radix_ten ( n2, rad) ;
if ( check ( n1) && check ( target) ) cout<< "Yes" << endl;
else cout<< "No" << endl;
}
}
1027.火星颜色
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
char get ( int x) {
if ( x<= 9 ) return x+ '0' ;
else return x- 10 + 'A' ;
}
string change ( int n) {
vector< char > c;
while ( n) {
c. push_back ( get ( n% 13 ) ) ;
n= n/ 13 ;
}
if ( n) c. push_back ( get ( n) ) ;
while ( c. size ( ) < 2 ) c. push_back ( 0 + '0' ) ;
string res;
for ( int i= c. size ( ) - 1 ; i>= 0 ; i-- ) res+ = c[ i] ;
return res;
}
int main ( ) {
int n1, n2, n3;
cin>> n1>> n2>> n3;
cout<< "#" ;
cout<< change ( n1) << change ( n2) << change ( n3) ;
}
#include <iostream>
using namespace std;
char get ( int x)
{
if ( x <= 9 ) return '0' + x;
return 'A' + x - 10 ;
}
int main ( )
{
int a[ 3 ] ;
for ( int i = 0 ; i < 3 ; i ++ ) scanf ( "%d" , & a[ i] ) ;
cout << '#' ;
for ( int i = 0 ; i < 3 ; i ++ ) cout << get ( a[ i] / 13 ) << get ( a[ i] % 13 ) ;
return 0 ;
}
作者:yxc
链接:https: / / www. acwing. com/ activity/ content/ code/ content/ 269939 /
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1100.火星数字(sstream)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char num[ ] [ 5 ] = { "tret" , "jan" , "feb" , "mar" , "apr" , "may" , "jun" , "jly" , "aug" , "sep" , "oct" , "nov" , "dec" ,
"tam" , "hel" , "maa" , "huh" , "tou" , "kes" , "hei" , "elo" , "syy" , "lok" , "mer" , "jou" } ;
int get ( string s) {
for ( int i= 0 ; i< 26 ; i++ ) {
if ( s== num[ i] ) {
if ( i< 13 ) return i;
else return ( i- 12 ) * 13 ;
}
}
return 0 ;
}
int main ( ) {
int n;
cin>> n;
getchar ( ) ;
while ( n-- ) {
string s;
getline ( cin, s) ;
if ( s[ 0 ] <= '9' ) {
int x= stoi ( s) ;
int high= x/ 13 , low= x% 13 ;
if ( high) {
if ( low) cout<< num[ x/ 13 + 12 ] << " " << num[ x% 13 ] << endl;
else cout<< num[ x/ 13 + 12 ] << endl;
}
else cout<< num[ x% 13 ] << endl;
}
else {
int k= s. find ( ' ' ) ;
int res= get ( s. substr ( 0 , k) ) ;
if ( k!= - 1 ) res+ = get ( s. substr ( k+ 1 ) ) ;
cout<< res<< endl;
}
}
}
#include <iostream>
#include <sstream>
using namespace std;
char names[ ] [ 5 ] = {
"tret" , "jan" , "feb" , "mar" , "apr" , "may" , "jun" , "jly" , "aug" , "sep" , "oct" , "nov" , "dec" ,
"tam" , "hel" , "maa" , "huh" , "tou" , "kes" , "hei" , "elo" , "syy" , "lok" , "mer" , "jou" ,
} ;
int get ( string word)
{
for ( int i = 0 ; i < 25 ; i ++ )
if ( names[ i] == word)
{
if ( i < 13 ) return i;
return ( i - 12 ) * 13 ;
}
return - 1 ;
}
int main ( )
{
int n;
cin >> n;
getchar ( ) ;
while ( n -- )
{
string line;
getline ( cin, line) ;
stringstream ssin ( line) ;
if ( line[ 0 ] <= '9' )
{
int v;
ssin >> v;
if ( v < 13 ) cout << names[ v] << endl;
else
{
cout << names[ 12 + v / 13 ] ;
if ( v % 13 == 0 ) cout << endl;
else cout << ' ' << names[ v % 13 ] << endl;
}
}
else
{
int res = 0 ;
string word;
while ( ssin >> word) res + = get ( word) ;
cout << res << endl;
}
}
return 0 ;
}
作者:yxc
链接:https: / / www. acwing. com/ activity/ content/ code/ content/ 269965 /
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1019.普通回文数
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
vector< int > change_b ( int n, int b) {
if ( ! n) return { 0 } ;
vector< int > res;
while ( n) {
res. push_back ( n% b) ;
n= n/ b;
}
return res;
}
bool check ( vector< int > c) {
for ( int i= 0 , j= c. size ( ) - 1 ; i< j; i++ , j-- )
if ( c[ i] != c[ j] ) return false ;
return true ;
}
int main ( ) {
int n, b;
cin>> n>> b;
vector< int > res= change_b ( n, b) ;
if ( check ( res) ) cout<< "Yes" << endl;
else cout<< "No" << endl;
cout<< res[ res. size ( ) - 1 ] ;
for ( int i= res. size ( ) - 2 ; i>= 0 ; i-- ) cout<< " " << res[ i] ;
}