POJ 1003
題目鏈接 http://poj.org/problem?id=1003 大意:長度=1/2+1/3+…+1/n,給定長度值,求n
#include <iostream>
using namespace std
;
int main ( )
{ float len
= 0 , sum
; int n
; while ( cin
>> len
&& len
!= 0 ) { for ( n
= 2 , sum
= 0 ; sum
< len
; ++ n
) { sum
+ = 1 / ( n
* 1.0 ) ; } cout
<< n
- 2 << " " << "card(s)" << endl
; } return 0 ;
}
POJ 1004
題目鏈接 http://poj.org/problem?id=1004 大意:求平均數
#include <iostream>
using namespace std
;
int main ( )
{ float money
, avgmoney
= 0 ; int i
= 0 ; while ( cin
>> money
&& i
!= 12 ) { avgmoney
+ = money
; ++ i
; } cout
<< "$" << avgmoney
/ 12 << endl
;
}
POJ 1005
題目鏈接 http://poj.org/problem?id=1005 大意:求一個點什么時候被慢慢變大的半圓吃掉
#include <iostream>
using namespace std
;
#define PI 3.141592654
int main ( )
{ double X
, Y
; double area
, distance
, dangerdistance
; int year
; int N
; cin
>> N
; for ( int i
= 1 ; i
<= N
; ++ i
) { cin
>> X
>> Y
; distance
= X
* X
+ Y
* Y
; dangerdistance
= 0 ; for ( area
= 0 , year
= 0 ; dangerdistance
<= distance
; ++ year
) { area
+ = 50 ; dangerdistance
= 2 * area
/ PI
; } cout
<< "Property " << i
<< ": This property will begin eroding in year " << year
<< "." << endl
; } cout
<< "END OF OUTPUT." ; return 0 ;
}
POJ 1207
題目鏈接 http://poj.org/problem?id=1207 大意:有一個數,按那個規則,最后能夠轉換到1,算出這個序列的長度,然后輸入兩個數,在這兩個數構成的閉區間中,每個數都有其序列長度,求這個序列中最長的一個。
#include <iostream>
using namespace std
;
int main ( )
{ int a
, b
; while ( cin
>> a
>> b
) { cout
<< a
<< " " << b
<< " " ; if ( a
> b
) swap ( a
, b
) ; int maxcyclen
= 0 ; int cyctime
= 0 ; int num
= 0 ; for ( int i
= a
, j
= 0 ; i
<= b
; ++ i
, ++ j
) { num
= i
; cyctime
= 1 ; while ( num
!= 1 ) { if ( num
% 2 == 1 ) { num
= 3 * num
+ 1 ; } else { num
/ = 2 ; } ++ cyctime
; } if ( maxcyclen
< cyctime
) { maxcyclen
= cyctime
; } } cout
<< maxcyclen
<< endl
; } return 0 ;
}
POJ 3299
題目鏈接 http://poj.org/problem?id=3299 大意:公式推導,給任意兩個,求第三個。(注意輸入順序)
#include <iostream>
using namespace std
;
#include <iomanip>
#include <math.h> int main ( )
{ cout
<< setiosflags ( ios
:: fixed
) << setprecision ( 1 ) ; double t
, d
, h
, num1
, num2
; char alpha
, beta
; while ( cin
>> alpha
&& alpha
!= 'E' ) { cin
>> num1
>> beta
>> num2
; if ( alpha
== 'T' && beta
== 'D' || alpha
== 'D' && beta
== 'T' ) { t
= num1
; d
= num2
; if ( alpha
== 'D' ) swap ( t
, d
) ; h
= t
+ 0.5555 * ( 6.11 * exp ( 5417.7530 * ( ( 1 / 273.16 ) - ( 1 / ( d
+ 273.16 ) ) ) ) - 10 ) ; } else if ( alpha
== 'T' && beta
== 'H' || alpha
== 'H' && beta
== 'T' ) { t
= num1
; h
= num2
; if ( alpha
== 'H' ) swap ( t
, h
) ; d
= 1 / ( 1 / 273.16 - log ( ( ( h
- t
) / 0.5555 + 10.0 ) / 6.11 ) / 5417.7530 ) - 273.16 ; ; } else if ( alpha
== 'D' && beta
== 'H' || alpha
== 'H' && beta
== 'D' ) { d
= num1
; h
= num2
; if ( alpha
== 'H' ) swap ( d
, h
) ; t
= h
- 0.5555 * ( 6.11 * exp ( 5417.7530 * ( ( 1 / 273.16 ) - ( 1 / ( d
+ 273.16 ) ) ) ) - 10.0 ) ; } cout
<< "T " << t
<< " D " << d
<< " H " << h
<< endl
; } return 0 ;
}
POJ 2159
題目鏈接 http://poj.org/problem?id=2159 大意: 1.一串字符串(大寫字母),每個字母可以按照一個數平移成另一個字母 2.每一個字符加密后,再亂序 3.給定A,B兩個字符串,求A是否可能是B的密文
之前題目理解錯了,以為每個字母都是平移1位(按照示例的理解)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std
;
int main ( )
{ int secretnum
= 1 ; char word1
, word2
; vector
< char > secrettext
, origintext
; while ( cin
. get ( word1
) ) { if ( word1
== '\n' ) break ; if ( word1
>= 'A' && word1
<= 'Z' ) { word1
= word1
- secretnum
; if ( word1
< 'A' ) word1
= word1
+ 26 ; secrettext
. push_back ( word1
) ; } else { continue ; } } while ( cin
. get ( word2
) ) { if ( word2
== '\n' ) break ; if ( word2
>= 'A' && word2
<= 'Z' ) { origintext
. push_back ( word2
) ; } else { continue ; } } sort ( secrettext
. begin ( ) , secrettext
. end ( ) ) ; sort ( origintext
. begin ( ) , origintext
. end ( ) ) ; for ( int i
= 0 ; i
!= secrettext
. size ( ) ; ++ i
) { if ( secrettext
[ i
] != origintext
[ i
] ) { cout
<< "NO" ; return 0 ; } } cout
<< "YES" ; return 0 ;
}
正確的解答
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std
;
int main ( )
{ char word1
, word2
; int secrettext
[ 26 ] = { 0 } , origintext
[ 26 ] = { 0 } ; while ( cin
. get ( word1
) ) { if ( word1
== '\n' ) break ; if ( word1
>= 'A' && word1
<= 'Z' ) { ++ secrettext
[ word1
- 'A' ] ; } else { continue ; } } while ( cin
. get ( word2
) ) { if ( word2
== '\n' ) break ; if ( word2
>= 'A' && word2
<= 'Z' ) { ++ origintext
[ word2
- 'A' ] ; } else { continue ; } } sort ( secrettext
, secrettext
+ 26 ) ; sort ( origintext
, origintext
+ 26 ) ; for ( int i
= 0 ; i
!= 26 ; ++ i
) { if ( secrettext
[ i
] != origintext
[ i
] ) { cout
<< "NO" ; return 0 ; } } cout
<< "YES" ; return 0 ;
}
POJ 1083
題目鏈接 http://poj.org/problem?id=1083 大意: 1.400個房間,從一個房間移動1張桌子到另一個房間,需要10分鐘。 2.過道只能有一張桌子,包含門前的位置,被占用的時候,其它需要經過的移動需要等待,不能同時進行。 解法: 1.申請200大小的數組代表門前的過道,從房間m到房間n,(m < n),則 ( m - 1 ) /2 到(n - 1)/ 2 的元素都加10; 2.掃描數組,最大的元素即為搬運的時間。
#include <iostream>
#include <cstring>
using namespace std
;
int main ( )
{ int testtime
, table
, roomid1
, roomid2
; int corridor
[ 200 ] = { 0 } , corridor_id
, maxtime
; cin
>> testtime
; while ( testtime
-- ) { memset ( corridor
, 0 , sizeof ( corridor
) ) ; cin
>> table
; while ( table
-- ) { cin
>> roomid1
>> roomid2
; if ( roomid1
> roomid2
) swap ( roomid1
, roomid2
) ; for ( corridor_id
= ( roomid1
- 1 ) / 2 ; corridor_id
<= ( roomid2
- 1 ) / 2 ; ++ corridor_id
) { corridor
[ corridor_id
] + = 10 ; } } maxtime
= 0 ; for ( corridor_id
= 0 ; corridor_id
!= 200 ; ++ corridor_id
) { if ( maxtime
< corridor
[ corridor_id
] ) maxtime
= corridor
[ corridor_id
] ; } cout
<< maxtime
<< endl
; }
}
POJ 3094
題目鏈接 http://poj.org/problem?id=3094 大意: 每一行字符串包括空格,經過對每個字符乘以系數,然后加總求和。 例如: ACM:1 * 1 + 2 * 3 + 3 * 13 = 46 (A=1,B=2,…空格=0) MID CENTRAL:1 * 13 + 2 * 9 + 3 * 4 + 4 * 0 (空格占位置)+ 5 * 3 + 6 * 5 + 7 * 14 + 8 * 20 + 9 * 18 + 10 * 1 + 11 * 12 = 650
#include <iostream>
using namespace std
;
int main ( )
{ char alpha
; int quicksum
= 0 ; while ( cin
. get ( alpha
) ) { if ( alpha
== '#' ) break ; quicksum
= 0 ; for ( int i
= 1 ; alpha
!= '\n' ; ++ i
) { if ( alpha
!= ' ' ) quicksum
= quicksum
+ i
* ( alpha
- 'A' + 1 ) ; cin
. get ( alpha
) ; } cout
<< quicksum
<< endl
; }
}
POJ 2388
題目鏈接 http://poj.org/problem?id=2388 大意:求中位數,簡單。
#include <iostream>
#include <algorithm>
using namespace std
;
int main ( )
{ int num
; cin
>> num
; int * milk
= new int [ num
] ; for ( int i
= 0 ; i
!= num
; ++ i
) { cin
>> milk
[ i
] ; } sort ( milk
, milk
+ num
) ; cout
<< milk
[ num
/ 2 ] ; delete [ ] milk
; milk
= NULL ; return 0 ;
}
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std
;
int main ( )
{ int N
, num
, i
= 0 ; cin
>> N
; vector
< int > maxheap
, minheap
; while ( i
++ < N
&& cin
>> num
) { if ( maxheap
. empty ( ) ) { maxheap
. push_back ( num
) ; continue ; } if ( ! maxheap
. empty ( ) && num
<= maxheap
[ 0 ] ) { maxheap
. push_back ( num
) ; push_heap ( maxheap
. begin ( ) , maxheap
. end ( ) ) ; } else if ( ! maxheap
. empty ( ) && num
> maxheap
[ 0 ] ) { minheap
. push_back ( num
) ; push_heap ( minheap
. begin ( ) , minheap
. end ( ) , greater
< int > ( ) ) ; } if ( maxheap
. size ( ) > minheap
. size ( ) && maxheap
. size ( ) - minheap
. size ( ) > 1 ) { minheap
. push_back ( maxheap
[ 0 ] ) ; push_heap ( minheap
. begin ( ) , minheap
. end ( ) , greater
< int > ( ) ) ; pop_heap ( maxheap
. begin ( ) , maxheap
. end ( ) ) ; maxheap
. pop_back ( ) ; } else if ( maxheap
. size ( ) < minheap
. size ( ) ) { maxheap
. push_back ( minheap
[ 0 ] ) ; push_heap ( maxheap
. begin ( ) , maxheap
. end ( ) ) ; pop_heap ( minheap
. begin ( ) , minheap
. end ( ) , greater
< int > ( ) ) ; minheap
. pop_back ( ) ; } } if ( maxheap
. size ( ) ) cout
<< maxheap
[ 0 ] << endl
; return 0 ;
}
總結
以上是生活随笔 為你收集整理的POJ1003/1004/1005/1207/3299/2159/1083/3094/2388解题(刷一波水题) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。