1.输入一个字符串,计算字符串中子串出现的次数
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 30
int main()
{
int len;
int i;
int j;
int flag = 0;
int count = 0;
char str1[MAX_SIZE];
char str2[MAX_SIZE];
char str_temp[MAX_SIZE];
printf("Please input a string:\n");
scanf("%s",str1);
printf("Please input another string:\n");
scanf("%s",str2);
len = strlen(str2);
for(i = 0; str1[i] != '\0'; i++)
{
for(j = 0; j < len; j++)
{
str_temp[j] = str1[i + j];
}
if(strcmp(str_temp, str2) == 0)
{
count++;
}
}
printf("\nThe number is:%d\n",count);
return 0;
}
2.有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.
#include <stdio.h>
#define MAX_SIZE 1024
int main()
{
int i;
int num;
int count = 0;
int temp;
int a[MAX_SIZE] = {0};
printf("Please input n:");
scanf("%d",&num);
temp = num;
for(i = 0; i < num; i++)
{
a[i] = i+1;
}
i = 0;
while(num > 1)
{
if(a[i] != 0)
{
count++;
}
if(count == 3)
{
a[i] = 0;
num--;
count = 0;
}
i++;
if(i == temp) //如何做到循环遍历这个数组,当i到末尾的时候再将它置为0,就又到了数组开头。
{
i = 0;
}
}
for(i = 0; i < temp; i++)
{
if(a[i] != 0)
{
printf("the result is:%d\n",a[i]);
break;
}
}
return 0;
}