2015年2月22日 星期日

P2A寒假作業(第2章)

章末習題之 c++ 版 解題參考,不開檔只用 cin+cout

// 參考 p2.22 {習題2-1 位數 digit}  
不用檔案,一律 cin+cout
#include <iostream>
using namespace std;
int main( )
{
int n , d=1;
cin >> n;    // <= 10^9 的正整數
while( (n /= 10) != 0 ) ++d;
cout << d << endl;
   return 0;
}

// 參考 p2.22 {習題2-2 水仙花數 daffodil}

#include <iostream>
using namespace std;
int main( )
{
int n , a,b,c, d=0;
for(n=100; n<=999; ++n)  // 100 ~ 999 之間
{
a=n/100;  b=n/10%10;  c=n%10;
d = a*a*a + b*b*b + c*c*c;
if( n == d ) cout << n << endl;
}
   return 0;
}

// 參考 p2.22 {習題2-3 韓信點兵hanxin}

#include <iostream>
using namespace std;

int main( )
{
int n , a,b,c;
cin >> a >> b >> c;
for(n=10; n<=100; ++n)  // 10 ~ 100 之間
{
if ( n%3==a && n%5==b && n%7==c ) break;
}
if(n<=100) cout << n << endl;
else cout << "No answer" << endl;
   return 0;
}

// 參考 p2.23 {習題2-4 倒三角形 triangle}

#include <iostream>
using namespace std;
int main( )
{
int n , i,j;
cin >> n;  // n<=20 的正整數
for(i=n; i>0; --i) 
{
for(j=0; j<n-i; ++j)  cout << ' ';   // 左邊空幾格
for(j=i*2-1; j>0; --j)  cout << '#'; // 第 i 層印 2i-1個 #
cout << endl;
}

   return 0;
}


// 參考 p2.23 {習題2-5 統計 stat} 

#include <iostream>
using namespace std;
int main( )
{
int n , m , i;
cin >> n;
int a[n];
for(i=0; i<n; ++i) cin >> a[i];   // a[0] ~ a[n-1] 共 n 個 
cin >> m;
int cnt=0;  // 計算 < m 的個數
for(i=0; i<n; ++i)
// { 只有一個敘述時可以不使用段落的大括號
 if( a[i] < m ) ++cnt;
// }
cout << cnt << endl;
   return 0;
}
/* 資料1 n=10 , m=15 小於 m 的有 7 5 6 8 不含15 共 4 個
10
35 16 23 7 5 69 6 15 46 8
15
  資料2 n=15 , m=30 小於 m 的有 16 23 5 6 15 18 共 6 個
15
35 99 16 75 23 47 61 5 69 82 6 15 38 46 18
30

*/

// 參考 p2.23 {習題2-6 調和級數 harmony}  
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
int n  , i;
cin >> n;
double h=1.0 , u;
for(i=2; i<n; ++i)
u = 1.0/i;
h += u;
}
cout <<fixed << setprecision(3) << h << endl;
   return 0;
}

// 參考 p2.23 {習題2-7 近似計算 approximation }

#include <iostream>
#include <iomanip>
#define MinV 1.0e-6 // 最後一項的值
using namespace std;
int main( )
{
int  i ;
bool s=true;
double h=1.0 , u=1.0;
for(i=3;  u >= MinV; i+=2 , s=!s)
u = 1.0/i;
if(s) h-=u; else h+=u;
}
// cout <<fixed << setprecision(x位小數) << h << endl;
cout << h << endl;
   return 0;
}

// 參考 p2.23 {習題2-8 子序列的和 subsequence } 
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
long long i,m,n;   // m,n < 10^6 , m^2、n^2 < 10^12
double s=0.0 , u;
cin >> n >> m;
for(i=n;  i<=m ; ++i)
u = 1.0/(i*i);
s += u;
}
cout <<fixed << setprecision(5) << s << endl;
   return 0;
}

// 參考 p2.24 {習題2-9 分數化小數 decimal }  
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
int i,a,b,c;
cin >> a >> b >> c;
cout << a/b << ".";
a = a%b*10;
for(i=1;  i<=c ; ++i)
cout << a/b ;
a = a%b*10;
}
   return 0;
}

// 參考 p2.24 {習題2-10 排列 permutation }  使用 next_permutation
#include <iostream>
#include <algorithm>  // 使用 next_permutation
using namespace std;
int main( )
{
int   abc , def , ghi;
int d[] = {1,2,3,4,5,6,7,8,9};
  do {
if(d[3]<d[0]+d[0] || d[6]<d[0]+d[3]) continue;
abc = d[0]*100+d[1]*10+d[2];
if(abc>329) break;
def = d[3]*100+d[4]*10+d[5];
if( abc*2 != def ) continue;
ghi = d[6]*100+d[7]*10+d[8];
if(ghi == abc + def ) 
cout << abc << ":" << def << ":" << ghi << endl;
  } while ( next_permutation(d,d+9) );
   return 0;
}

// 參考 p2.24 {習題2-10 排列 permutation }  使用 bool 陣列做註記
#include <iostream>
#include <cstring> // memset
using namespace std;
int main( )
{
int  i,abc ;
bool mk[10];  // 某一數字被使用過則設為 true ,重複則不合規定
for(i=123;  i<=329 ; ++i)  // 最小三位數不重複123, 最大的987/3 = 329
  abc = i;
memset(mk,0,sizeof(mk));  // 將 mk清成 0:false 或使用 for(int i=1; i<=9; ++i) mk[i]=false;
mk[0]=true;  // 不可使用 0 
mk[abc/100] = true;
if( mk[abc/10%10] ) continue;  else mk[abc/10%10]=true;
if( mk[abc%10] ) continue;  else mk[abc%10]=true;
abc*=2;  // 判斷 abc*2的數字是否用過
if(mk[abc/100])  continue;  else mk[abc/100] = true;
if( mk[abc/10%10] ) continue;  else mk[abc/10%10]=true;
if( mk[abc%10] ) continue;  else mk[abc%10]=true;
abc = abc/2*3;  // 判斷 abc*3的數字是否用過
if(mk[abc/100])  continue;  else mk[abc/100] = true;
if( mk[abc/10%10] ) continue;  else mk[abc/10%10]=true;
if( mk[abc%10] ) continue;
cout  << abc / 3 << ":" << abc/3*2 <<  ":" << abc << endl;
}
   return 0;
}








0 意見:

張貼留言