c学习第7天
约瑟夫环
int people[ALL_NUM]; int pos, people_num = ALL_NUM, step = 0; for (pos = 0; pos < ALL_NUM ; pos++) { people[pos] = pos + 1; } pos = 0; while (people_num > 0) { if (people[pos]) { step++; } if (people[pos] && step == OUT_NUM) { printf("%d out \n", people[pos]); people[pos] = 0; people_num--; } pos++; if (pos == ALL_NUM) { pos = 0; } if (step == OUT_NUM) { step = 0; } }
有一段文本,将文本中的所有单词,存放到一个字符串数组中
int main () { char (*str1)[50] = malloc(50*10); int last_index = 0; int current_index = 0; int count = 10; int word_num = 0; char *str =" main Hello World by on Copyright (c) 2013年 All rights reserved."; while (*(str+current_index++) ==' ') { } printf("current = %d\n", current_index); last_index = current_index-1; do { if (*(str+current_index) == ' '||*(str+current_index) == '\0') { memcpy(str1[word_num], str+last_index, current_index-last_index); last_index = current_index; word_num++; if (word_num == count) { count+=10; str1 = realloc(str1, 50*count); } } }while (*(str+current_index++)); for (int i = 0; i < word_num; i++) { printf("%s ", str1[i]); } return 0; }