#include <stdbool.h>
bool xo (const char* str)
{
int x_con=0,o_con=0;
while(*str!='\0'){//这样也不会报错或者*str
if(*str=='x'||*str=='X'){//区分大小写
x_con++;
}
if(*str=='o'||*str=='O'){
o_con++;
}
str++;//推进循环
}
if(o_con==x_con)
return true;
else return false;
}
Training on Exes and Ohs | Codewars
#include <stddef.h>
// do not allocate memory for the output array
// assign values to preallocated array results
void smaller(size_t length, const int array[length], size_t results[length]) {
// <---- hajime!
size_t item=0;//注意题中定义的数据类型
for(int i=0;i<length-1;i++){//注意!题中函数直接传输组
for(int j=i+1;j<length;j++){
if(array[i]>array[j]){
item++;
}
}results[i]=item;//array[]是常量,不可修改存入results【】
item=0;
}results[length]=0;
}
Training on How many are smaller than me? | Codewars
#include <stddef.h>
int sum_no_duplicates(size_t length, const int array[length]) {
// <---- hajime!
//桶思想
int sum=0,a[100000]={0},item=0,len=(int)length;
for(int i=0;i<len;i++){
item=array[i];
a[item]+=item;
}
for(int j=0;j<len;j++){
if(a[j]==j){
sum+=j;
}
}return sum;
}
Training on Sum a list but ignore any duplicates | Codewars
#include <stdbool.h>
bool xor(bool a, bool b) {
if(a==b)
return false;
return true;
}
Training on Exclusive "or" (xor) Logical Operator | Codewars
int getAge(const char *inputString)
{
// return correct age (int). Happy coding :)
int item;
item=(int)(*inputString-(char)'0');
return item;
}
Training on Parse nice int from char problem | Codewars
#include<math.h>
int divisors(int n) {
// <---- hajime!
int sum=0;
for(int i=1;i<n+1;i++){
if(n%i==0){
sum++;
}
}
return sum++;
}
Training on Count the divisors of a number | Codewars
unsigned convert_lojban(const char *lojban) {
// <---- hajime!
int sum=0;
while(*lojban){
if(*lojban=='p') sum=sum*10+1;
if(*lojban=='r') sum=sum*10+2;
if(*lojban=='c') sum=sum*10+3;
if(*lojban=='v') sum=sum*10+4;
if(*lojban=='m') sum=sum*10+5;
if(*lojban=='x') sum=sum*10+6;
if(*lojban=='z') sum=sum*10+7;
if(*lojban=='b') sum=sum*10+8;
if(*lojban=='s') sum=sum*10+9;
if(*lojban=='n') sum=sum*10+0;
lojban+=2;
}
return sum;
}
Training on Lojban Numbers | Codewars
long long jie(long long a,long long b){
long long sum=1;
for(long long i=a;i>b;i--){
sum*=i;
}
return sum;
}
long long factorial_division(long long n, long long d) {
// Your turn !
long long sum=1;
if(n==0 && d==1) return 1;
if(d==1) return jie(n,1);
else return jie(n,d);
}
Training on Factorial division | Codewars
unsigned int countRedBeads (int n)
{
if(n<=1) return 0;
else return (n-1)*2;
}
Training on Simple beads count | Codewars
#include <stddef.h>
size_t str_count(const char *str, char letter) {
size_t sum=0;
//if(*str=='\0') return 0;
while(*str){
if(*str==letter) sum++;
str++;
}
return sum;
}