2015年2月18日 星期三

P2A寒假作業(第1章)

部份例題及章末習題之 c++ 版 解題參考

// 參考 p1.2 {了解整數除法及小數除法}
#include <iostream>
#include <iomanip>  // setprecision
using namespace std;
int main( )
{
printf("%.1lf\n", 7/4);  // 整數 1 無法轉成浮點輸出
printf("%.1lf , %.1lf , %1.2lf , %1.3lf\n", 7/4 , 7.0/4 ,7.0/4 , 7.0/4 );  // 全部輸出為 0.0..或?
printf("%.1lf , %.1lf , %1.2lf , %1.3lf\n", 7.0/4 , 7.0/4 ,7.0/4 , 7.0/4 ); // 正確的輸出
   cout << 7/4 <<"," << 7.0/4 <<"," << 7.0/4 <<"," << double(7)/4<< endl;
   cout << fixed << setprecision(1);
   cout << 7/4 <<"," << 7.0/4 <<"," << 7.0/4 <<"," << double(7)/4<< endl;
   cout << fixed << setprecision(3);
   cout << 7/4 <<"," << 7.0/4 <<"," << 7.0/4 <<"," << double(7)/4<< endl;
   int a=7,b=4, c=a/b;
   double d=a/b , da=a , e=da/b;
   cout << fixed << setprecision(1);
   cout << c <<"," << d <<"," << e <<"," << a*10/b/10.0<< endl;
   return 0;
}
// fixed 顯示時固定小數位數 、 setprecision( x ) 設定小數位數  {解釋不完整,可另查手冊 }

// 參考 p1.6 {例題1-1 圓柱體的表面積}
#include <iostream>
#include <cmath>    // sqrt 大部份c的include檔在cpp時前加c後去.h
#include <iomanip>  // setprecision
using namespace std;
int main( )
{
   double pi = 4.0*atan(1.0) ;
   double r,h,s1,s2,s;
   cin >> r >> h;        // scanf需指定型別,cin會依變數自動判別
   s1 = pi*r*r;
   s2 = 2*pi*r*h;
   s = s1*2 + s2;
   printf("面積 = %.3lf\n",s);
   cout << fixed << setprecision(3) << "面積 = " << s << endl;
    return 0;
}
// fixed 顯示時固定小數位數 、 setprecision( x ) 設定小數位數  {解釋不完整,可另查手冊 }

// 參考 p1.9 {例題1-2 三位數反轉 + 無前導0}
#include <iostream>
using namespace std;
int main( )
{
int n;
cin >> n;
cout << n%10 << n/10%10 << n/100 << endl;
// 以上輸出 123、520、600 、 708 分別顯示 321 、 025 、 006 、 807
// 若反轉後的前導百位數及十位數為0不顯示 ,則以上輸出為 321 、 25 、 6 、 807
int a=n%10, b=n/10%10;
if(a!=0) cout << a;
if( a!=0 || b!=0 ) cout << b;
cout << n/100 << endl;
   return 0;
}

// 參考 p1.18 {實驗 A1 長長整}
#include <iostream>
using namespace std;
int main( )
{
int n=11111;
long long nn;
cout << n << "x" << n << " = " << n*n << endl;
cin >> n;
cout << n << "x" << n << " = " << n*n << endl;  // 若6個1相乘會顯示 負數
nn = n*n;
cout << n << "x" << n << " = " << nn << endl;   // 還是負數
nn = n;
cout << nn << "x" << nn << " = " << nn*nn << endl; // 這個才正確
   return 0;
}

// 參考 p1.20 {習題1-1 平均數 average}
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
int a,b,c;
cin >> a >> b >> c;
// 必需假設 -2147483448 < a+b+c < 2147483647 否則以下前 3 個輸出會有誤
// 請以 1234567890 + 1234567891 + 123456791 試試看
cout << fixed << setprecision(3) << (a+b+c)/3.0 << endl;
double s1=a+b+c;
cout << fixed << setprecision(3) << s1/3.0 << endl;
long long s2=a+b+c;
cout << fixed << setprecision(3) << s2/3.0 << endl;
s1=a;
s1=s1+b+c;
cout << fixed << setprecision(3) << s1/3.0 << endl;
s2=a;
s2=s2+b+c;
cout << fixed << setprecision(3) << s2/3.0 << endl;
   return 0;
}

// 參考 p1.20 {習題1-2 溫度 temperature}
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
double f , c;  // 沒有規定輸入是整數, 但若 f 宣告為整數,計算式需注意型別
cin >> f;
c = 5*(f-32)/9;
cout << fixed << setprecision(3) << c << endl;
int fi=(int) f;  // 當 f 轉為整數,請輸入 79 試試
        c =  5*(fi-32)/9;
cout << fixed << setprecision(3) << c << endl;
        c =  5.0*(fi-32)/9 ;
cout << fixed << setprecision(3) << c << endl;
   return 0;
}

// 參考 p1.20 {習題1-3 連續和 sum}
#include <iostream>
using namespace std;
int main( )
{
int n;
cin >> n;
unsigned  sum = n*(n+1)/2;  // 若 n <= 65535 可,否則 sum 需宣告為 long long 
long long sum2 =  (long long )n *(n+1)/2;
cout << sum << endl;
cout << sum2 << endl;
   return 0;
}

// 參考 p1.21 {習題1-4 正弦和餘弦 sin cos}
#include <iostream>
#include <cmath>

using namespace std;
int main( )
{
   const double pi = 4.0 * atan(1.0);
int n;
cin >> n;
cout << sin(n*pi/180) << ","   << cos(n*pi/180)<< endl;  // float 有效位數 7 位
   return 0;
}

// 參考 p1.21 {習題1-5 距離 distance}
#include <iostream>
#include <cmath>  // sqrt(  ) 開平方非負根

using namespace std;
int main( )
{
   const double pi = 4.0 * atan(1.0);
double x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << sqrt(x1*x1 + y1*y1) << endl;
   return 0;
}

// 參考 p1.21 {習題1-6 偶數 not odd}
#include <iostream>
using namespace std;
int main( )
{
   int n;
cin >> n;
cout << (n-n/2*2 ==0 ? "yes" : "no") << endl;
cout << (n%2 ==0 ? "yes" : "no") << endl;
       // ... 也可以使用 if
   return 0;
}

// 參考 p1.21 {習題1-7 打折 discount}
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
   const int price = 95;
   const int dc_pri = 300;
   int n;
cin >> n;
cout << fixed << setprecision(2) ;
cout << (n*price >= dc_pri ? 0.85 : 1.0)*n*price << endl;
   return 0;
}

// 參考 p1.21 {習題1-8 絕對值 abs}
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
   double f;   // float 的位數較小
cin >> f;
cout << fixed << setprecision(2) ;  // 預設依第3位四捨五入進位成2位
cout << (f<0?-1:1)*f << endl;
   return 0;
}
// 1.2349999999999999  => 1.23  有效位數的誤差?
// 1.23499999999999999 => 1.24 目前先不考慮,以後再說

// 參考 p1.21 {習題1-9 三角形 triangle}
#include <iostream>
using namespace std;
int main( )
{
   int a,b,c;
cin >> a >> b >> c;
// 找出最大數 放在 c
int m=c;
if(a>c) m=a , a=c, c=m;
if(b>c) m=b , b=c, b=m;
if( c>=a+b) cout << "not a triangle" << endl;
   else if(  c*c == a*a + b*b ) cout << "yes" << endl;
   else cout << "no" << endl;
   return 0;
}
// 5 4 3 yea
// 6 4 3 no
// 7 4 3 not a

// 參考 p1.21 {習題1-10 年份 year leap}
#include <iostream>
using namespace std;
int main( )
{
   int y;
cin >> y;

   if( y%400==0 || ( y%100!=0 && y%4==0 ) ) cout << "yes" << endl;
   else cout << "no" << endl;
   return 0;
}
// 1992 yea
// 1994 no
// 2000 yes
// 2100 no

0 意見:

張貼留言