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
*/

0 意見:

張貼留言