/* 201. */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f201(double*, int, double*, int *);
int main(void){
FILE* in;
FILE* out;
int ERROR;
double* A; int q,i; int N; double y;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N);
if (q!=1 || N<0) return -1;
A=(double*) malloc(N*sizeof(double)); assert(A!=NULL);
for(i=0; i<N; i++) q=fscanf(in, "%lf", &A[i]);
assert (q==1);
f201(A,N, &y, &ERROR);
if(ERROR==-1) return ERROR;
fprintf(out,"%lf ", y);
fclose(in); fclose(out); free(A);
return 0;
}
void f201(double A[], int N, double* y, int* err){
int i,n; double s;
assert(N>0);
for (i=0,n=0,s=0.0; i<N; i++)
if(A[i]>0.0) {n=n+1; s=s+A[i];}
if (n==0) *err=-1;else *err=0;
*y=(s+0.0)/n;
return;
}
/* file 202.c
202. .
: .
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f202(double*, int);
int main(void){
FILE* in;
FILE* out;
double* A; int q,i; int N;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N);
if (q!=1 || N<1) return -1;
A=(double*) malloc(N*sizeof(double));
for(i=0; i<N; i++) q=fscanf(in, "%lf", &A[i]);
assert (q==1);
f202(A,N);
for(i=0; i<N; i++) fprintf(out,"%lf ",A[i]);
fclose(in); fclose(out); free(A);
return 0;
}
void f202(double A[], int N){
double tmp;
int ind1=0, ind2=N-1;
assert(N>0);
while (ind1<ind2){
tmp=A[ind1]; A[ind1]=A[ind2]; A[ind2]=tmp;
ind1++; ind2--;
}
return;
}
. 1) out.txt , "%lf ". , .
/* 203. : Anew[i]=A[0]+A[1]+...+A[i] */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f203(double*, int);
int main(void){
FILE* in;
FILE* out;
double* A; int q,i; int N;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N);
if (q!=1 || N<1) return -1;
A=(double*) malloc(N*sizeof(double));assert(A!=NULL);
for(i=0; i<N; i++) q=fscanf(in, "%lf", &A[i]);
assert (q==1);
f203(A,N);
for(i=0; i<N; i++) fprintf(out,"%lf ",A[i]);
|
|
fclose(in); fclose(out); free(A);
return 0;
}
void f203(double A[], int N){
int i;
assert (N>0);
for (i=1; i<N; i++) A[i]=A[i-1]+A[i];
return;
}
/* 204. 1: Anew[0]=A[N-1];Anew[i]=A[i-1] */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f204(int*, int);
int main(void){
FILE* in;
FILE* out;
int* A; int q,i; int N;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N);
if (q!=1 || N<1) return -1;
A=(int*) malloc(N*sizeof(int));
for(i=0; i<N; i++) q=fscanf(in, "%d", &A[i]);
assert (q==1);
f204(A,N);
for(i=0; i<N; i++) fprintf(out,"%d ",A[i]);
fclose(in); fclose(out); free(A);
return 0;
}
void f204(int A[], int N){
int i, tmp;
assert (N>0);
tmp=A[N-1];
for (i=N-1; i>0; i--) A[i]=A[i-1];
A[0]=tmp;
return;
}
205
assert
205. . : - .
, 206 209 . f205sort. 205.c : main, f205, f205sort.
f205sort , , (x,y) :
void f205sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
.........
/* file 205.c */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f205sort(int*, int);
void f205(int*, int, int*); int main(void){
FILE* in; FILE* out;
int* A;
int q, N, i;
int otvet;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N); if (q!=1 || N<1) return -1;
A = (int*) malloc(N*sizeof(int));
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
assert (q==1);
f205(A,N,&otvet);
fprintf(out,"%d ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
void f205sort(int A[], int N){
#define Besporyadok(x,y) (x>y)
int i,j,tmp;
int ChisloBespor;
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f205 (int A[], int N, int* y){
int i, n, last;
assert(N>0);
f205sort(A,N);
last=A[0];
for(i=1,n=1;i<N;i++){
if(last<A[i]) n++;
last=A[i];
}
*y=n;
return;
}
/* 206. .
O f205sort , . */
|
|
/* (*y,*ny)=( , ) */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f205sort(int*, int);
void f205(int*, int, int*); int main(void){
FILE* in; FILE* out;
int* A;
int q, N, i;
int otvet; /* f205 */
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (int*) malloc(N*sizeof(int));/* N*4 */
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
assert (q==1); /* */
f205(A,N,&otvet);
fprintf(out,"%d ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f205sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f206(int A[], int N, int* y, int* ny){
int i, last, znach, dlina, maksdlina;
assert (N>0);
f205sort(A,N);
/* . [N] . . */
last=A[0]; znach=A[0]; dlina=1; maksdlina=1;
for (i=1; i<N; i++){
if (A[i]==last){
dlina++;
if(dlina>maksdlina){maksdlina=dlina;znach=A[i];}
}
else {znach=A[i]; dlina=1;}
last=A[i];
}
*y=znach; *ny=maksdlina;
return;
}
/* 207. .
f207sort o , , .
; , . */
/* .*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f207sort(int*, int);
void f207(int*, int);
int main(void){
FILE* in;
FILE* out;
int* A;
int q, N, i;
out=fopen("output.txt","w"); if(out==NULL)return -1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return -1;}
q=fscanf(in, "%d", &N);
if (q!=1 || N<1) return -1;
A = (int*) malloc(N*sizeof(int));
assert(A!=NULL);
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
f207(A,N);
for (i=0; i<N; i++) fprintf(out, "%d ", A[i]);
fclose(in); fclose(out); free(A);
return 0;
}
void f207sort(int A[], int N){
#define Besporyadok(x,y) (x<0&&y>=0)
int i,j,tmp;
int ChisloBespor;
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f207(int A[], int N){
int i;
assert (N>0);
f207sort(A,N);
for (i=N-1; i>=0; i--){
if (A[i]>=0)return; else A[i]=0;
}
return;
}
208. .
. 2- [i] [i-1] double tek, pred.
|
|
/* new[i]=(A[i-1]+A[i+1])/2;
new[0]=(0+A[1])/2; new[N-1]=(A[N-2]+0)/2; */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f208(int*, int, int*); int main(void){
FILE* in; FILE* out;
int* A; // A[]
int q, N, i;
int otvet; /* f205 */
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (int*) malloc(N*sizeof(int));/* N*4 */
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
assert (q==1); /* */
f208(A,N,&otvet);
fprintf(out,"%d ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
void f208(double A[], int N){
int i; double pred, tek, predposl;
assert (N>0);
if(N==1){A[0]=0.0;return;};
predposl=A[N-2];
pred=A[0];
A[0]=A[1]/2.0;
for (i=1; i<N-1; i++){
tek=A[i]; A[i]=(pred+A[i+1])/2.0; pred=tek;
}
A[N-1]=predposl/2.0;
return;
}
209. .
A f205sort. A [ A[0], A[N-1] ]. , - 1. f209
: . main output.txt ; -1
. {5,3,6,2,8,3,5,7,2} , a {2,2,3,3, 5,5,6,7,8} (3, 5).
/* */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f209sort(int*, int);
void f209(int*, int, int*); int main(void){
FILE* in; FILE* out;
int* A; // A[]
int q, N, i;
int otvet; /* f205 */
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (int*) malloc(N*sizeof(int));/* N*4 */
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
assert (q==1); /* */
f209(A,N,&otvet);
fprintf(out,"%d ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f209sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f209(int A[], int N, int* LEV, int* PRAV, int* y){
int i,chislo_dyr,last;
assert (N>0);
f205sort(A,N);
/* : */
last=A[0]; chislo_dyr=0;
for (i=1; i<N; i++){
if (A[i]>(last+1))chislo_dyr++;
|
|
last=A[i];
}
*LEV=A[0]; *PRAV=A[N-1]; *y=chislo_dyr;
return;
}
:
/* file 209.c */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f205sort(int*, int);
void f209(int*, int, int*, int*, int*);
int main(void){
FILE *in, *out;
int* A; /* A[] */
int q,N,i;
int lev,prav, chislo_dyr; /* f209 */
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in,"%d",&N); assert(q==1&&N>0);/ *N */ A = (int*) malloc(N*sizeof(int)); /* N*4 */
for(i=0; i<N; i++) q=fscanf(in,"%d",&A[i]);
assert (q==1); /* */
f209(A,N,&lev,&prav,&chislo_dyr);
if(chislo_dyr==0) fprintf(out,"%d %d",lev,prav);
if(chislo_dyr >0) fprintf(out,"%d ",-1);
fclose(in); fclose(out); free(A);
return 0;
}
< void f209(...){...} >
< void f205sort(...){...} >
/* 209.c */
/* 210. : [0]+A[2]+...=A[1]+A[3]+...? */
void f210(int A[], int N, int* y){
int i, s0=0,s1=0;
assert (N>1);
for (i=0; i+1<N; i=i+2) {s0=s0+A[i]; s1=s1+A[i+1];}
/* ,
s0 : */
if(N%2==1)s0=s0+A[N-1];
if(s0==s1)*y=1;
if(s0!=s1)*y=0;
return;
}
211. , ,
A[0]+A[1]+...A[k-1]=A[k]+A[k+1]+...+A[N-1]?
. "", , .
/* , k ,
A[0]+A[1]+...A[k-1]=A[k]+A[k+1]+...+A[N-1]? */
void f211(int A[], int N, int* addr_k, int* addr_S){
int i,k,s,Slev;
assert (N>1);
for (i=0,s=0; i<N; i++) s=s+A[i];
Slev=A[0];
for(k=1; k<N; k++){
if(2*Slev==s){*addr_k=k; *addr_S=Slev; return;}
Slev=Slev+A[k];
}
*addr_k=-1;
return;
}
212. .
f212 205sort tmp int double 1 3. .
/* */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f212sort(double*, double);
void f212(double*, double, double*);
int main(void){
FILE* in; FILE* out;
double* A; // A[]
int q, N, i;
int otvet;
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (double*) malloc(N*sizeof(double));
for(i=0; i<N; i++) q=fscanf(in,"%lf ",&A[i]);
assert (q==1); /* */
f212(A,N,&otvet);
fprintf(out,"%lf ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f212sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f212(double A[], int N){
#define Besporyadok(x,y) (x>y) // <==>
int i,j; double tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
f213, f214 f215 f212 . 215 double fabs(double x), . 215 #include <math.h>
|
|
213. , , , -
, .
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f213sort(double*, double);
void f213(double*, double, double*);
int main(void){
FILE* in; FILE* out;
double* A; // A[]
int q, N, i;
int otvet;
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (double*) malloc(N*sizeof(double));
for(i=0; i<N; i++) q=fscanf(in,"%lf ",&A[i]);
assert (q==1); /* */
f213(A,N,&otvet);
fprintf(out,"%lf ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f213sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f213(double A[], int N){
#define Besporyadok(x,y) ((x>=0&&y>=0&&x>y)||(x<0&&y<0&&x<y)||(x<0&&y>=0))
int i,j; double tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
214. , , .
, .
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void f214sort(double*, double);
void f214(double*, double, double*);
int main(void){
FILE* in; FILE* out;
double* A; // A[]
int q, N, i;
int otvet;
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (double*) malloc(N*sizeof(double));
for(i=0; i<N; i++) q=fscanf(in,"%lf ",&A[i]);
assert (q==1); /* */
f214(A,N,&otvet);
fprintf(out,"%lf ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f214sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f214(double A[], int N){
#define Besporyadok(x,y) (x>=0 && y<0)
int i,j; double tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
(x,y). E Besporyadok(x,y) (x,y) (y,x). , , .
215. ( ). : , , , ,
.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
void f215sort(double*, double);
void f215(double*, double, double*);
int main(void){
FILE* in; FILE* out;
double* A; // A[]
int q, N, i;
int otvet;
out=fopen("output.txt","w"); if(out==NULL)return 1;
in =fopen("input.txt","r"); if(in==NULL){fclose(out);return 1;}
q=fscanf(in, "%d", &N); assert(q==1&&N>0); /* N */
A = (double*) malloc(N*sizeof(double));
for(i=0; i<N; i++) q=fscanf(in,"%lf ",&A[i]);
assert (q==1); /* */
f215(A,N,&otvet);
fprintf(out,"%lf ",otvet);
fclose(in); fclose(out); free(A);
return 0;
}
/* .
,
x, y x<=y.
*/
void f215sort(int A[], int N){
#define Besporyadok(x,y) (x>y) /* <==> */
int i,j,tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
void f215(double A[], int N){
#define Besporyadok(x,y) ((fabs(x))<(fabs(y)))
int i,j; double tmp;
int ChisloBespor; /* */
assert (N>0);
for (i=N-1; i>0; i=i-1) {
ChisloBespor=0;
for (j=0; j<i; j=j+1) {
if(Besporyadok(A[j],A[j+1])){
tmp=A[j];A[j]=A[j+1];A[j+1]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}
216. , , . , , .
void f216(double A[], int N){
#define Besporyadok(x,y) (x>y)
int i,j; double tmp;
int ChisloBespor; //
assert (N>0);
if (N<=3) return;
for (i=N-1; i>=3; i=i-2) {
ChisloBespor=0;
/* 1 j-1 */
/* */
for (j=1; j+1<i; j=j+2) {
if(Besporyadok(A[j],A[j+2])){
tmp=A[j];A[j]=A[j+2];A[j+2]=tmp;
ChisloBespor++;
}
}
if(ChisloBespor==0) return;
}
return;
}