PTA天梯赛练习集选做
002
模拟+找规律
规律就是上面正三角第i
行是i*2-1
个符号
正三角的符号数为t
,整个沙漏的符号数为t*2-1
#include <bits/stdc++.h>
using namespace std;
int n , m , t = 0;
char ch;
int main()
{
cin >> n >> ch;
for( register int i = 1 ; ; i ++ )
{
m = i * 2 - 1;
t += m;
if( ( t * 2 - 1) <= n ) continue;
t -= m;
m = i - 1;
break;
}
for( register int i = m ; i >= 1 ; i -- )
{
for( register int j = 1 ; j <= m - i ; j ++ ) putchar(' ');
for( register int j = 1 ; j <= i * 2 - 1 ; j ++ ) putchar(ch);
puts("");
}
for( register int i = 2 ; i <= m ; i ++ )
{
for( register int j = 1 ; j <= m - i ; j ++ ) putchar(' ');
for( register int j = 1 ; j <= i * 2 - 1 ; j ++ ) putchar(ch);
puts("");
}
cout << n - ( t * 2 - 1) << endl;
return 0;
}
003
很简单,数字太长用sting
存,用auto
便历
#include <bits/stdc++.h>
using namespace std;
int t[15];
int main()
{
string s;
cin >> s;
for( auto it : s )
{
t[ it - '0' ] ++;
}
for( int i = 0 ; i <= 9 ; i ++ )
{
if( t[i] ) printf( "%d:%d\n" , i , t[i] );
}
return 0;
}
004
printf("Celsius = %d\n" , 5 * ( a -32 ) / 9 );
005
这道题告诉我们要活用STL,map套pair可以很简单
#include <bits/stdc++.h>
using namespace std;
map < int , pair< string , int > > t;
int n , m;
string s;
int main()
{
cin >> n;
for( int i = 1 , a , b; i <= n ; i ++ )
{
cin >> s >> a >> b ;
t[a] = make_pair( s , b );
}
cin >> m;
for( int i = 1 , a ; i <= m ; i ++ )
{
cin >> a;
cout << t[a].first << ' ' << t[a].second << endl;
}
return 0;
}
006
一道挺有意思的枚举题,分别枚举左右短点就行
在配合一点优化就行
#include <bits/stdc++.h>
using namespace std;
long long n , t , maxlong = 0 , l, r , mul = 1 , temp;
int main()
{
cin >> n;
t = sqrt( n ) + 1, l = n , r = n;
for( register int i = 2 ;i <= t ; i ++ )//枚举左端点
{
if( n % i ) continue;
temp = 1;
for( register int j = i ; j < i + maxlong ; j ++ ) temp *= j;
for( register int j = i + maxlong ; j <= t ; j ++ )//枚举右端点
{
temp *= j;
if( temp > n ) break;
if( n % temp ) break;
l = i , r = j , maxlong = j - i + 1;
}
}
if( maxlong == 0 ) maxlong = 1;
cout << maxlong << endl;
for( register int i = l ; i < r ; i ++ ) cout << i << '*';
cout << r << endl;
return 0;
}