練習問題/解答例/平方根/C++(better C)
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
***平方根 [#h1b5f416]
#include<iostream>
using namespace std;
int main( int argc, char** argv ){
double target = .0;
double result = .0;
double temp;
int i, j, k;
char *str1, *str2;
sscanf( argv[ 1 ], "%lf", &target );
sscanf( argv[ 2 ], "%d", &k );
str1 = ( char* )malloc( k );
str2 = ( char* )malloc( k );
if( target == .0 ) return -1;
for( i = 9; i >= 0; i -- )
if( i * i <= ( int )( target )) break;
sprintf( str1, "%d.", i );
for( j = 0; j < k; j ++ ){
for( i = 9; i >= 0; i -- ){
sprintf( str2, "%s%d", str1, i );
sscanf( str2, "%lf", &temp );
if(temp * temp <= target ) break;
}
strcpy( str1, str2 );
}
sscanf( str1, "%lf", &result );
sprintf( str2, "%%lf,%%.%dlf", k );
printf( str2, target, result );
return 0;
}
終了行:
***平方根 [#h1b5f416]
#include<iostream>
using namespace std;
int main( int argc, char** argv ){
double target = .0;
double result = .0;
double temp;
int i, j, k;
char *str1, *str2;
sscanf( argv[ 1 ], "%lf", &target );
sscanf( argv[ 2 ], "%d", &k );
str1 = ( char* )malloc( k );
str2 = ( char* )malloc( k );
if( target == .0 ) return -1;
for( i = 9; i >= 0; i -- )
if( i * i <= ( int )( target )) break;
sprintf( str1, "%d.", i );
for( j = 0; j < k; j ++ ){
for( i = 9; i >= 0; i -- ){
sprintf( str2, "%s%d", str1, i );
sscanf( str2, "%lf", &temp );
if(temp * temp <= target ) break;
}
strcpy( str1, str2 );
}
sscanf( str1, "%lf", &result );
sprintf( str2, "%%lf,%%.%dlf", k );
printf( str2, target, result );
return 0;
}
ページ名: