2015年4月28日 星期二

國中程式社作業

最近三週將陸續講解a001~a030,請自行至台中女中程式解題系統練習

進度超前的同學可以到zerojudge建帳號練習以下的題目:
題目
a001. 哈囉
a002. 簡易加法
a003. 兩光法師占卜術
a004. 文文的求婚
a005. Eva 的回家作業
a053. Sagit's 計分程式
d049. 中華民國萬歲!
d050. 妳那裡現在幾點了?
d051. 糟糕,我發燒了!
d053. 10970 - Big Chocolate
d058. BASIC 的 SGN 函數
d059. 數學函數練習
d060. 還要等多久啊?
d063. 0 與 1
d064. ㄑㄧˊ 數?
d065. 三人行必有我師
d066. 上學去吧!
d067. 文文的求婚--續集 (1 行版)
d068. 該減肥了!
d069. 文文的求婚--續集 (n 行版)
d070. 文文的求婚--續集 (0 尾版)
d071. 文文的求婚--續集 (EOF 版)
d072. 文文的求婚--續集 (Case 版)
d073. 分組報告
d126. 一、牧场围栏
d127. 二、牧场面积
d532. 文文的求婚 (三)
d559. 班號
d562. 山寨版磁力蜈蚣
d573. CRC騎士團
d579. 兩條線
d581. 三條線
d827. 買鉛筆
d984. 棄保效應

2015年4月22日 星期三

2015年4月13日 星期一

2015年2月26日 星期四

P2A寒假作業(第3章)

// 參考 p3.24 {習題3-1 分數統計(stat) 整數版位數 digit} 
/*
p3-24 習題 3-1-a 分數統計(stat) 整數版
輸入 若干個非負整數n{ 0<=n<=100 },直至n為-1才停,-1不列入統計,輸入的數字間以白空白隔開
統計出現次數最多的數字,若有多個由小至大列出,輸出的數字中間以1空格隔開
*/
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
   int i,n,m=0;
 int cnt[101];
 for(i=0;i<=100;++i) cnt[i]=0;  //清為0 或使用memset
 while(cin >> n)    // 另有EOF版(參考3-2)、給n值讀入n個數(參考3-3)
 {
  if( n == -1 ) break;
  ++cnt[n];        // 數字為 n 的次數 +1
  if( cnt[n] > m ) m = cnt[n];  // 若次數 > m(最多的次數) 改 m 值
    }
 // 輸入 cnt[n] 為m的值
 bool first = true;  // 第1個數字前不加空格
 for(i=0; i<=100; ++i)
  if( cnt[i] == m )
  {
   if( first ) first = false; else cout << " ";
   cout << i;
  }
 cout << endl;
  return 0;
}
/*
範例輸入
1 2 3 2 1 3 4 3 5 2 -1
輸出
2 3
*/

// p3-24 習題 3-2 單字的長度(word)
/*
p3-24 習題 3-2 單字的長度(word)
輸入若干個單字{只有大小寫英文字母},單字間以空白隔開,檔案結束才停
計算平均長度,輸出保留小數位數3位
*/
#include <iostream>
#include <cstring>  // s.size()
#include <iomanip>
using namespace std;
int main()
{
   string s;
 int n,i , tot=0 , cnt=0;
 while(cin >> s)    // EOF版,另有 -1版 (參考3-1a)、給n值讀入n個數(參考3-3)
 {
  ++ cnt;         // 單字的個數
  n = s.size();   // 字串 s 的長度
  tot += n;      // 長度的總和
   }
 cout <<fixed << setprecision(3) << 1.0*tot / cnt << endl;
   return 0;
}
/*
ABC DEFGH IJKLMNOP QRSTU VW XY Z
---------(輸出 26/7 )
3.714
*/

// p3-24 習題 3-3 乘積的末3位(product)
/*
p3-24 習題 3-3 乘積的末3位(product)
第1列輸入1個正整數 n ( 1< n < 1000 ) 第2列有 n 個字串,以空白隔開
每個字串含一個整數 k (k可能有負數,每個k的數字間或前後會插入一些大寫字母 , -99999 <= k <= 99999 )
計算這 n 個數字的乘積,輸出末3位數即可,但若乘積為負需先輸出 - 號
*/
#include <iostream>
#include <cstring>  // s.size()
#include <cctype>   // isdigit(x) 是否為數字、 is...
using namespace std;
int main()
{
   int n , i ,p = 1;
 string s;
 bool m=false;    // 負數?
 cin >> n;
 while( n-- )    // 給n值讀入n個數 , 另有EOF版(參考3-2)、 -1版 (參考3-1a)
 {
  cin >> s;
  int sn = s.size(); // 字串長度
  int k = 0;
  char c;
  for( i=0; i<sn; ++i)
  {
   c=s[i];   // 每個字元檢查
   if( c == '-' ) m = !m;  // * 負號
   else if( isdigit(c) ) k = k * 10 + (c-'0');
  }
  p *= k;
  p %= 1000;
   }
 cout << (m ? "-" : "" ) << p << endl;
   return 0;
}
/*
輸入範例:
5
AB-3C876 123D5E7 -F9G7H63 -463K4J0 M5N2P3Q1R
-----代表 -3876 * 12357 * -9763 * -46340 * 5231  => -113349824970861482640
-640
*/
// p3.24 習題 3-4 計算機 (calculator)

/* p3.24 習題 3-4 計算機 (calculator)
   第1列 輸入 n {2<=n<=50} ,然後讀入n列,每列n個可印出的字元
   將這 n * n 個字元逆時針旋轉 90 度後顯示
*/    
#include <iostream>
using namespace std;
int main()
{
   int a,b;
   char c;
   cin >> a >> c >> b;
   if( c == '+') cout << a+b <<endl;
   else if( c == '-') cout << a+b <<endl;
 else cout << a*b <<endl;
   return 0;
}
/*
1+1
2-   5
0 *1982
--------
2
-3
0
*/
//  p3.25 習題 3-5 旋轉 (rotate)
/* p3.25 習題 3-5 旋轉 (rotate)
   第1列 輸入 n {2<=n<=50} ,然後讀入n列,每列n個可印出的字元
   將這 n * n 個字元逆時針旋轉 90 度後顯示
*/    
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   int i,j,n;
   char a[50][52] , b[50][52];
   cin >> n;
   cin.getline(b[0],50); // 換行
   for(i=0; i<n; ++i) cin.getline(a[i],50);// fgets(a[i],50,cin);
   for(i=0; i<n;++i) b[i][n]='\0';
   int n1 = n-1;
   for(i=0;i<n;++i)
     for(j=0;j<n;++j)
       b[n1-j][i] = a[i][j];
   for(i=0; i<n;++i)
    puts(b[i]);
  return 0;
}
/*
5
123 5
ab cd
zxcvb
98765
! @ #
--------
5db5#
 cv6
3 c7@
2bx8
1az9!
*/
//  p3.25 習題 3-6 進制轉換-1 (base-1)  10 轉 b 進位
/* p3.25 習題 3-6 進制轉換-1 (base-1)
   輸入基數 b(2<=b<=10)和正整數n(十進位),輸出n的b進位表示
   n<2^31 , 若轉為二進位,可能長達 31位元
*/    
#include <iostream>
using namespace std;
int main()
{
   int b  , n ;
   string n2b;
   cin >> b >> n ;
   if( n == 0 ) cout << 0 << endl;
   else
   {
  while( n )
  {
   n2b = char(n%b +'0') + n2b;
   n /= b;
  }
 cout << n2b<< endl;
 }

   return 0;
}
/*
2 12
5 125
3 0
2 17
--------
1100
1000
0
10001
*/

//  p3.25 習題 3-7 進制轉換-2 (base-2) b進位轉十
/* p3.25 習題 3-7 進制轉換-2 (base-2)
   輸入基數 b(2<=b<=10)和正整數n( b 進位),輸出n的 十 進位表示
假設轉成十進位 < 2^31 ,n若二進位可能達 31 位元
*/  
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
  int b , nsize , b2t=0;
  string n;
cin >> b >> n;
nsize = n.size();
for (int i=0; i<nsize; ++i)
{
b2t = b2t*b + int( n[i]-'0' );
}
cout << b2t << endl;
  return 0;
}
/*
2 101101
5 123401
9 987654321
3 1201201201
2 1111111111111111111111111111111
--------
45
4851
429794605
34066
2147483647
*/

//  p3.25 習題 3-8 手機鍵盤 (keyboard)
/* p3.25 習題 3-8 手機鍵盤 (keyboard)
   一英文字(皆小寫)轉成以手機按鍵,例 pig 轉為 p1i3i1 {p按1下、g按3下、g按1下}
   abc、def、ghi、jkl、mno、pqrs、tuv、wxyz
*/    
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
  int tbl[26]={1,2,3,  1,2,3,  1,2,3,  1,2,3,  1,2,3,  1,2,3,4,  1,2,3,  1,2,3,4};
string eng;
cin >> eng;
int nsize = eng.size();
int i,j;
for (i=0; i<nsize; ++i)
{
char c = eng[i];
j = tbl [ int(c-'a') ];
if(j==1) cout <<c<<1;
else cout <<char(c-j+1)<<j;
}
cout  << endl;
  return 0;
}
/*
pig
blue
--------
p1g3g1
a2j3t2d2
*/

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;
}








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

國中 C++ 心想數字考題方式

(一) 卡片的製作:
  如果有6張卡,已完成5張卡,保證選完6張一定可以猜出心中所想的數字 (0~63之一)
  請完成第6張卡的內容

(二) 猜出心想數字:
  依所列出的6張卡,選出心中所想的數字 (0~63之一)在哪幾張卡內沒有
  請問心中所想的數字是?