7.6上课笔记以及作业

#include
#include<assert.h>
#include
using namespace std;

//void Print(int br[10])

void Print_Ar(int* br, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%4d", br[i]);
if ((i + 1) % 10 == 0)
{
printf("\n");
}
printf("\n");
}
}

int FindValue(int* br, int n, int val)
{
assert(br != NULL);
int pos = n - 1;

while (pos>=0 && br[pos] != val)
{
	--pos;
}

return pos;

}
/* for (int i = 0; i < n; ++i)
{

	if (br[i] == val)
	{
		return i;
	}
	else
	{
		return 1;
	}

}*/

//1…100
void Init_Ar(int* br, int n)
{
assert(br != NULL);
int* table = new int[n + 1];
for (int i = 0; i <= n; ++i)
{
table[i] = 0;
}
int i = 0;
while(i<n)
{
int tmp = rand() % 100 + 1;
if (table[tmp] == 0)
{
table[tmp] = 1;
br[i] = tmp;
++i;
}

/*	int* p = NULL;
	int n = 0;
	scanf_s("%d", &n);//scanf("%d",&n);cin>>n;
	p = new int[n];
	//p=(int*)malloc{sizeof(int)*n};
	for (int i = 0; i < n; ++i)
	{
		p[i] = i;
	}
	for (int i = 0; i < n; ++i)
	{
		printf("%d", p[i]);
	}

	delete p;//C++
	//free (p);*/

}
delete[]table;
/*
{
int tmp = rand() % 100 + 1;
int pos = FindValue();
for (int i = 0; i < n; ++i)
{
br[i] = rand % 100 + 1;

}
*/

}
void Swap_Int(int*br[j],*br[j+1]) {

int tmp = br[j-1];
br[j - 1] = br[j];
br[j] = tmp;

}

void BubbleSort(int*br,int n)
{
assert(br != NULL);

	for (int i = 0; i < n; i++)
	{
		for (int j =1; j < n-i; j++)
		{
			if (br[j-1] > br[j])
			{
				Swap_Int(&br[j], &br[j + 1]);

			}
		}
	}
}

int main()
{
int i, j;
const int n = 100;
int ar[n] = { 0 };
sizeof(ar);
Init_Ar(ar, n);
Print_Ar(ar,n);
BubbleSort(ar, n);
Print_Ar(ar, n);

return 0;

}
#if 0
int main()
{
const int n = 10;
int ar[n] = { 12,23,34,45,56,67,78,89,90,100 };
int* p = &ar[0];

for (int i = 0; i < n; ++i)
{

	printf("%x=>%d\n", p, *p);
	p = p + 1;
	//Print("%d %d\n",ar[

—————————/————————-
#include
using namespace std;

int main()
{
char a = 0;
a = a | 0x80;//|为或,01取1;
a = a | 0x20;//二进制第五位从0变成1
a = a | 0x05;
//1010 0101
//1010 1010/且
//1010 0000
printf("%b\n", a);

return 0;

}
#if 0
int Get1Bit(int x)
{
int sum = 0;
while (x != 0)
{
x = x & (x - 1);
sum += 1;
}
/*

int a=1;
for(int i=0;i<32;i++)
{
	sum+=1;
	}*/

return sum;

}
int main()
{
int x = 23;
int sum = Get1Bit(x);
printf("%d\n", sum);
return 0;
}

while (x != 0)
{
if (x&0x01)
{
sum += 1;
}
x = x >> 1;
}
int main()
{

char a = 67;
char b = 0x54;
char c = 0;
c = a & b;
printf("%x \n", c);
c = a | b;
printf("%x \n", c);
c = a ^ b;
printf("%x \n", c);
a = ~b;
printf("%x \n", a);
return 0;

}

//103转换成二进制…1100111

//113 十六:71
int main()
{
int a = 10;//10
int b = 010;//8
int c = 0x10;//16
int d = 0b1010;//2

printf("%d %d %d %d\n", a, b, c, d);
printf("%o %o %o %o\n", a, b, c, d);
printf("%x %x %x %x\n", a, b, c, d);
return 0;

}
#endif

上一篇:2021-10-16最大公约数


下一篇:谈谈元宇宙