---
# System prepended metadata

title: 108題目(B卷)
tags: [一社一服務]

---

---
tags: 一社一服務
---
# 108題目(B卷)
[回目錄](https://hackmd.io/@PeiYun/CppTutorial)
> 為了幫(強)助(迫)各位思考，以下僅提供<font color=#f00>部分程式碼</font>，有任何問題歡迎私訊[資訊社IG](https://www.instagram.com/dzif_22nd_/?hl=zh-tw)



## 最大公因數及最小公倍數
```cpp=
 int a,b,c,d,e,x,y,z,f;
    
	f=a;
	b=x*y;

while(x%y!=0)  //x除數 y被除數
	{
	    z=x%y;	//z餘數
	    x=y;	
	    y=z;	
    }
        d=b/y;
        e=d*a;
        while(y%f!=0)
 	    {
		c=y%f;
		y=f;
		f=c;
	    }
		 
 	while(d%a!=0)
	    {
                c=d%a;
                d=a;
                a=c;
	    }
  cout<<f<<" "<<e/a<<"\n";
```
## 兩正整數間質數，並求其總和
```cpp=
int x1,x2,i,j,k,l=0;
for(i=x1 ;i<=x2  ;++i )
 {
  for( j=2; j<=i ;++j )
  {
   if(i%j==0 )
   {
    break;
   }
  }
  if(i==j )
  {
   cout<<i<<" ";
   k++;
   l+=i;
  }  
 }
 cout<<k<<" "<<l;

```
## 隨機亂數兩十位正整數N跟M，並計算總合
```cpp=
#include <ctime>
```
```cpp=
int x, y, a, c;
srand(time(NULL));
 x=rand()%(99-10)+10;//大數減小數加小數
 y=rand()%(99-10)+10;
 cout<<x<<" + "<<y<<" = \n";
```
```cpp=
 c=x+y;
 cin>>a;
 if(a==c)
 {
 cout<<"正確\n";
 }
 else
 {
 cout<<"錯誤\n";
 }
```
## 級數
```cpp=
#include <cmath>
```
```cpp=
 while(true)
 {
  int n;
  float i,a,ans=0;
  cout<<"N=";
  cin>>n;
  if(n%2==1)
  {
   for(float i=1;i<=n;i+=2)
    {
     a=1/i;
     ans=ans+a;
    }
  cout<<ans<<"\n";
  }
  else
  {
   for(float i=2;i<=n;i+=2)
   {
    ans+=i;
   }
  cout<<sqrt(ans)<<"\n";
  }
```


