//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
一、打印2-100之间的素数。
[cpp] view plain copy
intmain(intargc,constchar* argv[]) {
for(inti =2; i <100; i++) {
intr = isPrime(i);
if(r ==1) {
printf("%ld ", i);
}
}
return0;
}
intisPrime(intn){
inti, s;
for(i =2; i <= sqrt(n); i++)
if(n % i ==0)return0;
return1;
}
二、求两个整数的最大公约数
[cpp] view plain copy
intgcd(inta,intb) {
inttemp =0;
if(a < b) {
temp = a;
a = b;
b = temp;
}
while(b !=0) {
temp = a % b;
a = b;
b = temp;
}
returna;
}
三、查找字符串的位置
1、给定一个字符串,输出本字符串中只出现一次并且最靠前的那个字符的位置?如“abaccddeeef”,字符是b,输出应该是2。
char*strOutPut(char*);
intcompareDifferentChar(char,char*);
intmain(intargc,constchar* argv[]) {
char*inputStr ="abaccddeeef"
char*outputStr =strOutPut(inputStr);
printf("%c \n", *outputStr);
return0;
}
char*strOutPut(char*s) {
charstr[100];
char*p = s;
intindex =0;
while(*s !='\0') {
if(compareDifferentChar(*s, p) ==1) {
str[index] = *s;
index++;
}
s++;
}
return&str;
}
intcompareDifferentChar(charc,char*s) {
inti =0;
while(*s !='\0'&& i<=1) {
if(*s == c) {
i++;
}
s++;
}
if(i ==1) {
return1;
}else{
return0;
}
谢谢!!!