int pow(m,n)
{
int j,s=1;
for(j=1;j<=n;j++){
s*=m;
}
return s;
}
int main()
{
int arr[10] = {9,8,7,6,5,4,3,2,1};
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr,sz,sizeof(arr[0]),cmp);
}
int cmp(const void* a,const void* b)
{
return *(int*) b - *(int*) a;
}
void quickSort(int* arr, int low, int high) {
int i = low;
int j = high;
int temp = arr[low];
if (i >= j)
return;
while (i != j) {
while (i < j && arr[j]>=temp)
j--;
while (i < j && arr[i] <= temp)
i++;
if (i < j)
swap(arr[i], arr[j]);
}
swap(arr[low], arr[i]);
quickSort(arr, i + 1, high);
quickSort(arr, low, i - 1);
}
void select_sort(int* arr, int n)
{
int max,tmp;
for (int i = 0; i < n - 1; i++)
{
max = i;
for (j = i + 1; j < n ; j++)
{
if (arr[j] > arr[max])
{
max = j;
}
}
if (i != max)
{
tmp = arr[i];
arr[i] = arr[max];
arr[max] = tmp;
}
}
}
void bubble_sort(int* arr, int n)
{
int tmp;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] < arr[j+1]) {
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
void bubble_sort(void* arr, int sz, int width, int(*cmp)(const void* e1, const void* e2))
{
for (int i = 0; i < sz - 1; i++) {
for (int j = 0; j < sz - i - 1; j++) {
if (cmp((char*)arr + j * width, (char*)arr + (j + 1) * width)<0) {
swap((char*)arr + j * width, (char*)arr + (j + 1) * width,width);
}
}
}
}
int cmp(const void* e1, const void* e2) {
return *(int*)e1 - *(int*)e2;
}
void swap(char* bf1, char* bf2, int width) {
for (int i = 0; i < width; i++) {
char e = *bf1;
*bf1 = *bf2;
*bf2 = e;
bf1++;
bf2++;
}
}
int Isprime(int x) {
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0)
return 0;
return 1;
}
int gcd(int a, int b)
{
if(b == 0)
return a;
return gcd(b, a%b);
}
执行条件为真后不执行break会继续执行后面的case知道break;
打印的优先级问题。
const int *p;
int a = 4;
int b = 5;
p = &a;
*p = 5; /*错误,不能通过指针p来改变值*/
a = 5; /*正确,*p这时候也等于5*/
p = &b; /*常量指针可以指向其他的地址*/
int *const p;
int a = 4;
int *const p = &a;/*要马上初始化,之后无法赋值*/
p = &b;/*错误,无法赋值*/
*p = 5;/*正确*/
‘A’ = 65 / ‘a’ = 97 / ’0‘ = 48
if() 括号内为赋值语句 :表达式右侧为0 不执行语句 1;表达式右侧不为0 执行语句 1;
#include <stdio.h>
int main(void){
char m[40];
char n;
printf("please input first str:\n");
scanf("%s",m);
printf("you input str is :%s\n",m);
printf("input second char :\n");
scanf("%c",&n); //程序将未输入而直接输出
printf("now you input second char is :%c\n",n);
return 0;
}
不能用 == ; strcmp
到达文件末尾结束读取时返回非0值;
if (ferror(fp))
{
puts("I/O error when reading");
}
else if (feof(fp))
{
puts("End of the file reached successfully");
}