#include<stdio.h>
#include<pthread.h>
#include<time.h>
typedef struct
{
int i;
double result;
}Param;
//////
long n;//the number which every thread should count
//////
void p(void *v)
{
Param* pa=(Param*)v;
int i=(*pa).i;
int begin=0;
int end=0;
double x=0,y=0;
begin=i*n+1;
end=(i+1)*n;
int j=0;
double total=0;
for(j=begin;j<=end;j++)
{
if(j%3==0) x=y=1.0;
if(j%3==1) x=y=-1.0;
if(j%3==2) x=y=0.0;
total+=x*y;
}
(*pa).result=total;
}
/////////////////////////
int main(int args,char*argv[])
{
int threadnumber;//the number of threadnumber
long length;//
scanf("%d",&threadnumber);
scanf("%ld",&length);
n=length/threadnumber;
//////////////////////////////////////////////
int i=0;
Param* param;
param=(Param*)malloc(sizeof(Param)*(threadnumber-1));
//////////////////////////////////////////////
for(i=0;i<=threadnumber-2;i++)
{
param[i].i=i;
param[i].result=0;
}
/////////////////////////////////////
int ret;
int a[threadnumber];
pthread_t pt[threadnumber];
for(i=0;i<=threadnumber-2;i++)//开始线程
{
a[i]=i;
ret=pthread_create(&pt[i],NULL,(void*)p,¶m[a[i]]);
if(ret!=0)printf("error\n");
}
///////////////////////
int k;
for(k=0;k<=threadnumber-2;k++)
pthread_join(pt[a[k]],NULL);
//////////////////////////
int h=0;
double total=0,x=0,y=0;
for(h=(threadnumber-1)*n+1;h<=length;h++)
{
if(h%3==0) x=y=1.0;
if(h%3==1) x=y=-1.0;
if(h%3==2) x=y=0.0;
total+=x*y;
}
/////////////////////////
double last=0;//count the total
for(i=0;i<=threadnumber-2;i++)
{
last+=param[i].result;
}
last+=total+1;
printf("the last result is %f \n ",last);
/////////////////////////
return (0);
}
#include<pthread.h>
#include<time.h>
typedef struct
{
int i;
double result;
}Param;
//////
long n;//the number which every thread should count
//////
void p(void *v)
{
Param* pa=(Param*)v;
int i=(*pa).i;
int begin=0;
int end=0;
double x=0,y=0;
begin=i*n+1;
end=(i+1)*n;
int j=0;
double total=0;
for(j=begin;j<=end;j++)
{
if(j%3==0) x=y=1.0;
if(j%3==1) x=y=-1.0;
if(j%3==2) x=y=0.0;
total+=x*y;
}
(*pa).result=total;
}
/////////////////////////
int main(int args,char*argv[])
{
int threadnumber;//the number of threadnumber
long length;//
scanf("%d",&threadnumber);
scanf("%ld",&length);
n=length/threadnumber;
//////////////////////////////////////////////
int i=0;
Param* param;
param=(Param*)malloc(sizeof(Param)*(threadnumber-1));
//////////////////////////////////////////////
for(i=0;i<=threadnumber-2;i++)
{
param[i].i=i;
param[i].result=0;
}
/////////////////////////////////////
int ret;
int a[threadnumber];
pthread_t pt[threadnumber];
for(i=0;i<=threadnumber-2;i++)//开始线程
{
a[i]=i;
ret=pthread_create(&pt[i],NULL,(void*)p,¶m[a[i]]);
if(ret!=0)printf("error\n");
}
///////////////////////
int k;
for(k=0;k<=threadnumber-2;k++)
pthread_join(pt[a[k]],NULL);
//////////////////////////
int h=0;
double total=0,x=0,y=0;
for(h=(threadnumber-1)*n+1;h<=length;h++)
{
if(h%3==0) x=y=1.0;
if(h%3==1) x=y=-1.0;
if(h%3==2) x=y=0.0;
total+=x*y;
}
/////////////////////////
double last=0;//count the total
for(i=0;i<=threadnumber-2;i++)
{
last+=param[i].result;
}
last+=total+1;
printf("the last result is %f \n ",last);
/////////////////////////
return (0);
}