2015年1月16日 星期五

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

2014年10月30日 星期四