解題紀錄 codeforces Codeforces Round 976 (Div. 2) A Find Minimum Operations
# include <iostream>
# include <vector>
using namespace std;
int main ( ) {
int t;
cin >> t;
vector< int > results;
for ( int i = 0 ; i < t; i++ ) {
long long n, k;
cin >> n >> k;
int cnt = 0 ;
if ( k == 1 ) {
cnt = n;
} else {
while ( n > 0 ) {
cnt += n % k;
n = n/ k;
}
}
results. push_back ( cnt) ;
}
for ( const auto & res : results) {
cout << res << endl;
}
return 0 ;
}
Codeforces Round 981 (Div. 3) A. Sakurako and Kosuke
# include <iostream>
using namespace std;
int main ( ) {
int t;
cin >> t;
while ( t-- ) {
int n;
cin >> n;
int pos = 0 ;
int step = 1 ;
bool sakurakoTurn = true ;
while ( true ) {
if ( sakurakoTurn) {
pos -= step;
} else {
pos += step;
}
if ( pos < - n || pos > n) {
if ( sakurakoTurn) {
cout << "Sakurako" << endl;
} else {
cout << "Kosuke" << endl;
}
break ;
}
sakurakoTurn = ! sakurakoTurn;
step += 2 ;
}
}
return 0 ;
}
Codeforces Global Round 27 A. Sliding
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
long long int n, m, r, c;
cin>> n>> m>> r>> c;
long long int total= 0 ;
total+= ( n- r) * ( m) ;
total+= 1 * ( m- c) ;
total+= 1 * ( m- 1 ) * ( n- r) ;
cout<< total<< endl;
}
}
B. Everyone Loves Tres
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
ios_base:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
int t;
cin >> t;
while ( t-- ) {
int n;
cin >> n;
if ( n== 1 ) {
cout<< "-1\n" ;
}
if ( n== 2 ) {
cout<< "66\n" ;
}
if ( n== 3 ) {
cout<< "-1\n" ;
}
if ( n== 4 ) {
cout<< "3366\n" ;
}
if ( n== 5 ) {
cout<< "36366\n" ;
}
if ( n> 5 ) {
if ( n% 2 == 0 ) {
for ( int j= 0 ; j< n- 4 ; j++ ) {
cout<< "3" ;
}
cout<< "3366\n" ;
}
else {
for ( int j= 0 ; j< n- 5 ; j++ ) {
cout<< "3" ;
}
cout<< "36366\n" ;
}
}
}
return 0 ;
}
APCS模擬測驗 apcs0101電車難題
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n, p, k, m, d;
cin>> n>> p>> k>> m>> d;
int l= p- m- k;
if ( n- l- d+ 1 <= 0 ) {
cout<< "Sad :((\n" << - 1 * ( n- l- d+ 1 ) ;
}
else
cout<< "Happy :>\n" << n- l- d;
}
apcs0201氧化還原滴定 (Redox titration)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int c1, c2, v1, v2;
cin>> c1>> c2>> v1>> v2;
if ( 2 * c1* v1== 5 * c2* v2) {
cout<< "Yes\n" ;
cout<< 2 * c1* v1;
}
else
cout<< "No" ;
}
apcs0202Charles 的頂級球隊 (Soccer-Team)
# include "bits/stdc++.h"
using namespace std;
struct Change {
int minute;
int second;
int down;
int up;
} ;
int main ( ) {
int n, q;
cin>> n>> q;
int player[ 11 ] ;
for ( int i= 0 ; i< 11 ; i++ ) {
cin>> player[ i] ;
}
int cplayer[ 11 ] ;
Change change[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> change[ i] . minute>> change[ i] . second>> change[ i] . down>> change[ i] . up;
}
int fm[ q] , fs[ q] ;
for ( int i= 0 ; i< q; i++ ) {
cin>> fm[ i] >> fs[ i] ;
}
for ( int k= 0 ; k< q; k++ ) {
int m= fm[ k] ;
int s= fs[ k] ;
for ( int l= 0 ; l< 11 ; l++ ) {
cplayer[ l] = player[ l] ;
}
for ( int i= 0 ; i< n; i++ ) {
if ( m> change[ i] . minute or ( m== change[ i] . minute and s>= change[ i] . second) ) {
for ( int temp = 0 ; temp < 11 ; temp++ ) {
if ( cplayer[ temp] == change[ i] . down) {
cplayer[ temp] = change[ i] . up;
break ;
} }
}
}
for ( int l= 0 ; l< 11 ; l++ ) {
cout<< cplayer[ l] ;
if ( l < 10 ) {
cout << " " ;
}
}
if ( k< q- 1 ) cout<< "\n" ;
}
}
apcs0301轉譯 (Translation)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
char str[ 1001 ] ;
cin>> str;
int now= 0 ;
while ( str[ now] != 'A' or str[ now+ 1 ] != 'U' or str[ now+ 2 ] != 'G' ) {
now++ ;
}
int total= 0 ;
while ( 1 ) {
total++ ;
if ( str[ now] == 'U' && str[ now+ 1 ] == 'A' && str[ now+ 2 ] == str[ now+ 1 ] )
break ;
if ( str[ now] == 'U' && str[ now+ 1 ] == 'A' && str[ now+ 2 ] == 'G' )
break ;
if ( str[ now] == 'U' && str[ now+ 1 ] == 'G' && str[ now+ 2 ] == 'A' )
break ;
now+= 3 ;
}
cout<< total- 1 ;
}
apcs0401辯論賽 (Debate)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int t;
cin>> t;
int totalp= 0 , totaln= 0 , vp= 0 , vn= 0 ;
for ( int i= 0 ; i< t; i++ ) {
int a, b;
cin>> a>> b;
totalp+= a;
totaln+= b;
if ( a> b)
vp++ ;
else
vn++ ;
}
if ( vp> vn) {
cout<< "Positive side\n" ;
}
else {
cout<< "Negative side\n" ;
}
cout<< vp<< " " << vn<< "\n" << totalp<< " " << totaln;
}
apcs0402黑洞旅行 (Blackhole)
# include <bits/stdc++.h>
using namespace std;
struct m {
char S;
int t;
int n;
} ;
int main ( ) {
char mapp[ 8 ] [ 8 ]
{
{ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' } ,
{ 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' } ,
{ 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' } ,
{ 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' } ,
{ 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' } ,
{ 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' } ,
{ 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' } ,
{ '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' } } ;
int q;
cin>> q;
m f[ q] ;
for ( int i= 0 ; i< q; i++ ) {
cin>> f[ i] . S>> f[ i] . t>> f[ i] . n;
}
for ( int i= 0 ; i< q; i++ ) {
int x= 0 , y= 0 ;
for ( int j= 0 ; j< 8 ; j++ ) {
for ( int k= 0 ; k< 8 ; k++ ) {
if ( mapp[ k] [ j] == f[ i] . S) {
x= j; y= k;
}
}
}
int face= f[ i] . t;
for ( int j= 1 ; j<= f[ i] . n; j++ ) {
if ( face== 0 ) {
y-= j;
}
if ( face== 1 ) {
y-= j; x+= j;
}
if ( face== 2 ) {
x+= j;
}
if ( face== 3 ) {
y+= j; x+= j;
}
if ( face== 4 ) {
y+= j;
}
if ( face== 5 ) {
y+= j; x-= j;
}
if ( face== 6 ) {
x-= j;
}
if ( face== 7 ) {
y-= j; x-= j;
}
face= ( face+ 1 ) % 8 ;
if ( x> 7 ) x= x% 8 ;
if ( y> 7 ) y= y% 8 ;
if ( x< 0 ) x+= 8 ;
if ( y< 0 ) y+= 8 ;
}
cout<< mapp[ y] [ x] ;
if ( i< q- 1 ) cout<< endl;
}
}
apcs0403愛吃字母的怪獸 (Alphabet Monster)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
long long int W[ 2 ] , M[ 2 ] , L[ 2 ] , n, m;
cin>> n>> m;
string ab;
long long int mi[ n] = { 0 } ;
cin>> ab;
cin>> W[ 0 ] >> M[ 0 ] >> L[ 0 ] >> W[ 1 ] >> M[ 1 ] >> L[ 1 ] ;
long long int total= 0 ;
long long int now= 0 ;
for ( int i= 0 ; i< n; i++ ) {
if ( ab[ i] == 'L' ) {
now+= L[ 0 ] ;
if ( i+ L[ 1 ] - 1 < n)
mi[ i+ L[ 1 ] - 1 ] += L[ 0 ] ;
}
if ( ab[ i] == 'M' ) {
now+= M[ 0 ] ;
if ( i+ M[ 1 ] - 1 < n)
mi[ i+ M[ 1 ] - 1 ] += M[ 0 ] ;
}
if ( ab[ i] == 'W' ) {
now+= W[ 0 ] ;
if ( i+ W[ 1 ] - 1 < n)
mi[ i+ W[ 1 ] - 1 ] += W[ 0 ] ;
}
if ( now> m)
total++ ;
now-= mi[ i] ;
}
cout<< total;
}
apcs0501磁鐵 Magnet
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string num;
cin>> num;
int n[ 10 ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 4 , 7 , 6 } ;
long long int total= 0 ;
for ( int i= 0 ; i< num. size ( ) ; i++ )
total+= n[ num[ i] - '0' ] ;
cout<< total;
}
apcs0601大智慧學苑-畢業旅行囉 (Trip)
# include "bits/stdc++.h"
using namespace std;
struct mi {
int L;
int R;
int P;
} ;
int main ( ) {
int n, m;
cin>> n>> m;
int person[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> person[ i] ;
}
int k;
cin>> k;
mi dis[ k] ;
for ( int i= 0 ; i< k; i++ ) {
cin>> dis[ i] . L>> dis[ i] . R>> dis[ i] . P;
}
int total= 0 ;
for ( int i= 0 ; i< n; i++ ) {
total+= m;
for ( int j= 0 ; j< k; j++ ) {
if ( dis[ j] . L<= person[ i] and dis[ j] . R>= person[ i] ) {
total-= m- dis[ j] . P;
break ;
}
}
}
cout<< total;
}
apcs0701AI 運算 (AI Operation)
# include "bits/stdc++.h"
using namespace std;
struct IO {
int in;
int out;
} ;
int main ( ) {
IO io[ 26 ] ;
int k;
cin>> k;
for ( int i= 0 ; i< k; i++ ) {
char t;
cin>> t;
cin>> io[ t- 'A' ] . in>> io[ t- 'A' ] . out;
}
cin>> k;
string ts;
cin>> ts;
for ( int i= 0 ; i< k- 1 ; i++ ) {
char now1= ts[ i] ;
char now2= ts[ i+ 1 ] ;
if ( io[ now1- 'A' ] . out== io[ now2- 'A' ] . in) {
cout<< "1\n" ;
}
else {
cout<< "0\n" ;
}
}
}
apcs0801遊戲 (Game)
# include "bits/stdc++.h"
using namespace std;
struct Person {
int T;
int S;
int R;
int number;
} ;
bool c ( const Person & a, const Person & b) {
if ( a. T != b. T) return a. T> b. T;
if ( a. R != b. R) return a. R> b. R;
if ( a. S != b. S) return a. S> b. S;
return a. number> b. number;
}
int main ( ) {
int n;
cin>> n;
Person person[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> person[ i] . T>> person[ i] . S>> person[ i] . R;
person[ i] . number= i+ 1 ;
}
sort ( person, person+ n, c) ;
for ( int i= 0 ; i< n; i++ ) {
cout<< person[ i] . number<< " " << person[ i] . T<< " " << person[ i] . S<< " " << person[ i] . R<< "\n" ;
}
}
apcs0802天旋地轉 (Spinning)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
int turn[ n* n- 1 ] ;
int m[ n] [ n] ;
for ( int i= 0 ; i< n* n- 1 ; i++ ) {
cin>> turn[ i] ;
}
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
cin>> m[ i] [ j] ;
}
}
int total= m[ 0 ] [ 0 ] ;
int x= 0 , y= 0 ;
for ( int i= 0 ; i< n* n- 1 ; i++ ) {
if ( turn[ i] == 0 ) {
int mt[ n] [ n] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
mt[ i] [ j] = m[ j] [ n- 1 - i] ;
}
}
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
m[ i] [ j] = mt[ i] [ j] ;
}
}
}
if ( turn[ i] == 1 ) {
int mt[ n] [ n] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
mt[ i] [ j] = m[ n- 1 - j] [ i] ;
}
}
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
m[ i] [ j] = mt[ i] [ j] ;
}
}
}
x++ ;
if ( x> n- 1 ) {
x= 0 ;
y++ ;
}
total+= m[ y] [ x] ;
}
cout<< total;
}
apcs0804滑雪道 II (Ski Runs II)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
int ski[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> ski[ i] ;
long long int total= 0 ;
for ( int i= 0 ; i< n- 1 ; i++ ) {
for ( int j= i+ 1 ; j< n; j++ ) {
if ( ski[ i] > ski[ j] )
total++ ;
else
break ;
}
}
for ( int i= n- 1 ; i> 0 ; i-- ) {
for ( int j= i- 1 ; j> - 1 ; j-- ) {
if ( ski[ i] > ski[ j] )
total++ ;
else
break ;
}
}
cout<< total;
}
apcs0901Chung 的 GPA (Chung's GPA)
# include "bits/stdc++.h"
using namespace std;
struct gra {
string rate;
} ;
int main ( ) {
int n;
cin>> n;
float total= 0 ;
gra ptr[ 101 ] ;
for ( int i= 0 ; i< n; i++ ) {
ptr[ i] . rate= 'X' ;
}
for ( int i= 0 ; i< n; i++ ) {
int j;
cin>> j;
cin>> ptr[ j] . rate;
}
for ( int i= 1 ; i< 101 ; i++ ) {
if ( ptr[ i] . rate[ 0 ] == 'A' and ptr[ i] . rate[ 1 ] == '+' ) {
total+= 4.3 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'A' and ptr[ i] . rate[ 1 ] == '-' ) {
total+= 3.7 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'A' ) {
total+= 4.0 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'B' and ptr[ i] . rate[ 1 ] == '+' ) {
total+= 3.3 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'B' and ptr[ i] . rate[ 1 ] == '-' ) {
total+= 2.7 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'B' ) {
total+= 3.0 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'C' and ptr[ i] . rate[ 1 ] == '+' ) {
total+= 2.3 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'C' and ptr[ i] . rate[ 1 ] == '-' ) {
total+= 1.7 ;
}
else if ( ptr[ i] . rate[ 0 ] == 'C' ) {
total+= 2.0 ;
}
if ( ptr[ i] . rate[ 0 ] == 'D' ) {
total+= 1.0 ;
}
}
cout<< fixed<< setprecision ( 1 ) << total;
}
apcs0902飛鏢 (Dart)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n, m;
cin>> n>> m;
int score[ 101 ] [ 101 ] ;
int times[ 101 ] [ 101 ] ;
int hit[ 101 ] [ 101 ] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
cin>> score[ j] [ i] ;
times[ j] [ i] = 1 ;
hit[ j] [ i] = 0 ;
}
}
int k, q;
cin>> k>> q;
int prex= 100000 , prey= 100000 ;
int dis= 0 ;
while ( k> 0 or q> 0 ) {
int in= 0 ;
cin>> in;
if ( in== 1 ) {
k-- ;
int x, y;
cin>> y>> x;
x-- ;
y-- ;
if ( x>= 0 and y>= 0 and x< m and y< n) {
hit[ x] [ y] ++ ;
if ( x== prex and y== prey) {
times[ x] [ y] ++ ;
}
else {
times[ x] [ y] = 1 ;
}
}
else {
int mint= 1000000 ;
for ( int i= 0 ; i< m; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
times[ i] [ j] = 1 ;
mint= min ( mint, ( abs ( x- i) + abs ( y- j) ) ) ;
}
}
dis+= mint;
}
prex= x;
prey= y;
}
if ( in== 2 ) {
q-- ;
int total= 0 ;
for ( int i= 0 ; i< m; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
if ( hit[ i] [ j] != 0 ) {
int timess= times[ i] [ j] ;
total+= hit[ i] [ j] * ( score[ i] [ j] * ( timess* ( timess+ 1 ) ) ) / 2 ;
}
}
}
cout<< total- dis<< endl;
}
}
}
Zerojudge 基礎題庫 a001. 哈囉
# include <iostream>
using namespace std;
int main ( ) {
string in;
cin>> in;
cout<< "hello, " << in;
return 0 ;
}
a002. 簡易加法
# include <iostream>
using namespace std;
int main ( ) {
int a, b;
cin>> a>> b;
cout<< a+ b;
return 0 ;
}
a003. 兩光法師占卜術
# include <string>
int main ( ) {
int M, D;
std:: cin>> M>> D;
std:: string ptr[ 3 ] = { "普通" , "吉" , "大吉" } ;
std:: cout<< ptr[ ( M* 2 + D) % 3 ] ;
return 0 ;
}
a004. 文文的求婚
# include <iostream>
using namespace std;
int main ( ) {
int in;
while ( cin>> in) {
if ( in% 4 == 0 and in% 100 != 0 )
cout<< "閏年\n" ;
else if ( in% 400 == 0 )
cout<< "閏年\n" ;
else
cout<< "平年\n" ;
}
return 0 ;
}
a005. Eva 的回家作業
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int t;
cin>> t;
int ptr[ t] [ 4 ] ;
for ( int i= 0 ; i< t; i++ )
for ( int j= 0 ; j< 4 ; j++ )
cin>> ptr[ i] [ j] ;
for ( int i= 0 ; i< t; i++ ) {
int r= 0 ;
if ( ptr[ i] [ 3 ] - ptr[ i] [ 2 ] == ptr[ i] [ 2 ] - ptr[ i] [ 1 ] and ptr[ i] [ 2 ] - ptr[ i] [ 1 ] == ptr[ i] [ 1 ] - ptr[ i] [ 0 ] ) {
r= ptr[ i] [ 3 ] - ptr[ i] [ 2 ] ;
for ( int j= 0 ; j< 4 ; j++ ) {
cout<< ptr[ i] [ j] << " " ;
}
cout<< ptr[ i] [ 3 ] + ptr[ i] [ 3 ] - ptr[ i] [ 2 ] << endl;
}
else {
for ( int j= 0 ; j< 4 ; j++ ) {
cout<< ptr[ i] [ j] << " " ;
}
cout<< ptr[ i] [ 3 ] * ptr[ i] [ 3 ] / ptr[ i] [ 2 ] << endl;
}
}
return 0 ;
}
a006. 一元二次方程式
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b, c, x, x1, x2;
cin>> a>> b>> c;
x= ( b* b- 4 * a* c) ;
x1= ( - b+ sqrt ( x) ) / ( 2 * a) ;
x2= ( - b- sqrt ( x) ) / ( 2 * a) ;
if ( x1< x2)
swap ( x1, x2) ;
if ( x> 0 ) { cout<< "Two different roots x1=" << x1<< " , x2=" << x2; }
if ( x== 0 ) { cout<< "Two same roots x=" << x1; }
if ( x< 0 ) { cout<< "No real root" ; }
return 0 ;
}
a009. 解碼器
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
string str;
getline ( cin, str) ;
for ( int i= 0 ; i< str. length ( ) ; i++ ) {
str[ i] -= 7 ;
cout<< str[ i] ;
}
}
a010. 因數分解
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int num;
cin>> num;
while ( num!= 1 ) {
for ( int i= 2 ; i<= num; i++ ) {
if ( num% i== 0 ) {
num/= i;
int down= 1 ;
while ( num% i== 0 ) {
down++ ;
num/= i;
}
if ( down== 1 and num== 1 ) { cout<< i; }
if ( down!= 1 and num== 1 ) { cout<< i<< "^" << down; }
if ( down== 1 and num!= 1 ) { cout<< i<< " * " ; }
if ( down!= 1 and num!= 1 ) { cout<< i<< "^" << down<< " * " ; }
}
}
}
}
a015. 矩陣的翻轉
# include <iostream>
using namespace std;
int main ( ) {
int a[ 100 ] [ 100 ] = { } ;
int r, c;
while ( cin>> r>> c) {
for ( int i= 0 ; i< r; ++ i) {
for ( int j= 0 ; j< c; ++ j) {
cin>> a[ i] [ j] ;
}
}
for ( int i= 0 ; i< c; ++ i) {
for ( int j= 0 ; j< r; ++ j) {
cout << a[ j] [ i] ;
if ( j != r - 1 ) {
cout << " " ;
}
}
cout << endl;
}
}
return 0 ;
}
a017五則運算
[print (eval (line.replace('/' , '//' ))) for line in __import__ ("sys" ).stdin ]
a020. 身分證檢驗
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string input;
cin>> input;
int change[ 26 ] = { 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 34 , 18 , 19 , 20 , 21 , 22 , 35 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 32 , 30 , 31 , 33 } ;
int en= change[ input[ 0 ] - 'A' ] ;
int sum= 0 ;
sum+= en/ 10 + ( en% 10 ) * 9 ;
for ( int i= 1 ; i< 9 ; i++ )
sum+= ( input[ i] - '0' ) * ( 9 - i) ;
sum+= input[ 9 ] - '0' ;
if ( sum% 10 == 0 )
cout<< "real" ;
else
cout<< "fake" ;
}
a022. 迴文
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string str, restr;
cin>> str;
restr= str;
reverse ( restr. begin ( ) , restr. end ( ) ) ;
if ( str== restr)
cout<< "yes" ;
else
cout<< "no" ;
return 0 ;
}
a024. 最大公因數(GCD)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b;
cin >> a >> b;
while ( 1 ) {
a= a% b;
if ( a== 0 ) {
cout << b;
break ;
}
b= b% a;
if ( b== 0 ) {
cout << a;
break ;
}
}
return 0 ;
}
a034. 二進位制轉換
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int num;
while ( cin>> num) {
int k= 0 ;
while ( pow ( 2 , k) <= num) {
k++ ;
}
k-- ;
int how[ k+ 1 ] ;
for ( int i= 0 ; i<= k; i++ ) {
how[ i] = 0 ;
if ( pow ( 2 , k- i) <= num) {
how[ i] = 1 ;
num-= pow ( 2 , k- i) ;
}
}
for ( int i= 0 ; i< k+ 1 ; i++ ) {
cout<< how[ i] ;
}
cout<< endl;
}
}
a038. 數字翻轉
# include <iostream>
using namespace std;
int main ( ) {
int n, reversed_number = 0 , remainder;
cin >> n;
while ( n != 0 ) {
remainder = n % 10 ;
reversed_number = reversed_number * 10 + remainder;
n /= 10 ;
}
cout << reversed_number;
return 0 ;
}
a040. 阿姆斯壯數
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n, m;
cin >> n >> m;
int total = 0 ;
for ( int i = n; i <= m; i++ ) {
string strnum = to_string ( i) ;
int sum = 0 ;
for ( int j = 0 ; j < strnum. length ( ) ; j++ ) {
sum += pow ( ( strnum[ j] - '0' ) , strnum. length ( ) ) ;
}
if ( sum == i) {
cout << i << " " ;
total++ ;
}
}
if ( total == 0 ) {
cout << "none" ;
}
}
a042. 平面圓形切割
# include <bits/stdc++.h>
int main ( ) {
int n;
while ( std:: cin>> n)
std:: cout<< n* n- n+ 2 << std:: endl;
}
a053. Sagit's 計分程式
# include <bits/stdc++.h>
using namespace std;
int main ( )
{
int score;
cin>> score;
if ( score<= 10 )
cout<< score* 6 ;
if ( 10 < score and score<= 20 )
cout<< 60 + ( score- 10 ) * 2 ;
if ( 20 < score and score<= 40 )
cout<< 80 + ( score- 20 ) * 1 ;
if ( score> 40 )
cout<< "100" ;
}
a059. 完全平方和
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
int a, b;
cin>> a>> b;
int sum= 0 ;
for ( int j= 0 ; j< 32 ; j++ ) {
if ( a<= ( j* j) and j* j<= b)
sum+= j* j;
}
cout<< "Case " << i+ 1 << ": " << sum<< endl;
}
}
a104. 排序
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
while ( cin>> n) {
int ptr[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> ptr[ i] ;
sort ( ptr, ptr+ n) ;
for ( auto && k: ptr)
cout<< k<< " " ;
cout<< endl;
}
}
a121. 質數又來囉
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b;
while ( cin>> a>> b) {
int total= 0 ;
for ( int i= a; i<= b; i++ ) {
int y= 1 ;
if ( i== 1 ) { y= 0 ; }
for ( int j= 2 ; j* j<= i; j++ )
if ( i% j== 0 ) {
y= 0 ;
break ; }
total+= y;
}
cout<< total<< endl;
}
}
a147. Print it all
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
while ( cin>> n) {
if ( n== 0 ) {
break ;
}
else {
for ( int i= 1 ; i< n; i++ ) {
if ( i% 7 != 0 )
cout<< i<< " " ;
}
cout<< endl;
}
}
}
a148. You Cannot Pass?!
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
float n;
while ( cin>> n) {
float total= 0 ;
for ( int i= 0 ; i< n; i++ ) {
float score;
cin>> score;
total+= score;
}
if ( ( total/ n) > 59 )
cout<< "no" << endl;
else
cout<< "yes\n" ;
}
}
a149. 乘乘樂
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
string str;
cin. ignore ( ) ;
for ( int i= 0 ; i< n; i++ ) {
getline ( cin, str) ;
int sum= 1 ;
for ( int j= 0 ; j< str. size ( ) ; j++ )
sum= sum* ( str[ j] - '0' ) ;
cout<< sum<< endl;
}
}
a215. 明明愛數數
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b;
while ( cin>> a>> b) {
int sum= a;
int total= 1 ;
while ( sum<= b) {
a++ ;
sum+= a;
total++ ;
}
cout<< total<< endl;
}
}
a216. 數數愛明明
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
long long int n;
while ( cin>> n) {
cout<< n* ( n+ 1 ) / 2 << " " << n* ( n+ 1 ) * ( 2 * n+ 4 ) / 12 << endl;
}
}
b970. 我不說髒話 (續)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
cout<< i+ 1 << ". I don't say swear words!" << endl;
}
}
b971. 等差數列
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b, r; cin>> a>> b>> r; while ( a!= b) { cout<< a<< " " ; a+= r; } cout<< a; }
c431. Sort ! Sort ! Sort !
# include <stdio.h>
int main ( ) {
int a[ 101 ] ;
for ( int i= 1 ; i< 101 ; i++ ) {
a[ i] = 0 ;
}
int n;
scanf ( "%d" , & n) ;
for ( int i= 0 ; i< n; i++ ) {
int now;
scanf ( "%d" , & now) ;
a[ now] ++ ;
}
for ( int i= 1 ; i< 101 ; i++ ) {
if ( a[ i] != 0 ) {
for ( int j= 0 ; j< a[ i] ; j++ ) {
printf ( "%d" , i) ;
printf ( " " ) ;
}
}
}
}
c659. 連接詞
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string conj;
cin>> conj;
string other;
cin. ignore ( ) ;
getline ( cin, other) ;
int s= other. size ( ) ;
for ( int i= 0 ; i< s; i++ ) {
cout<< other[ i] ;
if ( other[ i] == ' ' ) {
cout<< conj;
cout<< " " ;
}
}
}
c717 You can say that again c726 K-I-S-S-I-N-G (一行版)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
string a, b;
cin>> a>> b;
cout<< a<< " and " << b<< " sitting in the tree" ;
}
d049. 中華民國萬歲!
# include <iostream>
using namespace std;
int main ( ) {
int y;
cin>> y;
cout<< y- 1911 ;
}
d050. 妳那裡現在幾點了?
# include <bits/stdc++.h>
using namespace std;
int main ( ) { int num; cin>> num; cout<< ( num+ 24 - 15 ) % 24 ; }
d051. 糟糕,我發燒了!
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
double f= 0.000 ;
cin>> f;
cout<< fixed<< setprecision ( 3 ) << ( f- 32 ) * 5 / 9 ;
}
d058. BASIC 的 SGN 函數
# include <bits/stdc++.h>
using namespace std;
int main ( ) { int n; cin>> n; cout<< ( n> 0 ) - ( n< 0 ) ; }
d060. 還要等多久啊?
# include <bits/stdc++.h>
using namespace std;
int main ( ) { int n; cin>> n; cout<< ( 25 - n+ 60 ) % 60 ; }
d063. 0 與 1
# include <bits/stdc++.h>
using namespace std;
int main ( ) { int num; cin>> num; cout<< 1 - num; }
d064. ㄑㄧˊ 數?
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
if ( n% 2 == 0 ) {
cout<< "Even" ;
}
else
cout<< "Odd" ;
}
d827. 買鉛筆
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
cout<< n/ 12 * 50 + ( n% 12 ) * 5 ;
}
e051. 文意字彙
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string n;
cin>> n;
for ( int i= 0 ; i< n. size ( ) ; i++ ) {
if ( i== 0 or i== n. size ( ) - 1 ) {
cout<< n[ i] ;
}
else
cout<< "_" ;
}
}
e447. queue 練習
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
vector< int > ptr;
for ( int i= 0 ; i< n; i++ ) {
int in;
cin>> in;
if ( in== 1 ) {
int x;
cin>> x;
ptr. push_back ( x) ;
}
if ( in== 2 ) {
if ( ptr. size ( ) != 0 ) {
cout<< ptr[ 0 ] << endl;
}
else
cout<< "-1" << endl;
}
if ( in== 3 ) {
if ( ptr. size ( ) != 0 )
ptr. erase ( ptr. begin ( ) ) ;
}
}
}
e621. 1. 免費停車 (Free Parking)
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
int a, b, r;
cin>> a>> b>> r;
int yn= 1 ;
for ( int i= a+ 1 ; i< b; i++ ) {
if ( i% r!= 0 ) {
cout<< i<< " " ;
yn= 0 ;
}
}
if ( yn== 1 ) {
cout<< "No free parking spaces." ;
}
cout<< "\n" ;
}
}
競賽題庫 e521. 106 彰雲嘉區複賽 - Q1 三角形
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int t[ 3 ] ;
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) {
cin>> t[ 0 ] >> t[ 1 ] >> t[ 2 ] ;
sort ( t, t+ 3 ) ;
int a= t[ 0 ] , b= t[ 1 ] , c= t[ 2 ] ;
if ( a+ b> c) {
cout<< "1 " ;
if ( a== b or b== c or c== a) {
cout<< "1" << endl;
}
else
cout<< "0" << endl;
}
else {
cout<< "0" << endl;
}
}
}
IOI/APCS b964.成績指標
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int size;
cin>> size;
int ptr[ size] ;
for ( int i; i< size; i++ )
cin>> ptr[ i] ;
sort ( ptr, ptr+ size) ;
for ( int i= 0 ; i< size; i++ )
cout<< ptr[ i] << " " ;
int low= 101 , high= - 1 ;
for ( int i= 0 ; i< size; i++ ) {
if ( ptr[ i] >= 60 and ptr[ i] < low)
low= ptr[ i] ;
if ( ptr[ i] < 60 and ptr[ i] > high)
high= ptr[ i] ;
}
if ( low== 101 )
cout<< "\n" << high<< "\nworst case" ;
else if ( high== - 1 )
cout<< "\nbest case" << "\n" << low;
else
cout<< "\n" << high<< "\n" << low;
return 0 ;
}
b966.線段覆蓋長度
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
long int N;
cin>> N;
long int start[ N] , end[ N] ;
for ( int i= 0 ; i< N; i++ ) {
cin>> start[ i] >> end[ i] ;
}
for ( int i= 0 ; i< N; i++ )
for ( int j= 0 ; j< N; j++ ) {
if ( i!= j) {
if ( start[ i] <= end[ j] and start[ i] >= start[ j] ) {
if ( end[ i] <= end[ j] and end[ i] >= start[ j] ) {
end[ i] = 0 ;
start[ i] = 0 ;
}
else {
start[ i] = end[ j] ;
}
}
if ( end[ i] > start[ j] and end[ i] < end[ j] )
end[ i] = start[ j] ;
}
}
long int total= 0 ;
for ( int i= 0 ; i< N; i++ )
total= total+ end[ i] - start[ i] ;
cout<< total;
}
c290. APCS 2017-0304-1秘密差
# include <cstdlib>
# include <iostream>
using namespace std;
int main ( ) {
string num;
cin>> num;
int sum= 0 , change= 1 ;
for ( int i= 0 ; i< num. size ( ) ; i++ ) {
sum+= ( num[ i] - 48 ) * change;
change*= - 1 ;
}
cout << abs ( sum) << endl;
}
c291. APCS 2017-0304-2小群體
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n, total= 0 ;
cin>> n;
int people[ 50000 ] ;
for ( int i= 0 ; i< n; i++ )
cin>> people[ i] ;
for ( int i= 0 ; i< n; i++ ) {
if ( people[ i] != - 1 ) {
for ( int temp= people[ i] ; temp!= - 1 ; ) {
int k= temp;
temp= people[ temp] ;
people[ k] = - 1 ;
}
total++ ;
}
}
cout<< total;
return 0 ;
}
c292. APCS2017-0304-3數字龍捲風
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n, way;
cin>> n>> way;
int map[ n] [ n] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
cin>> map[ i] [ j] ;
}
}
int x= n/ 2 , y= n/ 2 ;
cout<< map[ y] [ x] ;
int l= 1 ;
for ( l= 1 ; l< n+ 1 ; l++ ) {
for ( int i= 0 ; i< 2 ; i++ ) {
for ( int j= 0 ; j< l; j++ ) {
if ( way== 0 ) {
x= x- 1 ;
y= y;
}
if ( way== 1 ) {
x= x;
y= y- 1 ;
}
if ( way== 2 ) {
x= x+ 1 ;
y= y;
}
if ( way== 3 ) {
x= x;
y= y+ 1 ;
}
if ( x>= 0 and x< n and y>= 0 and y< n)
cout<< map[ y] [ x] ;
}
way= ( way+ 1 ) % 4 ;
}
}
}
c294. APCS-2016-1029-1三角形辨別
# include <iostream>
using namespace std;
int main ( ) {
float a, b, c;
cin>> a>> b>> c;
if ( a> b)
swap ( a, b) ;
if ( a> c)
swap ( a, c) ;
if ( b> c)
swap ( b, c) ;
cout<< a<< " " << b<< " " << c<< "\n" ;
if ( a+ b<= c)
cout<< "No" ;
else {
if ( a* a+ b* b< c* c)
cout<< "Obtuse\n" ;
if ( a* a+ b* b== c* c)
cout<< "Right\n" ;
if ( a* a+ b* b> c* c)
cout<< "Acute\n" ;
}
return 0 ;
}
c295. APCS-2016-1029-2最大和
# include <iostream>
using namespace std;
# include <bits/stdc++.h>
int main ( ) {
int n, m, ans = 0 ;
cin >> n >> m;
int s[ n] , xi[ m] ;
for ( int i = 0 ; i < n; i++ ) {
int maxs = 0 ;
for ( int j = 0 ; j < m; j++ ) {
cin >> xi[ j] ;
maxs = max ( maxs, xi[ j] ) ;
}
s[ i] = maxs;
}
for ( int i = 0 ; i < n; i++ ) {
ans += s[ i] ;
}
cout << ans << '\n' ;
int count = 0 ;
for ( int i = 0 ; i < n; i++ ) {
if ( ans % s[ i] == 0 ) {
if ( count == 0 ) {
cout << s[ i] ;
}
else {
cout << ' ' << s[ i] ;
}
count += 1 ;
}
}
if ( count == 0 ) {
cout << - 1 ;
}
}
c296. APCS-2016-1029-3定時K彈
# include <iostream>
using namespace std;
int main ( ) {
int N, M, K;
while ( cin >> N >> M >> K) {
int ans = 0 ;
for ( int i = N - K + 1 ; i <= N; i++ )
ans = ( ans + M) % i;
cout << ans + 1 << "\n" ;
}
return 0 ;
}
c453. TOI2003 第二題:疊羅漢
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
long long int dp[ n+ 1 ] ;
dp[ 0 ] = 1 ;
dp[ 1 ] = 1 ;
for ( int i= 2 ; i< n+ 1 ; i++ ) {
dp[ i] = 0 ;
for ( int j= 0 ; j< i; j++ )
dp[ i] += dp[ j] * dp[ i- 1 - j] ;
}
cout<< dp[ n] ;
}
c461. apcs 邏輯運算子 (Logic Operators)
# include <bits/stdc++.h>
# include <iso646.h>
using namespace std;
int main ( ) {
int a, b, c;
cin>> a>> b>> c;
int yn= 0 ;
if ( ( a and b) == c) {
cout<< "AND\n" ;
yn= 1 ;
}
if ( ( a or b) == c) {
cout<< "OR\n" ;
yn= 1 ;
}
if ( ( ( ! a and b) or ( ! b and a) ) == c) {
cout<< "XOR\n" ;
yn= 1 ;
}
if ( yn== 0 )
cout<< "IMPOSSIBLE" ;
}
c462. apcs 交錯字串 (Alternating Strings)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int k;
string s;
cin>> k>> s;
int max= 0 ;
int sc[ s. length ( ) ] ;
for ( int i= 0 ; i< s. length ( ) ; i++ ) {
if ( s[ i] <= 'Z' and s[ i] >= 'A' )
sc[ i] = 1 ;
else
sc[ i] = 0 ;
}
vector< int > a;
int cnt= 1 ;
for ( int i= 1 ; i< s. length ( ) ; i++ ) {
if ( sc[ i] == sc[ i- 1 ] )
cnt++ ;
else {
a. push_back ( cnt) ;
cnt= 1 ; }
}
a. push_back ( cnt) ;
cnt= 0 ;
for ( int i= 0 ; i< a. size ( ) ; i++ ) {
if ( a[ i] == k) {
cnt++ ;
if ( cnt> max) {
max= cnt;
} }
else {
if ( a[ i] > k) {
cnt++ ;
if ( cnt> max)
max= cnt;
cnt= 1 ;
}
else
cnt= 0 ;
}
}
cout<< max* k;
}
d904. 換零錢
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int num, n;
cin>> num>> n;
int coin[ 15 ] ;
int dp[ 1005 ] ;
dp[ 0 ] = 0 ;
for ( int i= 0 ; i< n; i++ ) {
int in;
cin>> in;
coin[ i] = in;
}
for ( int i= 1 ; i< num+ 1 ; i++ ) {
dp[ i] = 1001 ;
for ( int j= 0 ; j< n; j++ ) {
if ( i- coin[ j] >= 0 )
dp[ i] = min ( dp[ i] , 1 + dp[ i - coin[ j] ] ) ;
}
}
cout<< dp[ num] ;
}
e283. APCS 類似題 - 小崴的特殊編碼
# include <iostream>
# include <map>
# define fastio ios_base:: sync_with_stdio ( false ) ; cin. tie ( 0 )
using namespace std;
map< string, string> mp;
void init ( ) {
mp[ "0 1 0 1" ] = "A" ; mp[ "0 1 1 1" ] = "B" ; mp[ "0 0 1 0" ] = "C" ;
mp[ "1 1 0 1" ] = "D" ; mp[ "1 0 0 0" ] = "E" ; mp[ "1 1 0 0" ] = "F" ;
}
int main ( ) {
fastio;
init ( ) ;
int N;
while ( cin>> N) {
string ans, ignore;
getline ( cin, ignore) ;
while ( N-- ) {
string s;
getline ( cin, s) ;
ans+= mp[ s] ;
}
cout<< ans<< "\n" ;
}
return 0 ;
}
e286. 籃球比賽
# include <iostream>
using namespace std;
int main ( ) {
int a[ 5 ] = { 0 } , n;
for ( int i= 1 ; i<= 4 ; i++ )
for ( int j= 1 ; j<= 4 ; j++ ) { cin>> n; a[ i] += n; }
cout << a[ 1 ] << ":" << a[ 2 ] << "\n" ;
cout << a[ 3 ] << ":" << a[ 4 ] << "\n" ;
if ( a[ 1 ] > a[ 2 ] && a[ 3 ] > a[ 4 ] ) cout<< "Win\n" ;
else if ( a[ 1 ] < a[ 2 ] && a[ 3 ] < a[ 4 ] ) cout<< "Lose\n" ;
else cout<< "Tie\n" ;
}
f312. 1. 人力分配
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int maxt= - 2147483647 ;
int n;
int a_1, b_1, c_1, a_2, b_2, c_2;
cin>> a_1>> b_1>> c_1>> a_2>> b_2>> c_2>> n;
for ( int i= 0 ; i< n+ 1 ; i++ ) {
int y_1= a_1* i* i+ b_1* i;
int y_2= a_2* ( n- i) * ( n- i) + b_2* ( n- i) ;
maxt= max ( maxt, y_1+ y_2) ;
}
cout<< maxt+ c_1+ c_2;
}
f514. 拼字遊戲 (Spelling)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string v, f;
cin>> v>> f;
int vs= v. size ( ) ;
int fs= f. size ( ) ;
int op[ fs] ;
for ( int i= 0 ; i< fs; i++ ) {
op[ i] = - 1 ;
for ( int j= 0 ; j< vs; j++ ) {
if ( v[ j] == f[ i] ) {
op[ i] = j+ 1 ;
v[ j] = ' ' ;
break ;
}
}
}
for ( int i= 0 ; i< fs; i++ ) {
if ( op[ i] == - 1 ) {
cout<< "X" << " " ;
}
else {
cout<< op[ i] << " " ;
}
}
}
f579. 1. 購物車
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b, people, total= 0 ;
cin>> a>> b;
cin>> people;
int ptr[ people] [ 100 ] ;
for ( int i= 0 ; i< people; i++ )
for ( int j= 0 ; j< 100 ; j++ ) {
cin>> ptr[ i] [ j] ;
if ( ptr[ i] [ j] == 0 )
break ;
}
int buy_a, buy_b;
for ( int i= 0 ; i< people; i++ ) {
buy_a= 0 ;
buy_b= 0 ;
for ( int j= 0 ; j< 100 ; j++ ) {
if ( ptr[ i] [ j] == a )
buy_a++ ;
if ( ptr[ i] [ j] == b)
buy_b++ ;
if ( ptr[ i] [ j] == ( - 1 * a) )
buy_a-- ;
if ( ptr[ i] [ j] == ( - 1 * b) )
buy_b-- ;
if ( ptr[ i] [ j] == 0 )
break ;
}
if ( buy_a>= 1 and buy_b>= 1 )
total++ ;
}
cout<< total;
return 0 ;
}
f581. 3. 圓環出口
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n, m;
cin>> n>> m;
int house[ n] ;
int ch[ m] ;
for ( int i= 0 ; i< n; i++ )
cin>> house[ i] ;
for ( int i= 0 ; i< m; i++ )
cin>> ch[ i] ;
int t= 0 ;
for ( int i= 0 ; i< m; i++ ) {
int sum= ch[ i] ;
while ( sum> 0 ) {
sum -= house[ t] ;
t = ( t + 1 ) % n;
}
}
cout<< t;
}
h028. 202001_3 砍樹
# include <iostream>
using namespace std;
int main ( ) {
int size;
int road;
cin>> size>> road;
int c[ size+ 2 ] = { 0 } ;
c[ size+ 1 ] = road;
int h[ size] ;
for ( int i= 1 ; i< size+ 1 ; i++ )
cin>> c[ i] ;
for ( int i= 0 ; i< size; i++ )
cin>> h[ i] ;
int cut= - 1 ;
int cut_down= 0 ;
int max= 0 ;
while ( cut!= 0 ) {
cut= 0 ;
for ( int i= 1 ; i< size+ 1 ; i++ ) {
if ( h[ i- 1 ] <= c[ i] - c[ i- 1 ] or h[ i- 1 ] <= c[ i+ 1 ] - c[ i] ) {
cut_down++ ;
cut++ ;
if ( h[ i- 1 ] > max)
max= h[ i- 1 ] ;
for ( int j= i; j< size; j++ )
c[ j] = c[ j+ 1 ] ;
for ( int j= i- 1 ; j< size- 1 ; j++ )
h[ j] = h[ j+ 1 ] ;
size-- ;
}
}
}
cout<< cut_down<< "\n" << max;
return 0 ;
}
k732 2.特殊位置
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n, m;
cin>> n>> m;
int mapp[ 50 ] [ 50 ] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
cin>> mapp[ i] [ j] ;
}
}
int total= 0 ;
vector< int > x;
vector< int > y;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
int xx= mapp[ i] [ j] ;
int totalnow= 0 ;
for ( int k= 0 ; k< n; k++ ) {
for ( int l= 0 ; l< m; l++ ) {
if ( abs ( k- i) + abs ( l- j) <= xx) {
totalnow+= mapp[ k] [ l] ;
totalnow= totalnow% 10 ;
}
}
}
if ( totalnow== xx) {
total++ ;
x. push_back ( i) ;
y. push_back ( j) ;
}
}
}
cout<< total<< endl;
for ( int i= 0 ; i< x. size ( ) ; i++ ) {
cout<< x[ i] << " " << y[ i] << endl;
}
}
j605. 1. 程式考試
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n;
cin>> n;
int t[ n] , s[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> t[ i] >> s[ i] ;
int max= - 2 , miss= 0 , time= 0 ;
int sc[ n] ;
for ( int i= 0 ; i< n; i++ )
if ( s[ i] == - 1 )
miss++ ;
for ( int i= 0 ; i< n; i++ ) {
int scn= s[ i] - n- ( miss* 2 ) ;
if ( scn< 0 )
scn= 0 ;
sc[ i] = scn;
if ( scn> max) {
max= scn; time= t[ i] ; }
}
cout<< max<< " " << time;
}
m370. 1. 機械鼠
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int x, n;
while ( cin>> x>> n) {
int ptr[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> ptr[ i] ;
sort ( ptr, ptr+ n) ;
int low= 0 , high= 0 ;
for ( int i= 0 ; i< n; i++ ) {
if ( ptr[ i] < x)
low++ ;
else
high++ ;
}
if ( low> high) {
cout<< low<< " " << ptr[ 0 ] ;
}
else
cout<< high<< " " << ptr[ n- 1 ] ;
}
}
m371. 2. 卡牌遊戲
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n, m;
while ( cin>> n>> m) {
int ptr[ n] [ m] ;
for ( int i= 0 ; i< n; i++ )
for ( int j= 0 ; j< m; j++ )
cin>> ptr[ i] [ j] ;
int yes= 0 ;
int total= 0 ;
while ( yes== 0 ) {
yes= 1 ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
if ( ptr[ i] [ j] != - 1 ) {
for ( int k= j+ 1 ; k< m; k++ ) {
if ( ptr[ i] [ k] != - 1 ) {
if ( ptr[ i] [ j] == ptr[ i] [ k] ) {
total+= ptr[ i] [ j] ;
ptr[ i] [ j] = - 1 ;
ptr[ i] [ k] = - 1 ;
yes= 0 ;
}
break ;
}
}
}
}
}
for ( int i= 0 ; i< m; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
if ( ptr[ j] [ i] != - 1 ) {
for ( int k= j+ 1 ; k< n; k++ ) {
if ( ptr[ k] [ i] != - 1 ) {
if ( ptr[ j] [ i] == ptr[ k] [ i] ) {
total+= ptr[ j] [ i] ;
ptr[ j] [ i] = - 1 ;
ptr[ k] [ i] = - 1 ;
yes= 0 ;
}
break ;
}
}
}
}
}
}
cout<< total;
}
}
m373. 4. 投資遊戲
# include "bits/stdc++.h"
using namespace std;
int main ( ) {
int n, r;
while ( cin>> n>> r) {
int ptr[ n] ;
for ( int i= 0 ; i< n; i++ )
cin>> ptr[ i] ;
int max= 0 ;
if ( r== 0 ) {
for ( int i= 0 ; i< n; i++ ) {
int nowmax= ptr[ i] ;
for ( int j= i+ 1 ; j< n; j++ ) {
nowmax+= ptr[ j] ;
if ( nowmax> max)
max= nowmax;
}
}
}
else {
for ( int i= 0 ; i< n; i++ ) {
for ( int j= i+ 1 ; j< n; j++ ) {
int nowmax= 0 ;
int ptrc[ j- i+ 1 ] ;
int q= i;
for ( int k= 0 ; k< j- i+ 1 ; k++ ) {
ptrc[ k] = ptr[ q] ;
q++ ;
}
sort ( ptrc, ptrc+ j- i+ 1 ) ;
for ( int k= 0 ; k< r; k++ ) {
if ( ptrc[ k] < 0 )
ptrc[ k] = 0 ;
}
for ( int k= 0 ; k< j- i+ 1 ; k++ ) {
nowmax+= ptrc[ k] ;
if ( nowmax< max) ;
break ;
}
if ( nowmax> max)
max= nowmax;
}
}
}
cout<< max;
}
}
m931. 1. 遊戲選角
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
int atk[ n] , def[ n] ;
int total[ n] , sortt[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> atk[ i] >> def[ i] ;
total[ i] = pow ( atk[ i] , 2 ) + pow ( def[ i] , 2 ) ;
sortt[ i] = total[ i] ;
}
sort ( sortt, sortt+ n) ;
int i= 0 ;
while ( sortt[ n- 2 ] != total[ i] ) {
i++ ;
}
cout<< atk[ i] << " " << def[ i] << "\n" ;
}
m932. 2. 蜜蜂觀察
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ;
vector< vector< char >> v;
bool t[ 123 ] = { 0 } ;
int m, n, k, d;
string s;
cin>> m>> n>> k;
int nowx= m- 1 , nowy= 0 , cnt= 0 ;
v. resize ( m, vector < char > ( n) ) ;
for ( int i= 0 ; i< m; i++ ) {
cin>> s;
for ( int j= 0 ; j< n; j++ ) {
v[ i] [ j] = s[ j] ;
}
}
for ( int i= 0 ; i< k; i++ ) {
cin>> d;
if ( d== 0 ) {
if ( ( nowx- 1 ) >= 0 ) nowx-- ;
}
else if ( d== 1 ) {
if ( ( nowy+ 1 ) < n) nowy++ ;
}
else if ( d== 2 ) {
if ( ( nowx+ 1 ) < m&& ( nowy+ 1 ) < n) nowx++ , nowy++ ;
}
else if ( d== 3 ) {
if ( ( nowx+ 1 ) < m) nowx++ ;
}
else if ( d== 4 ) {
if ( ( nowy- 1 ) >= 0 ) nowy-- ;
}
else if ( d== 5 ) {
if ( ( nowx- 1 ) >= 0 && ( nowy- 1 ) >= 0 ) nowx-- , nowy-- ;
}
cout<< v[ nowx] [ nowy] ;
t[ ( int ) ( v[ nowx] [ nowy] ) ] = 1 ;
}
for ( int i= 'A' ; i<= 'Z' ; i++ ) {
if ( t[ i] == 1 ) cnt++ ;
}
for ( int i= 'a' ; i<= 'z' ; i++ ) {
if ( t[ i] == 1 ) cnt++ ;
}
cout<< "\n" << cnt; }
n630. 電影院 (Cinema)
# include <bits/stdc++.h>
using namespace std;
struct t {
int hour;
int mi;
} ;
int main ( ) {
int n;
cin>> n;
t times[ 1001 ] ;
t now;
int yn= - 1 ;
t p;
p. hour= 99 ;
p. mi= 0 ;
for ( int i= 0 ; i< n; i++ ) {
cin>> times[ i] . hour>> times[ i] . mi;
}
cin>> now. hour>> now. mi;
now. mi+= 20 ;
if ( now. mi> 60 ) {
now. mi-= 60 ;
now. hour++ ;
}
for ( int i= 0 ; i< n; i++ ) {
if ( times[ i] . hour> now. hour) {
p. hour= times[ i] . hour;
p. mi= times[ i] . mi;
yn= 0 ;
break ;
}
else if ( times[ i] . hour== now. hour and times[ i] . mi>= now. mi) {
p. hour= times[ i] . hour;
p. mi= times[ i] . mi;
yn= 0 ;
break ;
}
}
if ( yn== 0 ) {
if ( p. hour< 10 ) {
cout<< "0" << p. hour<< " " << setw ( 2 ) << setfill ( '0' ) << p. mi;
}
else
cout<< p. hour<< " " << setw ( 2 ) << setfill ( '0' ) << p. mi;
}
else {
cout<< "Too Late" ;
}
}
o076. 1. 特技表演
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int n;
cin>> n;
int ptr[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> ptr[ i] ;
}
int total= 0 ;
int totalnow= 1 ;
for ( int i= 0 ; i< n- 1 ; i++ ) {
if ( ptr[ i] > ptr[ i+ 1 ] ) {
totalnow++ ;
total= max ( total, totalnow) ;
}
else {
totalnow= 1 ;
}
}
cout<< total;
}
o077. 2. 電子畫布
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a[ 100 ] [ 100 ] ;
int h= 0 , w= 0 , n= 0 ;
cin>> h>> w>> n;
for ( int i= 0 ; i< n; i++ ) {
int r= 0 , c= 0 , t= 0 , x= 0 ;
cin>> r>> c>> t>> x;
for ( int j= 0 ; j< h; j++ ) {
for ( int k= 0 ; k< w; k++ ) {
if ( abs ( j- r) + abs ( k- c) <= t) {
a[ j] [ k] += x;
}
}
}
}
for ( int j= 0 ; j< h; j++ ) {
for ( int k= 0 ; k< w; k++ ) {
cout<< a[ j] [ k] << " " ;
}
cout<< endl;
}
}
o257. 會議室租用 (Meeting Room)
# include <bits/stdc++.h>
using namespace std;
struct Meeting {
int start;
int end;
int p;
} ;
bool cmp ( const Meeting & a, const Meeting & b) {
if ( a. end == b. end) {
return a. p < b. p;
}
return a. end < b. end;
}
int main ( ) {
int n;
cin>> n;
Meeting meeting[ n] ;
for ( int i= 0 ; i< n; i++ ) {
cin>> meeting[ i] . start>> meeting[ i] . end>> meeting[ i] . p;
}
sort ( meeting, meeting+ n, cmp) ;
int dp[ n] ;
dp[ 0 ] = meeting[ 0 ] . p;
for ( int i= 1 ; i< n; i++ ) {
int now= meeting[ i] . p;
int left= 0 , right= i- 1 ;
while ( left<= right) {
int mid= ( left+ right) / 2 ;
if ( meeting[ mid] . end< meeting[ i] . start) {
left= mid+ 1 ;
}
else {
right= mid- 1 ;
}
}
if ( right>= 0 )
now+= dp[ right] ;
dp[ i] = max ( dp[ i- 1 ] , now) ;
}
cout<< dp[ n- 1 ] ;
}
o578. 起司 (Cheese)
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
int a, b, c, k;
cin>> a>> b>> c>> k;
if ( a% k== 0 and b% k== 0 and c% k== 0 ) {
cout<< ( a/ k) * ( b/ k) * ( c/ k) ;
}
else
cout<< "0" ;
}
o711. 1. 裝飲料
# include <iostream>
using namespace std;
int main ( ) {
int n; cin >> n;
int w1, w2, h1, h2; cin >> w1 >> w2 >> h1 >> h2;
int a1 = w1* w1, a2 = w2* w2;
int v1 = a1* h1, v2 = a2* h2;
int data[ n] ;
for ( int i= 0 ; i< n; i++ ) cin >> data[ i] ;
int total= 0 ;
int high[ n] = { 0 } ;
for ( int i= 0 ; i< n; i++ ) {
total+= data[ i] ;
if ( total< v1) {
high[ i] = total/ a1;
}
else if ( total>= v1 and total<= v1+ v2) {
high[ i] = h1+ ( total- v1) / a2;
}
else {
high[ i] = h1+ h2;
}
}
int maxx= high[ 0 ] ;
for ( int i= 1 ; i< n; i++ ) {
maxx= max ( maxx, high[ i] - high[ i- 1 ] ) ;
}
cout<< maxx;
}
UVa 題庫 a012. 10055 - Hashmat the Brave Warrior
# include <iostream>
# include <cmath>
using namespace std;
int main ( ) {
long long int a, b;
while ( cin>> a>> b) {
cout<< abs ( a- b) << endl;
}
}
e605. 10189 - Minesweeper
# include "bits/stdc++.h"
using namespace std;
const int dx[ ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ;
const int dy[ ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ;
int main ( ) {
int n, m;
int id= 1 ;
while ( cin>> n>> m) {
if ( n== 0 and m== 0 ) break ;
string mine[ n] ;
int ans[ n] [ m] ;
cin. ignore ( ) ;
for ( int i= 0 ; i< n; i++ ) {
getline ( cin, mine[ i] ) ;
}
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
int cnt= 0 ;
if ( mine[ i] [ j] == '.' ) {
for ( int k= 0 ; k< 8 ; k++ ) {
if ( i- dy[ k] >= 0 and i- dy[ k] < n and j- dx[ k] >= 0 and j- dx[ k] < m and mine[ i- dy[ k] ] [ j- dx[ k] ] == '*' ) {
cnt++ ;
}
}
}
ans[ i] [ j] = cnt;
}
}
cout<< "Field #" << id<< ":" << endl;
id++ ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
if ( mine[ i] [ j] == '*' ) {
cout<< "*" ;
}
else
cout<< ans[ i] [ j] ;
}
cout<< "\n\n" ;
}
}
}