C/C++ 题目练习、题集

C语言:

1.命令行输入颜色名称,显示对应的颜色值。使用枚举变量的方法。

#include<stdio.h>
#include<string.h>

void find_color(char * my_color){
	enum colors{
		red = 1,
		black,
		pink,
		other
	};
	enum colors in_color = red;
	if(strcmp(my_color,"red") == 0){
		 in_color = red;
		}
	else if(strcmp(my_color,"black") == 0){
		in_color = black;
		}
	else if(strcmp(my_color,"pink") == 0){
		in_color = pink;
		}
	else {
		in_color = other;
		}
	printf("the number of %s: %d \n", my_color,in_color);
}
int main()
{
	char my_color[16] = {0};
	printf(" please input your favorite color\n");
	scanf("%s",&my_color);
	find_color(my_color);
	return 0;
}

 

2.通过控制台输入N个字符,统计每种字符出现次数,输出字符和出现次数.

#include<stdio.h>

int main()
{
	char tmp =0; int i,j,k= 0;
	int hash[127] = {0};
	char zimu[100] = {0};
	printf("please input some letters:\n");
	while(scanf("%c",&tmp) && tmp != '\r')
		{
			zimu[i] = tmp;
			i++;
			k = i;
		}
	
	for(i = 0;i<k;i++)
	{
		printf("%c",zimu[i]);
		hash[zimu[i]]++;//哈希表
	}
	printf("\nthe size of letters:%d\n",k);
	for(i = 0;i<127;i++)
	{
		if(hash[i] == 0)
		{
			continue;
		}
		printf("%c:%d\n",i,hash[i]);
		
	}
	return 0;
}

C++:

3.删除数组中重复的元素,并打印出来。

#include <iostream>
using namespace std;

void delete_Duplicates(int* nums, int numsSize){
    int j = numsSize;
    if(j > 1){
		
        j = 1;
        for(int i = 1; i < numsSize; i++){			
            if(nums[i] == nums[i-1]){
                continue;
            } else{
                nums[j] = nums[i];
                j++;
            }
        }
		
    }
	cout << "new numsSize:\n" <<j << "\n nums[]:\n";
	for(int k = 0;k < j;k++){
		cout << nums[k];
	}
	cout << endl;
}
int main(void)
{
   int numsSize = 10;
   int nums[] = {0,0,1,1,1,2,2,3,3,4};
   delete_Duplicates(nums,numsSize);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值