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

Related Posts:

  • dfs-1(vb版參考)程式碼:     Dim a(100, 100) As Integer     Dim m As Integer, n As Integer     Private Sub Form1_Load( ...  ) Handles M… Read More
  • C129 油田數(VB版)     Dim n As Integer, m As Integer          'n列 x m格     Dim a(100, 100) As Integer      … Read More
  • a982迷宮問題(VB版)   Dim n As Integer                     ' n x n 方形     Dim a(100, 100) As Intege… Read More
  • a753最大面積(C++版)// a753 最大面積 最多 AxB , 5<=A,B<=30 // 矩形一個 AXB ,查詢有 n 個, 每個高度為h 的最大面積, // 如範例中 1的有7個、2的有5個、3的有6個、4的為0個 {1個的印0} #include <iostream> using nam… Read More
  • a982迷宮問題(C++版)/* a982 迷宮問題 */ #include <iostream> using namespace std; const int MaxN = 100; const int MaxQ = 200; int main(void) { int n,i,j; int x,y,h; … Read More

0 意見:

張貼留言