189 8069 5689

C语言经典题型(二)-创新互联

文章目录
  • 一、次方
    • 1.编写一个函数,实现n的k次方(考虑负数)
  • 二、输入一个非负整数,返回组成它的数字之和
  • 三、交换完打印
    • 1. 迭代+temp法+数组
    • 2.用指针
    • 2.递归
  • 四、求二进制中一的个数
    • 4.1 循环按位比较法
    • 4.2 余2除2法
    • 4.3 自己退格
  • 五、获取并分别打印一个整数二进制序列中所有的偶数位和奇数位
  • 六、求两个整数m和n的二进制表达中,有多少个位(bit)不同
    • 6.1 循环比较
    • 6.2 寻找中间媒介
    • 6.3 自己退格
  • 七、获得月份天数
    • 7.1 数组
    • 7.2 switch语句
  • 八、进制转换
    • 8.1 递归
    • 8.2 数组实现
  • 九、删除指定字符

成都网站建设哪家好,找成都创新互联!专注于网页设计、成都网站建设、微信开发、成都小程序开发、集团成都企业网站建设等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:广告制作等众多领域,积累了大量丰富的经验,同时也获得了客户的一致称赞!
一、次方 1.编写一个函数,实现n的k次方(考虑负数)
#include#includedouble mul(int x, int y)
{if (y == 0)
	{return 1;
	}
	else if(y>0)
	{return x * pow(x, y - 1);
	}
	else
	{return 1.0 / pow(x, -y);
	}
}
int main()
{int n = 0;
	int k = 0;
	scanf("%d %d", &n, &k);
	double ret = mul(n, k);
	printf("%.2lf", ret);
	return 0;
}
二、输入一个非负整数,返回组成它的数字之和
#includeint DigitSum(int x)
{if (x >9)
		return DigitSum(x / 10) + x % 10;
	else
		return x;
}
int main()
{int num = 0;
	scanf("%d", &num);
	int ret = DigitSum(num);
	printf("%d", ret);
	return 0;
}
三、交换完打印 1. 迭代+temp法+数组
#include#includevoid string(char* ch,int sz)
{int left = 0;
	int right = sz - 1;
	char temp = 0;
	while (left<= right)
	{temp = ch[left];
		ch[left] = ch[right];
		ch[right] = temp;
		left++;
		right--;
	}
	printf("%s", ch);
}
int main()
{char ch[] = "abcdef";
	int sz = strlen(ch);
    string(ch,sz);     //返回值该用什么接收----不需要接收,地址直接是作用在上面的
	return 0;
}
2.用指针
#include#includevoid swap(char* ch,int x)
{int left = 0;
	int right = x - 1;
	while (left<= right)
	{char temp = 0;
		temp = *(ch + left);
		*(ch+left) = *(ch+right);
		*(ch + right) = temp;
		left++;
		right--;
	}

}
int main()
{char ch[] = "abcdef";
	int sz = strlen(ch);
	swap(ch,sz);
	printf("%s", ch);
	return 0;
}
2.递归
#include#includevoid swap(char ch[], int x)
{char temp = 0;
	temp = *ch;
	*ch = *(ch + x - 1);
	*(ch + x - 1) = '\0';
	if (strlen(ch + 1) >= 2)
	{swap(ch + 1,strlen(ch+1));
	}
	*(ch + x - 1) = temp;
}
int main()
{char ch[] = "abcdef";
	int sz = strlen(ch);
	swap(ch,sz);
	printf("%s", ch);
	return 0;
}
四、求二进制中一的个数 4.1 循环按位比较法
#includeint count_one(int input)
{int i = 0;
	int count = 0;
	for (i = 0; i< 32; i++)
	{if (((input >>i) & 1) == 1)
		{	count++;
		}
	}
	return count;
}
int main()
{int input = 0;
	scanf("%d", &input);
	int ret = count_one(input);
	printf("%d", ret);
	return 0;
}

注意:

4.2 余2除2法
int count_one(unsigned int input)
{int count = 0;
	while (input)
	{if (input % 2 == 1)
		{	count++;
		}
		input /= 2;
	}
	return count;
}
int main()
{int input = 0;
	scanf("%d", &input);
	int ret = count_one(input);
	printf("%d", ret);
	return 0;
}
4.3 自己退格
int count_one(int input)
{int count = 0;
	while (input)
	{input = input & (input - 1);
		count++;
	}
	return count;
}

int main()
{int input = 0;
	scanf("%d", &input);
	int ret = count_one(input);
	printf("%d", ret);
	return 0;
}
五、获取并分别打印一个整数二进制序列中所有的偶数位和奇数位
void Print(int n)
{int i = 0;
	printf("奇数位: ");
	for (i = 30; i >=0; i -= 2)
	{printf("%d ", (n >>i) & 1);
	}
	printf("\n");

	printf("偶数位: ");
	for (i = 31; i >= 1; i -= 2)
	{printf("%d ", (n >>i) & 1);
	}
	printf("\n");
}

int main()
{int n = 0;
	scanf("%d", &n);
	Print(n);//打印n的2进制中的所有奇数位和所有的偶数位
	return 0;
}
六、求两个整数m和n的二进制表达中,有多少个位(bit)不同 6.1 循环比较
#includeint is_different(int m,int n)
{int count = 0;
	int i = 0;
	for (i = 0; i< 32; i++)
	{if (((m >>i) & 1) != ((n >>i) & 1))
		{	count++;
		}
	}
	return count;
}
int main()
{int m = 0;
	int n = 0;
	scanf("%d %d", &m,&n);
	int ret = is_different(m,n);
	printf("%d", ret);
	return 0;
}
6.2 寻找中间媒介
#includeint is_different(int m, int n)
{int tmp = m ^ n;
	int count = 0;
	int i = 0;
	for (i = 0; i< 32; i++)
	{if (((tmp >>i) & 1) == 1)
		{	count++;
		}
	}
	return count;
}

int main()
{int m = 0;
	int n = 0;
	scanf("%d %d", &m,&n);
	int ret = is_different(m,n);
	printf("%d", ret);
	return 0;
}
6.3 自己退格
#includeint is_different(int m, int n)
{int count = 0;
	int tmp = m ^ n;
	while (tmp)
	{tmp = tmp & (tmp - 1);
		count++;
	}
	return count;
}
int main()
{int m = 0;
	int n = 0;
	scanf("%d %d", &m,&n);
	int ret = is_different(m,n);
	printf("%d", ret);
	return 0;
}
七、获得月份天数 7.1 数组
int get_days_of_month(int y, int m)
{int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };
    //                1  2  3  4  5  6  7  8  9  10 11 12
    int day = days[m];
    if ((m == 2) && (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)))
    {day++;
    }
    return day;
}

int main() {int y = 0;
    int m = 0;
    while (scanf("%d %d", &y, &m) == 2)
    {int ret = get_days_of_month(y, m);
        printf("%d\n", ret);
    }

    return 0;
}
7.2 switch语句
int get_days_of_month(int y, int m)
{int day = 0;
    switch (m)
    {case 1: case 3: case 5:  case 7:  case 8:  case 10:  case 12:
        day = 31;
        break;
    case 4: case 6: case 9: case 11:
        day = 30;
        break;
    case 2:
    {day = 28;
        if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
            day++;
    }
    break;
    }
    return day;
}
八、进制转换 8.1 递归
#includevoid is_six_count(int input)
{if(input>5)
    {   is_six_count(input/6);
    }
       printf("%d",input%6);


}
int main()
{int input = 0;
    scanf("%d",&input);
    is_six_count(input);
    return 0;
}
8.2 数组实现
int main()
{int arr[20] = {0};
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    while(n)
    {arr[i++] = n%6;
        n/=6;
    }
    for(--i; i>=0; i--)
    {printf("%d", arr[i]);
    }
    
    return 0;
}
九、删除指定字符
#includevoid is_delect(int* arr, int delect, int sz)
{int i = 0;
    int j = 0;
    for (i = 0; i< sz; i++)
    {if (arr[i] == delect)
        {arr[j++] = arr[i];
        }
    }
    for (i = 0; i< j; i++)
    {printf("%d", arr[i]);
    }
}
int main()
{int num = 0;
    scanf("%d", &num);

    int i = 0;
    int arr[50] = 0;
    for (i = 0; i< num; i++)
    {scanf("%d", &arr[i]);
    }
    int delect = 0;
    scanf("%d", &delect);
    is_delect(arr, delect, num);
    return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享名称:C语言经典题型(二)-创新互联
文章来源:http://jkwzsj.com/article/egjcj.html

其他资讯