1.素数🌈🌈🌈🌈🌈🌈🌈🌈
1.
#include "stdio.h"
int Fun(int m);
int main()
{
int m, flag;
printf("Please input a number:\n");
do
{
scanf("%d", &m); //1
}
while (m < 2); //e1
flag = Fun(m); //3
if (flag) //1
printf("%d is a prime number\n", m);
else
printf("%d is not a prime number\n", m); //1
return 0;
}
int Fun(int m)//2
{
int i, flag = 1; //1
for (i = 2; i < m; i++) //1
{
if (m % i == 0) //1
{
flag = 0; //1
break;
}
}
return flag;//2
}
2.编写函数any(s1,s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2的字符,则返回-1。
```c
#include<stdio.h>
int any(char s1[], char s2[])
{
int i, j;
for(i=0; s1[i]!='\0'; i++)
{
for(j=0; s2[j]!='\0'; j++)
{
if(s1[i]==s2[j])
{
return i+1;
}
}
}
return -1;
}
int main(void)
{
char s1[]="jsadkf";
char s2[]="kdf";
int n;
n = any(s1, s2);
printf("%d\n", n);
return 0;
}
#include <stdio.h>
int any(char s1[], char s2[]) {
int i, j;
int ret = -1;
for(j=0; s2[j] != ‘\0’; j++)
for(i=0; s1[i] != ‘\0’; i++)
if(s1[i] == s2[j])
if(ret<0)
ret = i;
else if(i ret = i;
return ret;
}
int main()
{
char a[80], b[80];
printf(“Input str1:\n”);
gets(a); //1
printf(“Input str2:\n”);
gets(b); //1
int p = -1;
p = any(a, b); //2
printf(“The position is %d”,p);
return 0;
}