計概開大篇
最小公倍數
#include
using namespace std;
int main()
{
int a,b,c,d,e,f;
cout<<"任意輸入一個正整數";
cin>>a;
cout<<"再輸入一個正整數";
cin>>b;
c=a;
do
{
d=a%c;
e=b%c;
c--;
}while(d!=0 || e!=0);
f=(a*b)/(c+1);
cout<<"最小公倍數為"<
return 0;
}
最大公因數
#include
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"任意輸入一個正整數";
cin>>a;
cout<<"再輸入一個正整數";
cin>>b;
c=a;
do
{
d=a%c;
e=b%c;
c--;
}while(d!=0 || e!=0);
cout<<"最大公因數為"<
return 0;
}
九九乘法表
#include
#include
using namespace std;
int main()
{int i,j,k,l;
i=1;j=1;
while(i<10)
{cout<<
i++;}
cout<
while(j<10)
{cout<
k=1;
while(k<10)
{l=k*j;
if(l<10)
{cout<<
else
{cout<<
k++;
}
cout<
j++;}
拍(隨機)
#include
#include
#include
using namespace std;
int main()
{
double x,y;double a,b;
a=0;
b=0;
srand(time(0));
do{
x=rand()*2/32767.0-1;
y=rand()*2/32767.0-1;
if (x*x+y*y<=1)
{a=a+1;
b=b+1;}
else
{b=b+1;}
}while (b<10000000);
cout<system("pause");
return 0; }
費氏樹列(可以知道第n個)
#include
using namespace std;
int f(int n)
{if(n==1||n==2)
{return 1;}
return f(n-1)+f(n-2);}
int main()
{int a,b,c;
a=1;b=1;
cout<<"費氏樹列第幾個";
cin>>c;
cout<
return 0;}
費氏樹列(2)(可以知道前n個)
#include
#include
using namespace std;
int f(int n)
{if(n==1||n==2)
{return 1;}
return f(n-1)+f(n-2);}
int main()
{int a,b,c;
a=1;b=1;c=1;
while(c<21)
{cout<<
if(c%5==0)
{cout<
c++;}
return 0;}
猜數字
#include
#include
#include
using namespace std;
int main()
{int x;
int l=1,r=10000;
srand(time(0));
x=rand()%10000;
do{
int g;
cout<<"目前範圍"<<<"~~"<<<"請猜";
cin>>g;
if(g>r||g
{continue;}
if(g>x)
{r=g;}
else if(g
{l=g;}
else
{break;}
}while (r-l>0);
cout<<"you lose";
return 0;}
四則運算
#include
using namespace std;
#define add(x,y) ((x)+(y))
#define minus(x,y) ((x)-(y))
#define multply(x,y) ((x)*(y))
#define divide(x,y) ((x)/(y))
int main()
{double a,b;
char c;
cout<<"請輸入算式";
cin>>a>>c>>b;
switch(c)
{case'+':cout<<<"+"<<<"="<
case'-':cout<<<"-"<<<"="<
case'*':cout<<<"*"<<<"="<
case'/':cout<<<"/"<<<"="<
}
return 0;}
十轉二完美版
#include
#include
using namespace std;
int main(){
int total=0,sum=1,x,a;
cout << "Please enter a Decimal number:";
cin >> x;
a=x;
do{
total+=(x%2)*sum;
x/=2;
sum*=10;
}while(x!=0);
cout << "The binary of " << a << " is " <<
total;
}
質數(1)利用(sqrt檢驗)
#include
#include
#include
using namespace std;
int main()
{int f,count=0;
int i,j;
for (i=2;i<230;++i){
f=1;
for(j=2;j<=sqrt(double(i));j++){
if(!(i%j))
{
f=0;
break;
}
}
if(f){
if(i<10)
{cout<<
if(i<100&&i>10)
{cout<<
if(i<1000&&i>100)
{cout<<
count=count+1;
if(count%10=0)
{cout<
}
}
system("pause");
return 0;
}
質數(2)(檢驗全部)
#include
#include
using namespace std;
int main()
{int a,b,c=0;
a=2;
while(c<50)
{b=2;
while(b<=a)
{if(a%b==0)
{break;}
b++;
}
if(a==b)
{cout<<
c=c+1;
if(c%10==0)
{cout<
a++;
}
return 0;
} #include #include using namespace std; int main() { int a;
cout<<"Enter the number of lines: "; cin>>a; for(int
i=1;i<=a;i++) { for(int j=1;j<=a;j++) { if(j<=a-i)
cout<<" "; else { for(int k=1;k<=2*i-1;k++) { if(k
school(2)
