C++ trong 21 ngay
#include "iostream"
#include "math.h"
using namespace std;
void findPrimesSequence(int N,int *primes);
int findNextPrimes(int k,int *primes);
/******************** Main function ****************** */
int main(){
int N;
int *primes;
cout< <"Input the number of primes you want to find: ";
cin>>N;
if(N< =0) exit(1);
cout<<endl;
primes=new int[N] ;
findPrimesSequence(N,primes);
for (int i=1;i<=N;i++){
cout.width(8);
cout<<primes[i-1];
if (i%9==0) cout<<endl;
};
delete primes;
return 0;
}
/*************** Other functions *****************/
void findPrimesSequence(int N,int *primes){
primes[0]=2;
if (N>1) primes[1]=3;
int k=2;
while (k<n ){
primes[k]=findNextPrimes(k-1,primes);
k++;
};
}
int findNextPrimes(int k,int *primes){
int start=primes[k];
while(1){
start+=2;
bool found=true;
if (start%6==1||start%6==5){
for(int i=1;i<sqrt(start);i++)
if (start % primes[i]==0) found=false;
if (found==true) return(start);
};
};
}
//Hàm Delete_k xóa ph?n t? t?i v? trí k
void Delete_k(int A[], int k, int &n) //k la vi tri phan tu can xoa
{
for(int i = k; i < n-1; i++)
A[i] = A[i+1];
n--;
}
//Hàm Find_k ?i tìm v? trí c?n xóa
void Find_k(int A[], int &n) //n la so phan tu cua mang
{
for(int i = 0; i < n-1; i++
for(int j = i+1; j < n; j++)
if(A[j] == A[i])
{
Delete_k(A, j, n);
j--;
}
}
#include <stdio.h>
#include <conio.h>
#define MAX 100
int main() {
int n,i,j,k;
double a[MAX];
printf("Size of array:");
scanf("%d",&n);
printf("Input array:
");
for (i=0;i<n;++i) {
printf("a[%d] = ",i+1);
scanf("%lf",&a[i]);
}
for (i=0;i<n-1;++i) {
for (j=i+1;j<n;++j)
if (a[j] == a[i]) {
for (k=j;k<n-1;++k) a[k]=a[k+1];
n--;
}
}
printf("Array after delete duplicate number:");
for (i=0;i<n;++i)
printf("%8.3lf",a[i]);
getch();
return 0;
}
ti?a ma?ng
#include <stdio.h>
#include <conio.h>
//bai tap tia mang.
int array[]={4,6,7,1,4,8,6,4,9,6,4,10,1,12,19};
int n=15,m=2;
int array_tmp[12]={0,0,0,0,0,0,0,0,0,0,0,0};//mang xac dinh xem 1 phan tu da duoc dem chua. ban dau bag 0 neu da dc dem thi bang 1
void xoa_array(int *array,int *n,int k){//ham xoa dj 1 phan tu thu k,va sau do so luong phan tu dc cap nhap laj la bot dj 1
int i;
for(i=k;i<*n;i++) array[i]=array[i+1];
--*n;
}
void tia_mang(int *array,int *n, int m){//ham tia mang sau do thi cap nhat luon so phan tu con lai
int i,j,dem;
for(i=0;i<*n;i++) if(array_tmp[i]==0){
dem=0;
for(j=i;j<*n;j++) if (array[j]==array[i]){
dem ++;
array_tmp[i]=1;
if (dem>m) xoa_array(array,n,j);
}
}
}
int main(){
int i;
tia_mang(array,&n,m);
printf("%d
",n);
for(i=0;i<n;i++) printf("%d ",array[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<malloc.h>
int n;//SO PHAN TU CUA MANG CHINH
int sopt_mp=0;//SO PHAN TU CUA MANG PHU
int m;//PHAN TU m CUA DE BAI
int mang[30],mangphu[15];
void Nhap();
void Xuat();
void GanGiatrichomangphu();//GAN GIA TRI CHO MANG PHU
int search_to_del();
int Dem_soluong(int);//DEM SO LUONG PHAN TU CO GIA TRI LA a.
int Tim_x_mangphu(int);//TIM PHAN TU CO GIA TRI x TRONG MANG PHU
void Xoa1_pt_mangphu();//XOA PHAN TU DAU TIEN TRONG MANG PHU
void Tiamang();
void Xuatmangphu();
void Delete();
int main()
{
Nhap();
printf("Mang ban dau la:
");
Xuat();
GanGiatrichomangphu();
Tiamang();
printf("Ket qua:
%d
",n);
Xuat();
getch();
return 0;
}
int search_to_del()//TIM KIEM PHAN TU, TRA VE VI TRI CUA NO TRONG MANG
{
for(int i=0;i<sopt_mp;i++)
for(int j=n-1;j>=0;j--)
{
if(mangphu[i]==mang[j])
return j;
}
return -1;
}
void Delete()//XOA PHAN TU TAI VI TRI pos TRONG MANG CHINH
{
int pos=0;
pos=search_to_del();
if(search_to_del()!=-1)
{
for(int i=pos;i<n;i++)
mang[i]=mang[i+1];
n--;
}
}
void Xoa1_pt_mangphu()
{
for(int i=0;i<sopt_mp-1;i++)
mangphu[i]=mangphu[i+1];
sopt_mp--;
}
void Tiamang()
{
int bientam=sopt_mp;//BIEN NAY DE LUU SO LUONG PHAN TU TRONG MANG PHU BAN DAU
for(int i=0;i<bientam;i++)
{
int counttemp=Dem_soluong(mangphu[i]);//BIEN NAY DE KIEM SOAT SO LUONG PHAN TU CO GIA TRI BANG NHAU TRONG MANG CHINH .
while(counttemp-->m)//DEM SO LUONG PHAN TU CO GIA TRI LA mangphu[i] TRONG MANG CHINH
{
Delete();
}
Xoa1_pt_mangphu();
}
}
void Nhap()
{
printf("Nhap so phan tu: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Nhap vao phan tu thu %d: ",i+1);
scanf("%d",&mang[i]);
}
}
void Xuat()
{
for(int i=0;i<n;i++)
printf("%d ",mang[i]);
}
int Dem_soluong(int a)
{
int dem=0;//BIEN NAY DE DEM SO LUONG PHAN TU CO GIA TRI a.
for(int i=0;i<n;i++)
{
if(mang[i]==a)
dem++;
}
return dem;
}
int Tim_x_mangphu(int x)
{
for(int i=0;i<sopt_mp;i++)
{
if(mangphu[i]==x)
return 1;
}
return 0;
}
void GanGiatrichomangphu()
{
printf("
Nhap vao gia tri m=");
scanf("%d",&m);
for(int i=n-1;i>=0;i--)
{
if((Dem_soluong(mang[i])>m)&&(Tim_x_mangphu(mang[i])==0))
{
mangphu[sopt_mp]=mang[i];
sopt_mp++;
}
}
}
void Xuatmangphu()
{
printf("
");
for(int i=0;i<sopt_mp;i++)
printf("%d ",mangphu[i]);
printf("
");
}
xóa 1 ph?n t? trong mãng và xu?t nó ra.
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
void nhap (int a[],int &n)
{
cout<<"Nhap vao so phan tu: ";cin>>n;
for(int i=0;i<n;i++)
{ printf("Nhap a[%d]: ",i);
cin>>a[i];
}
}
void xuat (int a[], int n)
{
cout<<"Mang vua nhap la: ";
for(int i=0;i<n;i++)
cout<<" "<<a[i];
}
void xoabot (int a[],int n)
{ int b;
cout<<"Vi tri bi xoa trong mang: ";cin>>b;
for(int i=0;i<n;i++)
if(i>=b)
{
for(int j=b+1;j<n;j++)
a[i]=a[j];
}
cout<<"Mang con lai sau khi xoa: ";
for(int k=0;k<n-1;k++)
cout<<" "<<a[k];
}
void main()
{ clrscr();
int a[100],n;
nhap(a,n);
cout<<"
";
xuat(a,n);
cout<<"
";
xoabot(a,n);
getch();
}
//
void xoabot (int a[],int &n)
{
int b;
cout<<"Vi tri bi xoa trong mang: ";cin>>b;
if (0<=b && b<n)
{
for(int i = b ; i<n-1 ; i++)
{
a[i] = a[i+1];
}
}
n--;
Xuat(a,n);
}
sua bai tren ve kiem tra lai
void xoabot (int a[],int &n)
{
int b;
cout<<"Vi tri bi xoa trong mang: ";cin>>b;
if (0<=b && b<n)
{
for(int i = b ; i<n-1 ; i++)
{
a[i] = a[i+1];
}
n--;
}
xuat(a,n);
}
Bạn đang đọc truyện trên: Truyen247.Pro