1).定义大小为100的整型数组, 使用随机函数给数组元素赋值。数值范围1..100, 并且排序,使用冒泡排随机函数
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 100
void format(int arr[]);
void Print(int arr[]);
void sort(int arr[]);
void Swap(int *ap, int *bp);
int main()
{
int arr[SIZE] = { 0 };
srand((unsigned int)time(NULL));
format(arr);
sort(arr);
Print(arr);
system("pause");
return 0;
}
void format(int arr[])
{
for (int i = 0; i < SIZE; i++)
{
arr[i] = rand()%100+1;
}
}
void Print(int arr[])
{
int count = 0;
for (int i = 0; i < SIZE; i++)
{
printf("%-3d ", arr[i]);
count++;
if (count % 10 == 0)
{
printf("\n");
}
}
printf("\n\n");
}
void sort(int arr[])
{
for (int i = SIZE - 1; i > 0; i--)
{
bool tag = true; //优化
for (int j = 0; j < i; j++)
{
if (arr[j]>arr[j + 1])
{
Swap(&arr[j], &arr[j + 1]);
tag = false;
}
}
if (tag)
{
break;
}
}
}
void Swap(int *ap, int *bp)
{
if (*ap>*bp)
{
int tmp = *bp;
*bp = *ap;
*ap = tmp;
}
}
2)定义大小为100的整型数组,使用随机函数给数组元素赋值。数值的范围是1.. 100,并且不容许重复
普通方法
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 100
void format(int arr[]);
void Print(int arr[]);
void sort(int arr[]);
void Swap(int *ap, int *bp);
int main()
{
int arr[SIZE] = { 0 };
srand((unsigned int)time(NULL));
format(arr);
sort(arr);
Print(arr);
system("pause");
return 0;
}
void format(int arr[])
{
int i = 0;
while (i < SIZE)
{
int val = rand()%100+1;
int pos = Foundval(arr, i, val);
if (pos == -1)
{
arr[i] = val;
i++;
}
}
}
int Foundval(int arr[], int n, int val)
{
int pose = -1;
for (int i = 0; i < n; i++)
{
if (arr[i] == val)
{
pose = i;
return i;
}
}
return pose;
}
void Print(int arr[])
{
int count = 0;
for (int i = 0; i < SIZE; i++)
{
printf("%-3d ", arr[i]);
count++;
if (count % 10 == 0)
{
printf("\n");
}
}
printf("\n\n");
}
void sort(int arr[])
{
for (int i = SIZE - 1; i > 0; i--)
{
bool tag = true; //优化
for (int j = 0; j < i; j++)
{
if (arr[j]>arr[j + 1])
{
Swap(&arr[j], &arr[j + 1]);
tag = false;
}
}
if (tag)
{
break;
}
}
}
void Swap(int *ap, int *bp)
{
if (*ap>*bp)
{
int tmp = *bp;
*bp = *ap;
*ap = tmp;
}
}
查表法
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define SIZE 100
void Swap(int *ap, int *bp);
void Print(int arr[]);
void sort(int arr[]);
void format(int arr[], int brr[]);
int main()
{
int arr[SIZE] = { 0 };
int brr[SIZE + 1] = { 0 };
format(arr, brr);
sort(arr);
Print(arr);
system("pause");
return 0;
}
void format(int arr[], int brr[])
{
int i = 0;
while (i < SIZE)
{
int val = rand() % 100 + 1;
if (brr[val] == 0)
{
arr[i] = val;
i++;
brr[val] = 1;
}
}
}
void sort(int arr[])
{
for (int i = SIZE - 1; i > 0; i--)
{
bool tag = true; //优化
for (int j = 0; j < i; j++)
{
if (arr[j]>arr[j + 1])
{
Swap(&arr[j], &arr[j + 1]);
tag = false;
}
}
if (tag)
{
break;
}
}
}
void Swap(int *ap, int *bp)
{
if (*ap>*bp)
{
int tmp = *bp;
*bp = *ap;
*ap = tmp;
}
}
void Print(int arr[])
{
int count = 0;
for (int i = 0; i < SIZE; i++)
{
printf("%-3d ", arr[i]);
count++;
if (count % 10 == 0)
{
printf("\n");
}
}
printf("\n\n");
}
3)统计字符串中每个英文字符出现的次数, 不区分大小写, 只统计英文字符。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 26
int main()
{
int arr[SIZE] = { 0 };
char ch = 0;
while ((ch = getchar()) != EOF)
{
if (isalpha(ch))
{
if (isupper(ch))
{
tolower(ch);
}
arr[ch - 'a'] += 1;
}
}
for (int i = 0; i < SIZE; i++)
{
printf("%c=>%d \n",'a'+i, arr[i]);
}
system("pause");
return 0;
}
4)统计字符串中每个英文字符出现的次数, 区分大小写,只统计英文字符。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 26
int main()
{
int arr[SIZE] = { 0 };
int brr[SIZE] = { 0 };
char ch = 0;
while ((ch = getchar()) != EOF)
{
if (isalpha(ch))
{
if (isupper(ch))
{
brr[ch - 'A'] += 1;
}
else
{
arr[ch - 'a'] += 1;
}
}
}
for (int i = 0; i < SIZE; i++)
{
printf("%c=>%d ", 'a' + i, arr[i]);
printf("%c=>%d ", 'A' + i, brr[i]);
printf("\n");
}
system("pause");
return 0;
}