#include "vector"
#include "stdio.h"
#include "stdlib.h"
#include <algorithm>
#include <iostream>
#include<math.h>
using namespace std;
//******************--求指针数组的长度--*********************
int main31()
{
char *p[]={"zsxdzx","sdf","ZAcds"};
//int s = sizeof(p)/sizeof(p[0]);
int s = sizeof(p)/sizeof(*p);
printf("%d\n",s);
printf("%c\n",p[1][0]);
return 0;
}
//******************--快速排序--*********************
void Swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int Partition(int data[], int length, int start, int end)
{
if(data == NULL || length <=0 || start <0 || end >= length)
return -1;
int index = (rand() % (end-start+1))+ start;
Swap(&data[index],&data[end]);
int small = start-1;
for (index=start;index<end;index++)
{
if(data[index]<data[end])
{
++small;
if(index != small)
Swap(&data[index],&data[small]);
}
}
++small;
Swap(&data[index],&data[small]);
return small;
}
void QuickSort(int data[], int length, int start, int end)
{
if(data == NULL || length <=0 || start <0 || end >= length)
return;
int index = Partition(data,length,start,end);
if(index>start)
QuickSort(data,length,start,index-1);
if(index<end)
QuickSort(data,length,index+1,end);
}
int main32()
{
int number[] = {3,6,78,9,1,2,4,12,3};
int startNum = 0;
int endLen = sizeof(number)/sizeof(number[0]);
QuickSort(number, endLen, startNum, endLen-1);
//int num1=0, num2 = 10;
//Swap(&num1,&num2);
//printf("%d,%d\n",num1,num2);
for (int i=0; i<endLen;i++)
printf("%d\t",number[i]);
return 0;
}
//******************--年龄排序--*********************
void SortAge(int ages[], int length)
{
if(ages == NULL || length <=0)
return;
const int oldAge = 99;
int timeOfAge[oldAge+1];
for(int i=0;i<=oldAge;i++)
timeOfAge[i] = 0;
for(int i=0; i<length; i++){
int age = ages[i];
if(age <=0 || age >oldAge)
throw std::exception("input age wrong!");
timeOfAge[age]++;
}
int index=0;
for (int i=0;i<=oldAge;i++)
{
for(int j=0;j<timeOfAge[i];j++){
ages[index] = i;
index++;
}
}
}
int main33()
{
int age[] = {15,16,72,25,21,23,22,21,22,21,23,25,29,30,26};
int length = sizeof(age)/sizeof(age[0]);
SortAge(age,length);
for (int i=0; i<length;i++)
printf("%d, ",age[i]);
return 0;
}
//******************--字符串中去除重复的字符--改变原字符顺序*********************
char* SortString(char *str, int length){
if(str == NULL || length <= 0)
return NULL;
const int numbers = 52;
int mychar[numbers+1];
for(int i=0;i<=numbers;i++)
mychar[i] = 0;
for(int i=0; i<length-1; i++){
int age = str[i]-'A';
if(age <0 || (age >26 && age<32) || age > 58){
printf("input age wrong!");
return NULL;
}
if (age >=32)
age -= 6;
mychar[age]++;
}
int index = 0;
for (int i=0;i<=numbers;i++)
{
if (mychar[i] == 1)
{
if (i<26)
str[index] = i+'A';
else if(i<52)
str[index] = i+'A'+6;
index++;
}
}
for(int i=index;i<length;i++)
str[i] = '\0';
return str;
}
int main341()
{
char str[] = "abcabsdasdaAAA";
int length = sizeof(str)/sizeof(str[0]);
char* mystr = SortString(str,length);
if(mystr == NULL)
return -1;
int length2 = strlen(mystr);
for (int i=0; i<length2;i++)
printf("%c",mystr[i]);
printf("\n");
return 0;
}
//******************--字符串中去除重复的字符--未改变原字符顺序*********************
char* SortString2(char *str, int length){
if(str == NULL || length <= 0)
return NULL;
const int numbers = 26;
int mychar[numbers];
char* tmp = (char*)malloc(sizeof(char)*length);
memset(tmp,0,length);
for(int i=0;i<numbers;i++)
mychar[i] = 0;
for(int i=0; i<length-1; i++){
tmp[i] = str[i];
int s = str[i]-'a';
if(s <0 || s > 25){
printf("input age wrong!");
return NULL;
}
mychar[s]++;
}
int index = 0;
for (int i=0;i<length;i++)
{
if(mychar[str[i]-'a'] == 1){
str[index] = tmp[i];
index++;
}
}
for(int i=index;i<length;i++)
str[i] = '\0';
return str;
}
int main342()
{
char str[] = "abcabsdasdathfgr";
int length = sizeof(str)/sizeof(str[0]);
char* mystr = SortString2(str,length);
if(mystr == NULL)
return -1;
int length2 = strlen(mystr);
for (int i=0; i<length2;i++)
printf("%c",mystr[i]);
printf("\n");
return 0;
}
//******************--求旋转数组中的最小值--*********************
int minNum(int *numbers, int length)
{
if(numbers == NULL || length <=0)
return -1;
int res = numbers[0];
for(int i=1;i<length;i++){
if(res > numbers[i])
res = numbers[i];
}
return res;
}
int Min(int *numbers, int length)
{
if(numbers == NULL || length <=0)
return -1;
int index1 = 0;
int index2 = length-1;
int mid = index1;
while(numbers[index1]>=numbers[index2])
{
if(index2-index1 == 1){
mid = index2;
break;
}
mid = (index1+index2)/2;
if(numbers[index1]==numbers[mid] && numbers[index2] == numbers[mid])
return minNum(numbers,length);
if(numbers[mid]>numbers[index1])
index1 = mid;
else if(numbers[mid] <= numbers[index2])
index2 = mid;
}
return numbers[mid];
}
int main36()
{
int numbers[] = {1,1,1,0,1};
int length = sizeof(numbers)/sizeof(numbers[0]);
int res = 0;
res = Min(numbers, length);
printf("%d\n",res);
return 0;
}
//******************--动态规划问题---剪绳子问题--*********************
int maxProductAfterCutting_solution(int num)
{
if(num<2)
return 0;
if(num==2)
return 1;
if(num==3)
return 2;
int* product = new int[num+1];
product[0] = 0;
product[1] = 1;
product[2] = 2;
product[3] = 3;
int max = 0;
for (int i=4;i<=num;i++)
{
max = 0;
for (int j=1;j<=i/2;j++)
{
int prod = product[j]*product[i-j];
if(prod>max)
max = prod;
//product[i] = max;
}
product[i] = max;
}
max = product[num];
return max;
}
//******************-贪心算法---剪绳子问题--*********************
int maxProductAfterCutting_solution2(int num)
{
if(num<2)
return 0;
if(num==2)
return 1;
if(num==3)
return 2;
int timeOf3 = num/3;
if (num%3 == 1)
timeOf3--;
int timeOf2 = (num-3*timeOf3)/2;
return (int)(pow((double)3,timeOf3)) * (int)(pow((double)2,timeOf2));
}
int main35()
{
int num = 10;
int res = 0;
res = maxProductAfterCutting_solution(num);
printf("%d\n",res);
int res2 = maxProductAfterCutting_solution2(num);
printf("%d\n",res2);
return 0;
}
//******************--求数字对应的二进制中1的个数--*********************
//方法一
int NumberOf1(int num)
{
int count = 0;
unsigned int flag = 1;
while(flag){
if(num & flag)
count ++;
flag = flag << 1;
}
return count;
}
//方法二
int NumberOf1_low(int num)
{
int count = 0;
while(num){
count ++;
num = (num-1)#
}
return count;
}
//******************--判断一数字是否为2的次方--*********************
bool PowOf2(int num)
{
if(num&(num-1))
return false;
else
return true;
}
//******************-求一个数到另一个数改变的步数-*********************
int changeStride(int num1, int num2)
{
//求一个数到另一个数改变的步数,先将两个数异或,再求结果中1的个数
return NumberOf1(num1^num2);
}
int main37()
{
int number = 8;
int res = 0;
res = NumberOf1(number);
printf("%d\n",res);
int res2 = NumberOf1_low(number);
printf("%d\n",res2);
bool res3 = PowOf2(number);
printf("%d\n",res3);
int res4 = changeStride(10,13);
printf("%d\n",res4);
return 0;
}
//******************-数值的整数次方-*********************
//方法一
double PowerWithUnsignedExponent(double base, int exponent)
{
double res = 1.0;
for (int i=0;i<exponent;i++)
res *= base;
return res;
}
//方法二
double PowerWithUnsignedExponent2(double base, int exponent)
{
if(exponent==0)
return 1.0;
if(exponent == 1)
return base;
double res = PowerWithUnsignedExponent2(base, exponent>>1);
res *= res;
if((exponent & 0x1) == 1)
res *= base;
return res;
}
double Power(double base, int exponent)
{
double res = 0;
if(abs(base-0.0)<1e-6 && exponent<0)
return 0.0;
unsigned int absExponent = (unsigned int)exponent;
if(exponent <0)
absExponent = (unsigned int)(-exponent);
//res = PowerWithUnsignedExponent(base,absExponent);
res = PowerWithUnsignedExponent2(base,absExponent);
if(exponent<0)
res = 1.0/res;
return res;
}
int main38()
{
double base = 2;
int exponent = -2;
double res = 0;
res = Power(base, exponent);
printf("%lf\n",res);
return 0;
}
//******************-从1打印到最大的n位数-*********************
void PrintNumber(char* number)
{
if(number == NULL)
return;
bool flag = false;
int len = strlen(number);
for(int i=0;i<len;i++)
{
if(!flag &&number[i] != '0')
flag = true;
if(flag)
printf("%c",number[i]);
}
printf(", ");
}
//方法一
bool Increment(char* number)
{
if(number==NULL)
return false;
bool nFlow = false;
int nTakeOver = 0;
int len = strlen(number);
for (int i=len-1;i>=0;i--)
{
int nSum = number[i] - '0' + nTakeOver;
if(i == len-1)
nSum++;
if(nSum>=10){
if(i==0){
nFlow = true;
//return true;
}
else{
nSum -= 10;
nTakeOver = 1;
number[i] = '0' + nSum;
}
}
else{
number[i] = '0' + nSum;
break;
}
}
return nFlow;
}
void Print1ToMaxOfN(int n)
{
if(n<=0)
return;
char *number = new char[n+1];
memset(number,'0',n);
number[n] = '\0';
while(!Increment(number)){
PrintNumber(number);
}
delete []number;
}
//方法二:递归
void Print1ToMaxOfN2_Recu(char* number, int n, int index)
{
if(index == n-1){
PrintNumber(number);
return;
}
for(int i=0;i<10;i++)
{
number[index+1] = i + '0';
Print1ToMaxOfN2_Recu(number,n,index+1);
}
}
void Print1ToMaxOfN2(int n)
{
if(n<=0)
return;
char* number = new char[n+1];
number[n] = '\0';
for(int i=0;i<10;i++)
{
number[0] = i + '0';
Print1ToMaxOfN2_Recu(number,n,0);
}
delete []number;
}
int main39()
{
Print1ToMaxOfN(2);
printf("\n");
Print1ToMaxOfN2(2);
return 0;
}
int GetNumberOfK2(int *a,int start, int end, int i)
{
if(start == end)
return a[start];
int index = Partition(a,end+1,start,end);
int k = index - start;
if(i == k)
return a[index];
else if(i<k)
return GetNumberOfK2(a, start, index-1, i);
else
return GetNumberOfK2(a, index+1, end, i-k);
}
int GetNumberOfK(int *a,int start, int end, int k)
{
if(start == end)
return a[start];
int index = Partition(a,end+1,start,end);
while(index != k-1){
if(index>k-1)
index = Partition(a, end+1,start, index-1);
else
index = Partition(a, end+1,index+1, end);
}
return a[index];
}
int main390()
{
int a[] = {1,4,78,1,2,46,34,24};
int ret = GetNumberOfK2(a,0,7,5);
printf("%d\n",ret);
return 0;
}