Вы находитесь на странице: 1из 14

Bi tp nhm s 1

1)
Thut ton: Nhp s nguyn
M t: Nhp vo s nguyn dng n (n>0)
D liu vo: S nguyn n
D liu ra: Tng ca n s nguyn dng u tin.
Trnh by thut ton bng s khi:

Begin

Nhp n

i=1, S=0

n>=0

In ra S

i<=n
T
S=S+i
i=i+1

S dng thut ton bng gi ng


a)S dng cu trc lp for:
scanf(n);
if(n<=0)
else
{

printf(khong hop le!!!)

T
End

S=0;
for(i=1; i<=n; i++)
s=s+i;
}
b)S dng cu trc lp while:
scanf(n);
if(n<=0)

printf(khong hop le!!!)

else
{
s=0;
i=1;
while(i<=n)
{
s=s+i;
i++;
}}
c)S dng cu trc lp dowhile:
scanf(n);
if(n<=0)
else
{
s=0; i=1;
do
{
s=s+i;
i++;
}
while(i<=n)
}

printf(khong hop le!!!)

2)
Thut ton: Nhp s nguyn
M t: Nhp vo s nguyn dng n (n>0)
D liu vo: S nguyn n
D liu ra: n c phi l s nguyn t khng.

Trnh by thut ton bng s khi:

Begin
Nhp n

T
n<2 F
i=2

i<=sqrt(n
)

i=i+1

n
%i=0
F
L s nguyn t

End

Khng phi l
s nguyn t

S dng thut ton bng gi ng


a)S dng cu trc lp for:
scanf(n);
if(n<2) printf( %d khong phai so nguyen to., n);
else
{
for(i=2; i<=sqrt(n); i++)
if(n%i==0) k=0;
else k=1;
}
if(k==0)

printf( %d khong phai so nguyen to., n);

else

printf( %d la so nguyen to., n);

b)S dng cu trc lp while:


scanf(n);
i=2;
if(n<2) printf( %d khong phai so nguyen to., n);
else
{
while(i<=sqrt(n))
{
if(n%i==0)

k=0;

else
{
k=1;
i++;
}}}
if(k==0)

printf( %d khong phai so nguyen to., n);

else

printf( %d la so nguyen to., n);

c)S dng cu trc lp dowhile


scanf(n);
i=2;
if(n<2) printf( %d khong phai so nguyen to., n);
else
{
do
{
if(n%i==0)

k=0;

else
{
k=1;
i++;
}
}
while(i<=sqrt(n))
}
if(k==0)

printf( %d khong phai so nguyen to., n);

else

printf( %d la so nguyen to., n);

3)
Thut ton: Nhp s nguyn.
M t: Nhp vo s nguyn dng n.
D liu vo: S nguyn dng.
D liu ra: Thc hin c s t h thp phn sang h nh phn.

Trnh by thut ton bng s khi:

Begin
thuong, dem=0, i, n, A[100]

n>=0

Nhp li

T
thuong=n/2, A[dem]=n%2,
n=thuong, dem++

i>=0

In ra s nh
phn

T
i=dem 1, i--

S dng thut ton bng gi ng


do
{
scanf (n);
}
while (n<0);
do
{
thuong = n/ 2;
A[dem] =n %2;

End

n=thuong;
dem++;
}
while(thuong!=0);
printf("So nhi phan la: ");
for(i = dem -1; i>= 0; i--)
{
printf("%d",A[i]);
}
4)
Thut ton: Nhp xu k t.
M t: Nhp vo 1 xu k t.
D liu vo: 1 xu k t.
D liu ra: In ngc xu k t .

S dng thut ton bng gi ng


#include "stdio.h"
#include "conio.h"
#include "string.h"
char daochuoi(char st[])
{
if (strlen(st)==0) return(0);
else
{
printf("%c",st[strlen(st)-1]);
st[strlen(st)-1]='\0';
daochuoi(st);
}

}
main()
{
char st[255];
printf("Nhap vao 1 xau ky tu : ");
gets(st);
daochuoi(st);
getch();
}
5)
Thut ton: Nhp s nguyn.
M t: Nhp vo s nguyn dng n.
D liu vo: S nguyn dng.
D liu ra: Thc hin c s t h thp phn sang h nh phn.

S dng thut ton bng gi ng (Phng php quy)

#include"stdio.h"
#include"conio.h"
void DoiHe(int n)
{
if(n>=2) DoiHe(n/2);
printf("%c",(n%2)["01"]);//Chia 2 thi so du l 0 voi 1
}
main()
{
int n;
printf("---CHUYEN SANG HE NHI PHAN---");
printf("\nSo chuyen doi: ");

scanf("%d",&n);
printf("So nhi phan : ");
DoiHe(n);
getch();
}
6)
Thut ton: Nhp 2 s nguyn.
M t: Nhp vo 2 s nguyn dng .
D liu vo: Hai s nguyn dng.
D liu ra: c chung ln nht ca 2 s nguyn dng.
a)
r=p%q
iu kin dng: r=0
Dng tng qut:
if(r==0) return(q);
else
{
p=q;
q=r;
}
b)S dng phng php lp xy dng UCLN(p,q)
#include "stdio.h"
#include "conio.h"
int UCLN(int &p,int &q)
{
while(q!=0&& p!=0)
{
if(p>q) return(p=p-q);
else return(q=q-p);

if(p==0) return(q);
else return(p);
}
}
int main()
{
int p,q;
printf("Nhap vao 2 so nguyen:");
scanf("%d%d",&p,&q);
if(p==0 || q==0)
printf("UCLN(%d,%d)=%d",p,q,p+q);
else if(p==0 && q==0)
printf("Du lieu sai!!!");
else
printf ("UCLN(%d,%d)=%d",p,q,UCLN(p,q));

getch();
}
c)S dng phng php quy cho hm UCLN(p,q)
#include"stdio.h"
#include"conio.h"
#include"math.h"
int UCLN(int p, int q)
{
int r;
if(p>q)
{
r=p%q;
if(r==0) return (q);

else
{
p=q;
q=r;
}
}
else
{
r=q%p;
if(r==0) return (p);
else
{
p=q;
q=r;
}
}
return (UCLN(p,q));
}
main()
{
int p,q;
int r;
printf("Nhap vao so thu nhat: "); scanf("%d",&p);
printf("Nhap vao so thu hai : "); scanf("%d",&q);
if((p==0)||(q==0)) printf("Du lieu sai!!!");
else
{
UCLN(p,q);
printf("UCLN(%d,%d)=%d",p,q,UCLN(p,q));

}
getch();
}

7)
Thut ton: Nhp s nguyn n.
M t: Nhp vo s nguyn dng .
D liu vo: S nguyn dng.
D liu ra: S th n trong chui Fibonacci.

S dng thut ton bng gi ng


#include"stdio.h"
#include"conio.h"
int Fibonacci(int n)
{
if(n<=2) return (1);
else return (Fibonacci(n-2) + Fibonacci(n-1));
}
main()
{
int n;
printf("Nhap vao so n: ");
scanf("%d",&n);
Fibonacci(n);
printf("So thu %d trong chuoi Fibonacci: %d",n,Fibonacci(n));
getch();
}

1.3)
#include"stdio.h"
#include"conio.h"
#include"math.h"
main()
{
int n,i,k=0,thuong,A[100];
printf("Nhap vao mot so nguyen: ");
scanf("%d",&n);
do
{
thuong=n/2;
A[k]=n%2;
n=thuong;
k++;
}
while(thuong!=0);
printf("\nSo nhi phan la: ");
for(i=k-1;i>=0;i--)
printf("%d",A[i]);
getch();
}

Вам также может понравиться