2015年2月8日 星期日

P2A練習02/05

P2A 2015/02/05練習:讀入n個數,問數字相同的個數最多幾個?

以下為第(2)種方法的程式碼,另附測試檔 a.txt b.txt c.txt 
a共110個數字{有5個5} 、  b共10000個數字{有6個100}、 c共10000個數字{有8個98765432}

/*
p2a 02/05 練習:若有 N 個數 A[0],A[1],.....A[N-1],求重複最多的個數
例 有10個數 1,2,3,4,5,2,3,4,5,2,最多為3個 {註:2有3個}
本題(1) 若A[0]~A[N-1]的範圍不大<10^6的話{假設為M},可用空間換取時間,直接宣告C[M]來計數即可,
  (2) 但若範圍太大{0~10^9 },而N不太大<10^4的話,可將A陣列排序後,以下列程式碼來計算最多個數
(3) 也可以使用類似 b05的解法
*/
#include <iostream>
#include <algorithm>  // 使用 sort 需用
using namespace std;
int main()
{
  int i,n, a[10000];
  cin >> n ;
  for(i=0; i<n; ++i)  cin >> a[i];    // 讀入 a 陣列
  sort(a,a+n);                       // 將 a 陣列由小至大排序
// 計算連續相同的數字
  int ans = 1;
  for(i=1;i<n-1;++i)
  {
if( a[i] == a[i-ans] ) ++ ans;
// 當ans為1 若a[i]與前a[i-1]相同,則 ans+1就是長度為2
// 當ans為2 若a[i]與前a[i-2]相同,則 ans+1就是長度為3
// 當ans為 k 若a[i]與前a[i-k]相同,則 ans+1就是長度為 K+1
      //  若沒有比 k 長的相同數字, ans 不會再加
  }
cout << "最多相同數共 " << ans << "個\n";
  return 0;
}
/*
範例輸入
10
1 2 3 4 5 2 3 4 5 2
範例輸出
3
*/

2015年1月16日 星期五

P1選 C 另二個範例

gcd.cpp 求最大公因數
/* 輸入 {a,b} > 0  計算 a,b 的最大公因數 */
#include <iostream>
using namespace std;
int main()
{
  int a,b;
  while ( cin >> a >> b)
  {
cout << a << "," << b << " 的 gcd ";
int c=a%b;
  while( c )
  {
a=b;
b=c;
c=a%b;
}
cout << b << endl;
}
  return 0;
}
/*
執行範例
D:\cpp>gcd
100 55
100,55 的 gcd 5
1965 1155
1965,1155 的 gcd 15
^Z
*/

a的b次方÷1000取餘數
/* 輸入 1<{a,b}<1000 計算 a^b(a的b次方) 取最後三位數 */
#include <iostream>
using namespace std;
int main()
{
int a,b;
while( cin >> a >> b)
{
int  p=1;
for(int i=1; i<=b; ++i)
{
p*=a;   //乘 b 次
p %= 1000; // 每次皆除 1000 取餘數
}
cout << a << "^" << b << " 後3碼 = " << p << endl;
}
  return 0;
}
/*
執行範例
D:\cpp>apb
123 456
123^456 後3碼 = 561
12 23
12^23 後3碼 = 128
987 654
987^654 後3碼 = 689
^Z
*/

P1 選修C 的三個Prime程式

除了以下這三個之外還有兩個範例,明日(1/17)會加入
第一個prime-1.cpp 輸入一個數 n 判斷是否為質數
#include <iostream>
using namespace std;
int main()
{
  int n , i ;
 
  while( cin >> n )
  {
    if(n==1) { cout << "No\n"; continue; }
    for(i=2; i<n; ++i)
      if( n % i == 0 )  break;

    if( i <n ) cout << "No";
    else       cout << "Yes";
    cout << endl;
  }        
  system("pause");
  return 0;
}

第二個prime-2.cpp 輸入一個數 n 判斷是否為質數
#include <iostream>
#include <cmath>  // sqrt
using namespace std;
bool isp( int n )   // 函數方式判斷是否質數
{
  int q = int(sqrt(n));   // 只須檢查至平方根
  int i;  
  if(n==1)  return false;
  for(i=2; i<=q; ++i)
      if( n % i == 0 )  return false;
  return true;  
}
   
int main()
{
  int n ;
  while( cin >> n )
  {
    if( isp(n) ) cout << "Yes\n";
    else       cout << "No\n";
  }        
  system("pause");
  return 0;

}

第三個prime-3.cpp 輸入一個數 n  列出1~n的質數,每10個換列
#include <iostream>
#include <cmath>  // sqrt
using namespace std;
bool isp( int n )
{
  int q = int(sqrt(n));
  int i;  
  if(n==1)  return false;
  for(i=2; i<=q; ++i)
      if( n % i == 0 )  return false;  
  return true;  
}    
int main()
{
  int i,n,cnt ;
  while( cin >> n )
  {
     cnt =0;
     for(i=2; i<=n; ++i)
       if( isp(i) )
       {
          cout << i << " ";
          if((++cnt)%10==0) cout << endl;
       }
  cout << endl;                
  }        
  system("pause");
  return 0;
}


2014年12月5日 星期五

Z2X56-段2模C參考程式碼

Public Class Form1
    Private Sub CC3_DoubleClick(…) Handles CC3.DoubleClick
        ' 1 按2下
        CC3.Items.Add(CC2.Text)
        CC2.Focus()
        CC2.SelectAll()
    End Sub
    Private Sub Button2_Click(…) Handles Button2.Click
        ' 2 →
        Dim i As Integer
        For i = 0 To CC3.SelectedItems.Count - 1
            ListBox2.Items.Add(CC3.SelectedItems(i))
        Next
        For i = CC3.SelectedIndices.Count - 1 To 0 Step -1
            CC3.Items.RemoveAt(CC3.SelectedIndices(i))
        Next
    End Sub
    Private Sub Button3_Click(…) Handles Button3.Click
        ' 3 →
        Dim i As Integer
        For i = 0 To CC3.SelectedItems.Count - 1
            ListBox3.Items.Add(CC3.SelectedItems(i))
        Next
        For i = CC3.SelectedIndices.Count - 1 To 0 Step -1
            CC3.Items.RemoveAt(CC3.SelectedIndices(i))
        Next
    End Sub
    Private Sub Button4_Click(…) Handles Button4.Click
        ' 4 →
        Dim i As Integer
        For i = 0 To CC3.SelectedItems.Count - 1
            ListBox4.Items.Add(CC3.SelectedItems(i))
        Next
        For i = CC3.SelectedIndices.Count - 1 To 0 Step -1
            CC3.Items.RemoveAt(CC3.SelectedIndices(i))
        Next
    End Sub

    Private Sub Button5_Click(…) Handles Button5.Click
        ' 5 ↓
        For i = ListBox2.SelectedIndices.Count - 1 To 0 Step -1
            ListBox2.Items.RemoveAt(ListBox2.SelectedIndices(i))
        Next
    End Sub

    Private Sub Button6_Click(…) Handles Button6.Click
        '6 ←
        Dim i As Integer
        For i = 0 To ListBox3.SelectedItems.Count - 1
            CC3.Items.Add(ListBox3.SelectedItems(i))
        Next
        For i = ListBox3.SelectedIndices.Count - 1 To 0 Step -1
            ListBox3.Items.RemoveAt(ListBox3.SelectedIndices(i))
        Next
    End Sub

    Private Sub Button7_Click(…) Handles Button7.Click
        ' 7 ↑
        Dim i As Integer
        For i = 0 To ListBox4.SelectedItems.Count - 1
            CC2.Items.Add(ListBox4.SelectedItems(i))
        Next
        For i = ListBox4.SelectedIndices.Count - 1 To 0 Step -1
            ListBox4.Items.RemoveAt(ListBox4.SelectedIndices(i))
        Next
    End Sub

    Private Sub Button8_Click(…) Handles Button8.Click
        ' 8 ↑
        Dim k = CC3.SelectedIndex
        Dim k1 = k - 1
        If k = 0 Then k1 = CC3.Items.Count - 1
        Dim a = CC3.Text
        If (k < 0) Then Exit Sub
        CC3.Items.RemoveAt(k)
        CC3.Items.Insert(k1, a)
        CC3.SelectedIndex = k1
    End Sub

    Private Sub Button9_Click(…) Handles Button9.Click
        ' 9.修改 以 CC2的text 來改變 CC1.text
        CC1.Text = CC2.Text
    End Sub

    Private Sub Button10_Click(…) Handles Button10.Click
        ' 10 ↓
        Dim n = CC3.SelectedIndex
        If n >= CC3.Items.Count - 1 Then Exit Sub
        CC3.Items.Insert(n + 2, CC3.Items(n))
        CC3.Items.RemoveAt(n)
        CC3.SelectedIndex = n + 1
    End Sub

    Private Sub Button11_Click(…) Handles Button11.Click
        ' 11 顯示字型
        If ListBox2.SelectedIndex = -1 Then ListBox2.SelectedItem = CC1.Font.Name
        If ListBox3.SelectedIndex = -1 Then ListBox3.SelectedIndex = 0
        If ListBox4.SelectedIndex = -1 Then ListBox4.SelectedItem = "16"
        Dim 字型名稱 As String = ListBox2.SelectedItem
        Dim 字型樣式 As FontStyle
        Dim 字型大小 As Integer = Val(ListBox4.SelectedItem)
        Select Case ListBox3.SelectedItem
            Case "標準"
                字型樣式 = FontStyle.Regular
            Case "粗體"
                字型樣式 = FontStyle.Bold
            Case "斜體"
                字型樣式 = FontStyle.Italic
            Case "粗斜體"
                字型樣式 = FontStyle.Bold Or FontStyle.Italic
            Case "底線"
                字型樣式 = FontStyle.Underline
            Case "刪除線"
                字型樣式 = FontStyle.Strikeout
        End Select
        CC1.Font = New Font(字型名稱, 字型大小, 字型樣式)
    End Sub

    Private Sub Button12_Click(…) Handles Button12.Click
        ' 12 End
        MsgBox("56-模考2C卷")
        End
    End Sub
End Class

2014年11月28日 星期五

2014年11月26日 星期三

P1選CPP

範例試題檔

four.cpp


/*
 four四個運算
 輸入一串字: a op b
a,b是整數、   -1000<a,b<1000
op 是右列四個運算子  +  -  *  ^
其中 ^ 是a加b後的2次方 (a+b)乘上(a+b)

 */  
#include <iostream>
using namespace std;
int main()
{
    int a , b;
    char op;   // 一個字元 A~Z,a~z,0~9,符號,中文不行
  while( cin >> a)
  {
    cin  >> op >> b;
    switch (op)
    {
      case '+': cout << a+b <<endl;  break;
      case '-': cout << a-b <<endl;  break;
      case '*': cout << a*b <<endl;  break;
      case '^': cout << (a+b)*(a+b) <<endl;  break;
      default : cout <<"???" <<endl;  break;        
    }
}
    return 0;
}