OpenJudge 1.5.27: Summation

description

Known: Sn = 1 + 1/2 + 1/3 + ... + 1 / n. Clearly for any integer K, when n is large enough, Sn is larger than K.

Are now given an integer K (1 <= k <= 15), requires a minimum of n-calculated; such that Sn> K.

Enter an integer K. Output an integer n.

Wrong answers and analysis:

#include <cstdio>
 the using namespace STD ; int main () { int I , K ; a float n- = 0.0 , Sn = 0 ; // here noted that the data range should not use double type Scanf ( "% D" , & K ); the while (Sn <= K ) {n- + = . 1 ; Sn + = 1.0 / n- ; } the printf ( "% D" , ( int ) n- ); return 0 ; } attached: 
Types of Place range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0-4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int 2 bytes 0 to 65,535
signed short int 2 bytes -32768 to 32767
long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int 8 bytes 0 to 18,446,744,073,709,551,615
float 4 bytes +/- 3.4e +/- 38 (~ 7 digits)
double 8 bytes +/- 1.7e +/- 308 (~ 15 digits)
long double 16 bytes +/- 1.7e +/- 308 (~ 15 digits)
wchar_t 2 or 4 bytes A wide character
 
Correct answer:
#include<cstdio>
using namespace std;
int main() { int i,k; double n=0.0,sn=0; scanf("%d",&k); while(sn<=k) { n+=1; sn+=1.0/n; } printf("%d",(int)n); return 0; }
 

Guess you like

Origin www.cnblogs.com/ziyuan122625/p/11931090.html