1、打印0-100之间的素数
for (int i =0;i<100;i++){
int r = [self isPrime:i];
if(r==1){
NSLog(@"%d",i);
}
}
-(int)isPrime:(int)number{
for(int i=0;i<=sqrt(number);i++){
if(number%i==0) return0;
}
return 1;
}
2、求两个整数的最大公约数
int gcd(int a,int b){
int temp = 0;
if(a<b){
temp = a;
a = b;
b = temp;
}
while (b!=0) {
temp = a % b;
a = b;
b = temp;
NSLog(@"a:%d b:%d",a,b);
}
return a;
}
3、给定一个字符串,输出本字符串中只出现一次并且最靠前的那个字符的位置?如“abaccddeeef”,字符是b,输出应该是2
int main(int argc, const char * argv[]) {
char *inputStr = "abacddbeeef";
char *outputStr = strOutPut(inputStr);
NSLog(@"%c",*outputStr);
}
char *strOutPut(char *s){
char str[100];
char *p = s;
int index = 0;
while (*s != '\0') {
if(compareDifferentChar(*s, p)==1){
str[index] = *s;
index++;
}
s++;
}
return &str;
}
int compareDifferentChar(char c,char *s){
int i = 0;
while (*s != '\0' && i<=1) {
if (*s == c) {
i++;
}
s++;
}
if (i == 1) {
return 1;
}else{
return 0;
}
}
网友评论