Anonymous
BAN USER
/*
Given Two separate integer arrays,
find the combined median of the two arrays without using any extra array
*/
#include <iostream>
#include <algorithm>
#define MAX 9999
using namespace std;
double medianOfTwoIntegerArrays(int array1[],int nlength1, int array2[], int nlength2)
{
sort(array1 , array1 + nlength1);
sort(array2 , array2 + nlength2);
int index1 = 0;
int index2 = 0;
int i = 1;
int num1 = 0;
int num2 = 0;
int sumlength = nlength1 + nlength2;
if(sumlength % 2 == 0)
{
while(i <= (sumlength/2 - 1))
{
//如果array1和array2都没有遍历完
if(index1 < nlength1 && index2 < nlength2 && array1[index1] <= array2[index2])
{
index1++;
if(index1 == nlength1)
index1 = MAX;
i++;
}
else if(index1 < nlength1 && index2 < nlength2 && array1[index1] > array2[index2])
{
index2++;
if(index2 == nlength1)
index2 = MAX;
i++;
}
//如果遍历完了array1
else if(index1 == MAX)
{
index2++;
i++;
}
//如果遍历完了array2
else if(index2 == MAX)
{
index1++;
i++;
}
}
//取得最中间的两个数的值
//array1和array2都没遍历完
if(index1 != MAX && index2 != MAX)
{
num1 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
if(array1[index1] < array2[index2])
{
if(nlength1 != ++index1)
{
num2 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else
{
num2 = array2[index2];
}
}
else if((array1[index1] == array2[index2]))
{
num2 = array2[index2];
}
else
{
if(nlength1 != ++index1)
{
num2 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else
{
num2 = array2[index2];
}
}
}
//array1遍历完
else if(index1 == MAX )
{
num1 = array2[index2];
index2++;
num2 = array2[index2];
}
//array2遍历完
else
{
num1 = array1[index1];
index1++;
num2 = array2[index1];
}
return ((double)num1 + (double)num2) / 2;
}
else
{
while( i <= sumlength/2)
{
//如果array1和array2都没有遍历完
if(index1 < nlength1 && index2 < nlength2 && array1[index1] <= array2[index2])
{
index1++;
if(index1 == nlength1)
index1 = MAX;
i++;
}
else if(index1 < nlength1 && index2 < nlength2 && array1[index1] > array2[index2])
{
index2++;
if(index2 == nlength1)
index2 = MAX;
i++;
}
//如果遍历完了array1
else if(index1 == MAX)
{
index2++;
i++;
}
//如果遍历完了array2
else if(index2 == MAX)
{
index1++;
i++;
}
}
if(index1 != MAX && index2 != MAX)
{
num1 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else if(index1 == MAX)
{
num1 = array2[index2];
}
else
{
num1 = array1[index1];
}
return num1;
}
}
int main()
{
int a[8] = {6,3,9,3,6,54,22,12};
int b[5] = {2,25,8,20,21};
double c = medianOfTwoIntegerArrays(a,8,b,5);
cout<<c<<endl;
system("pause");
}
/*
Given Two separate integer arrays,
find the combined median of the two arrays without using any extra array
*/
#include <iostream>
#include <algorithm>
#define MAX 9999
using namespace std;
double medianOfTwoIntegerArrays(int array1[],int nlength1, int array2[], int nlength2)
{
sort(array1 , array1 + nlength1);
sort(array2 , array2 + nlength2);
int index1 = 0;
int index2 = 0;
int i = 1;
int num1 = 0;
int num2 = 0;
int sumlength = nlength1 + nlength2;
if(sumlength % 2 == 0)
{
while(i <= (sumlength/2 - 1))
{
//如果array1和array2都没有遍历完
if(index1 < nlength1 && index2 < nlength2 && array1[index1] <= array2[index2])
{
index1++;
if(index1 == nlength1)
index1 = MAX;
i++;
}
else if(index1 < nlength1 && index2 < nlength2 && array1[index1] > array2[index2])
{
index2++;
if(index2 == nlength1)
index2 = MAX;
i++;
}
//如果遍历完了array1
else if(index1 == MAX)
{
index2++;
i++;
}
//如果遍历完了array2
else if(index2 == MAX)
{
index1++;
i++;
}
}
//取得最中间的两个数的值
//array1和array2都没遍历完
if(index1 != MAX && index2 != MAX)
{
num1 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
if(array1[index1] < array2[index2])
{
if(nlength1 != ++index1)
{
num2 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else
{
num2 = array2[index2];
}
}
else if((array1[index1] == array2[index2]))
{
num2 = array2[index2];
}
else
{
if(nlength1 != ++index1)
{
num2 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else
{
num2 = array2[index2];
}
}
}
//array1遍历完
else if(index1 == MAX )
{
num1 = array2[index2];
index2++;
num2 = array2[index2];
}
//array2遍历完
else
{
num1 = array1[index1];
index1++;
num2 = array2[index1];
}
return ((double)num1 + (double)num2) / 2;
}
else
{
while( i <= sumlength/2)
{
//如果array1和array2都没有遍历完
if(index1 < nlength1 && index2 < nlength2 && array1[index1] <= array2[index2])
{
index1++;
if(index1 == nlength1)
index1 = MAX;
i++;
}
else if(index1 < nlength1 && index2 < nlength2 && array1[index1] > array2[index2])
{
index2++;
if(index2 == nlength1)
index2 = MAX;
i++;
}
//如果遍历完了array1
else if(index1 == MAX)
{
index2++;
i++;
}
//如果遍历完了array2
else if(index2 == MAX)
{
index1++;
i++;
}
}
if(index1 != MAX && index2 != MAX)
{
num1 = array1[index1] < array2[index2] ? array1[index1] :array2[index2];
}
else if(index1 == MAX)
{
num1 = array2[index2];
}
else
{
num1 = array1[index1];
}
return num1;
}
}
int main()
{
int a[8] = {6,3,9,3,6,54,22,12};
int b[5] = {2,25,8,20,21};
double c = medianOfTwoIntegerArrays(a,8,b,5);
cout<<c<<endl;
system("pause");
}
#include <iostream>
using namespace std;
int maxsum(int array[], int nlength)
{
int nfirst;
int nsecond;
if(array[0] > array[1])
{
nfirst = array[0];
nsecond = array[1];
}
else
{
nfirst = array[1];
nsecond = array[0];
}
for(int i=2; i<nlength; i++)
{
if(array[i] >= nfirst)
{
nsecond = nfirst;
nfirst = array[i];
}
}
return nfirst + nsecond;
}
int main()
{
int a[6] = {8,9,-6,0,3,9};
cout<<maxsum(a,6)<<endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void zero(int* array , int n)
{
#define array(i,j) array[i*n+j]
int *row = (int*) malloc (n * sizeof(int));
for(int i=0; i<n; i++)
row[i] = 0;
int *col = (int*) malloc (n * sizeof(int));
for(int i=0; i<n; i++)
col[i] = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(0 == array(i,j))
{
row[i] = 1;
col[j] = 1;
}
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(row[i] == 1 || col[j] == 1)
array(i,j) = 0;
}
}
free(row);
free(col);
}
int main()
{
int a[9] = {1,2,0,4,5,6,7,8,9};
zero(a,3);
for(int i=0; i<9; i++)
printf("%d " , a[i]);
system("pause");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
void MyPrint(char * strdes)
{
int N = (strlen(strdes) - 1) * 4 + 4 + 1;
char* strsrc;
int index = 0;
if(NULL == (strsrc =(char *) malloc (sizeof(char) * N)))
{
perror("not enough memeory!");
exit(1);
}
for(int i=0; i< strlen(strdes)-1; i++)
{
for(int j=0; j<2; j++)
strsrc[index++] = strdes[i];
}
for(int j=0; j<4; j++)
strsrc[index++] = strdes[strlen(strdes)-1];
for(int i= strlen(strdes)-2; i>=0; i--)
{
for(int j=0; j<2; j++)
strsrc[index++] = strdes[i];
}
strsrc[index] = '\0';
printf("%s\n", strsrc);
free(strsrc);
}
int main()
{
char* s = "ABC";
MyPrint(s);
}
Repdalejohnson762, Accountant at ABC TECH SUPPORT
I have exceptionally skilled Journalists possessed dogged determination to find the new story and deliver it to the public. I ...
Repjonesreina93, Hawaiian airlines reservations at American Airlines
Hello, I am currently working at the airport as an air hostess, and my responsibility are Check tickets and assisted ...
Repmelonydmaxwell, maintenence engineer at AMD
Hi, I am working as a health information technician and my work is to collect and maintain a patient's ...
Repfarleym761, Accountant at Adobe
Enjoy meeting up with my friends and family, and I currently volunteer as a guest columnist for my local paper ...
Repellenabeaudry4, Analyst at Boomerang Commerce
I'm a 23 year-old blogger, make-up junkie and follower of Hinduism.I love Reading because it brings happiness for ...
RepI believe in magic, power, aliens, parallel universe, god. I always dream about the powers and out of the world ...
Repellahrivas6, Android test engineer at 247quickbookshelp
I am working as an internist and I work in medical offices, clinics, and hospitals. An internist may work independently ...
RepGinaSanchez, Computer Scientist at Autoportal.com
Ginna from New York.Successfully managing a high volume of work assignments without compromising quality to exceed client expectations.Apart ...
- Anonymous October 09, 2013