1、完数
int main(int argc, char *argv[]) {
//完数
int i,j;
int k;
int sum;
int n;
int res[256];
for(i = 2;i<1000;i++)
{
sum = 1;
res[0] = 1;
k = 0;
for(j = 2;j<=i/2;j++)
{
if(i % j == 0)
{
res[++k] = j;
sum += j;
}
}
if(sum == i)
{
printf("%d=%d",i,res[0]);
for(n=1;n<=k;n++)
printf("+%d",res[n]);
printf("\n");
}
}
return 0;
}
2、几种排序(✔)
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void insertion_sort(int num[],int n)
{
int i,j;
int temp;
for(i = 1;i<n;i++)
{
temp = num[i];
j = i;
while(j > 0 && temp > num[j - 1])
{
num[j] = num[j - 1];
j--;
}
num[j] = temp;
}
}
void selection_sort(int num[],int n)
{
int i,j;
int temp;
for(i = 0;i<n;i++)
{
temp = i;
for(j = i+1;j<n;j++)
{
if(num[j] > num[j - 1])
temp = j;
}
swap(&num[temp],&num[i]);
}
}
void bubble_sort(int num[],int n)
{
int i,j;
for(i = n - 1;i>0;i--)
{
for(j = 0;j<i;j++)
{
if(num[j] > num[j + 1])
swap(&num[j],&num[j + 1]);
}
}
}
3、删除字符串中指定字符(✔)
//利用hash表删除字符
char* deleteCharacters(char* res, char* target)
{
int hash[256] = {0};
int i;
for(i = 0;i< strlen(target);i++)
{
hash[target[i]]++;
printf("%d\n",hash[target[i]]);
}
int currindex = 0;
for(i = 0;i< strlen(res);i++)
{
if(hash[res[i]] == 0)
res[currindex++] = res[i];
}
res[currindex] = '\0';
return res;
}
int main()
{
char s[3] = "ac"; // 要删除的字母
char s2[5] = "acba"; // 目标字符串
printf("%s\n", deleteCharacters(s2, s));
return 0;
}
4、求矩阵对角元素的和(✔)
//求 N*N 阶矩阵对角线元素之和(N 为奇数):
int main()
{
int i=0,j=0;
int a[N][N];
int sum=0;
srand(time(NULL)); //srand()函数是随机函数rand()函数的种子函数
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a[i][j]=rand()%(N*N+1);
}
}
printf("打印随机%d阶矩阵:\n",N);
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%3d ",a[i][j]);
if(j==N-1)
printf("\n");
}
}
for(i=0;i<N;i++){
sum=sum+a[i][i];
}
printf("%d阶矩阵对角线元素之和为:%d",N,sum);
return 0;
}
5、二维数组输出杨辉三角(✔)
int main()
{
int i, j;
int a[10][10];
for(i = 0; i < 10; i++)
{
a[i][i] = 1;
a[i][0] = 1;
}
//start from third line
for(i = 2;i<10;i++)
{
for(j = 1;j<i;j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for(i = 0; i < 10; i++)
{
for(j = 0; j <= i; j++)
{
printf("%-4d",a[i][j]);
}
printf("\n");
}
return 0;
}
6、利用指针实现数组向右滚动(✔)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[20];
int i,n,offset;
//输入数组大小和数组内容
printf("Total numbers?\n");
scanf("%d",&n);
printf("Input %d numbers.\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
//输入滚动偏移量
printf("Set your offset.\n");
scanf("%d",&offset);
printf("Offset is %d.\n",offset);
//打印滚动前数组
print_arr(arr,n);
//滚动数组并打印
move(arr,n,offset);
print_arr(arr,n);
}
//打印数组
void print_arr(int array[],int n)
{
int i;
for(i=0;i<n;++i)
printf("%4d",array[i]);
printf("\n");
}
//滚动数组
void move(int array[],int n,int offset)
{
int *arr_end = array + n;
int last;
int *p;
while(offset != 0)
{
last = *(arr_end - 1);
for(p = arr_end - 1;p != array;p--)
{
*p = *(p - 1);
}
*p = last;
offset--;
}
}
7、约瑟夫环小游戏(✔)
#include <stdio.h>
#include <stdlib.h>
//有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
int main()
{
int n;
printf("input num of people: \n");
scanf("%d",&n);
int j;
int *a = (int*)malloc(sizeof(int)*n);
for(j = 0;j<n;j++)
a[j] = 0;
int count = 0;//number 3 off boat
int i = 0;//记录编号
int total = 0;//the number of people off boat
int index = 0;
int *res = (int*)malloc(sizeof(int)*n);
int *hash = (int*)malloc(sizeof(int)*n);//check who stayed on the boat
for(j = 0;j<n;j++)
res[j] = 0;
for(j = 0;j<n;j++)
hash[j] = 0;
while(total != n-1)
{
if(a[i] == 0)
count++;
if(count == 3)
{
a[i] = 1;
printf("no %d off boat\n",i+1);
res[index++] = i+1;
count = 0;
total++;
}
i++;
if(i == n)
i = 0;
}
for(j = 0;j<n;j++)
{
hash[res[j] - 1] = 1;
}
for(j = 0;j<n;j++)
{
if(hash[j] == 0)
printf("%d remain",j+1);
}
return 0;
}
*8、计算字符串中子串出现的次数 *
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//计算字符串中子串出现的次数
//手动法
int count_time(char *sub,char *s)
{
int count = 0;
int sub_len = strlen(sub);
int s_len = strlen(s);
printf("sub_len = %d\n",sub_len);
printf("s_len = %d\n",s_len);
int i = 0;
int index;
int flag;
int temp;
while(i < s_len)
{
flag = 0;
temp = i;
index = 0;
while(index < sub_len)
{
if(sub[index] != s[temp] )
flag = 1;
printf("s[%d] = %c\n",temp,s[temp]);
index++;
temp++;
}
printf("flag = %d\n",flag);
if(flag == 0)
{
count++;
i += sub_len;
}
else
i++;
}
return count;
}
int fun(char *s,char *t)
{
int t_len = strlen(t);
int count = 0;
char *p;
while((p = strstr(s,t)) != NULL)
{
count++;
s = p + t_len;
}
return count;
}
int main()
{
printf("no zuo no die,let me try try\n");
printf("%d\n",fun("dh123dh","dh"));
return 0;
}
9、链表操作
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
typedef struct Node{
int data;
struct Node *next;
}Node;
Node* createNode(int data)
{
Node *n = (Node*)malloc(sizeof(Node));
n->data = data;
n->next = NULL;
// printf("temp data: %d\n",n->data);
return n;
}
void insert(Node *head,Node *node)
{
Node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
//printf("test1: %d\n",head->data);
}
void insertFront(Node **head,Node *node)
{
node->next = *head;
*head = node;
}
void print_list(Node *head)
{
Node *temp = head;
// printf("temp = %d\n",temp->data);
while(temp != NULL)
{
printf("ress:%d\n",temp->data);
temp = temp->next;
}
}
void reverse(Node **head)
{
Node *curr = *head;
Node *pre = NULL,*next = NULL;
while(curr != NULL)
{
// printf("curr:%d\n",curr->data);
next = curr->next;
// printf("next:%d\n",next->data);
curr->next = pre;
pre = curr;
// printf("pre:%d\n",pre->data);
curr = next;
}
*head = pre;
}
int main()
{
Node *head = createNode(0);
int i;
for(i = 0;i<5;i++)
{
insertFront(&head,createNode(i));
}
print_list(head);
reverse(&head);
printf("reversed LinkList:\n");
print_list(head);
return 0;
}