c_普通编程练习(无序)_持续更新

4 篇文章 0 订阅
3 篇文章 0 订阅

来源:牛客网

题目描述
第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。

输入描述:
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。
输出描述:
如果偶数比奇数多,输出NO,否则输出YES。

示例1
输入
5
1 5 2 4 3
输出
YES

#include<stdio.h>
int main ()
{//the shorter,the better.
    int count,num,i,size;
    count=0;
    scanf("%d",&size);
    for(int i=0;i<size && ~scanf("%d",&num);i++){
        if(num%2==0) count++;
        else count --;
        }
        if(count>0){
            printf("NO");
        }else{
            printf("YES");
        }
    }
    

> 题目描述
查找一个数组的第K小的数,注意同样大小算一样大。 如  2 1 3 4 5 2 第三小数为3。
输入描述:
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000),再输入k。
输出描述:
输出第k小的整数。

```c
#include<stdio.h>
int main(){
    int n=0;
    int m=0;
    
    while(~scanf("%d",&n)){
        int cnt=0,seq,a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        int i;
        scanf("%d",&seq);
        for( i=0;i<n-1;i++){
            m=i;
            for(int j=i+1;j<n;j++){
                if(a[j]<a[m])  m=j;
            }
            if(m!=i)  {a[i]^=a[m]^=a[i]^=a[m];}
            if(i==0|| a[i]!=a[i-1])
                cnt++;
            if(cnt==seq)  break;
        }
        printf("%d",a[i]);
    }
}

题目描述
输入一系列整数,建立二叉排序树,并进行前序,中序,后序遍历。
输入描述:
输入第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。
输出描述:
可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
每种遍历结果输出一行。每行最后一个数据之后有一个空格。
输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int data;
	struct node* left;
	struct node* right;
}Node;//typedef --  Node  方便定义结点变量

typedef struct {
	Node* root;//只要知道树的根结点,就可以知道整棵树
}Tree;//这个结构作用:把树看成整体

void insert(Tree* tree,int value){//往树里插进value
	Node *node = (Node*)malloc(sizeof(Node));//把value打包成一个结点?????动态内存分配
	node ->data = value;
	node ->left = NULL;
	node ->right= NULL;
	
	if(tree ->root==NULL){
		tree->root =node;
	}
	else{
		Node* temp = tree -> root;//临时指针temp是插进来的value需比较的结点
		while(temp!=NULL){
            if(value == temp->data) return;
			if(value < temp->data){
				if(temp->left ==NULL){
					temp->left = node;
					return;
				}
				else{
					temp=temp->left;
				}
			}else{
				if(temp->right==NULL){
					temp ->right = node;
					return;
				}else{
					temp = temp->right;
				}
			}
		}
	}

}
void preorder(Node* node){//先序
	if(node!=NULL){
		printf("%d ",node->data);
		preorder(node -> left);
		preorder(node -> right);
}
}
void inorder(Node* node){//中序 --  都是从小到大排列 
	if(node!=NULL){
		inorder(node -> left);
		printf("%d ",node->data);
		inorder(node -> right);
	}
}
void posorder(Node* node){
	if(node!=NULL){
		posorder(node -> left);
		posorder(node -> right);
		printf("%d ",node->data);
}
}
int main(){
	int n,x;

    while(scanf("%d",&n)!=EOF){
        Tree tree;
  	    tree.root = NULL;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            insert(&tree,x);
        }
        preorder(tree.root);//查询先序 
        printf("\n");
        inorder(tree.root);
        printf("\n");
        posorder(tree.root);
        printf("\n");
    }
	return 0;
}

//method1 
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int data;
	struct node* left;
	struct node* right;
}Node;//typedef --  Node  方便定义结点变量

typedef struct {
	Node* root;//只要知道树的根结点,就可以知道整棵树
}Tree;//这个结构作用:把树看成整体

void insert(Tree* tree,int value){//往树里插进value
	Node *node = (Node*)malloc(sizeof(Node));//把value打包成一个结点?????动态内存分配
	node ->data = value;
	node ->left = NULL;
	node ->right= NULL;
	
	if(tree ->root==NULL){
		tree->root =node;
	}
	else{
		Node* temp = tree -> root;//临时指针temp是插进来的value需比较的结点
		while(temp!=NULL){
            if(value == temp->data) return;
			if(value < temp->data){
				if(temp->left ==NULL){
					temp->left = node;
					return;
				}
				else{
					temp=temp->left;
				}
			}else{
				if(temp->right==NULL){
					temp ->right = node;
					return;
				}else{
					temp = temp->right;
				}
			}
		}
	}

}
void preorder(Node* node){//先序
	if(node!=NULL){
		printf("%d ",node->data);
		preorder(node -> left);
		preorder(node -> right);
}
}
void inorder(Node* node){//中序 --  都是从小到大排列 
	if(node!=NULL){
		inorder(node -> left);
		printf("%d ",node->data);
		inorder(node -> right);
	}
}
void posorder(Node* node){
	if(node!=NULL){
		posorder(node -> left);
		posorder(node -> right);
		printf("%d ",node->data);
}
}
int main(){
	int n,x;

    while(scanf("%d",&n)!=EOF){
        Tree tree;
  	    tree.root = NULL;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            insert(&tree,x);
        }
        preorder(tree.root);//查询先序 
        printf("\n");
        inorder(tree.root);
        printf("\n");
        posorder(tree.root);
        printf("\n");
    }
	return 0;
}

//method 2 


#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int data;
	struct node* left;
	struct node* right;
}Node;//typedef --  Node  方便定义结点变量

Node* insert(Node* T,int x){
    if(T==NULL){
        T=(Node*)malloc(sizeof(Node));
        T->data = x;
        T->left = T->right=NULL;
        return T;
    }else if(x<T->data){
        T->left = insert(T->left,x);
    }else if(x>T->data){
        T->right = insert(T->right,x);
    }
    return T;
}
void preorder(Node* T){
    if(T){
        printf("%d ",T->data);
        preorder(T->left);
        preorder(T->right);
        
    }
}
void inorder(Node* T){
    if(T){
        
        inorder(T->left);
        printf("%d ",T->data);
        inorder(T->right);
        
    }
}
void posorder(Node* T){
    if(T){
        
        posorder(T->left);
        posorder(T->right);
        printf("%d ",T->data);
    }
}
int main(){
	int n,x;
    Node* T;
    
    while(scanf("%d",&n)!=EOF){
         T = NULL;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            T=insert(T,x);
        }
        preorder(T);//查询先序 
        printf("\n");
        inorder(T);
        printf("\n");
        posorder(T);
        printf("\n");
    }
	return 0;
}

题目描述
输入数组长度 n 输入数组 a[1…n] 输入查找个数m 输入查找数字b[1…m] 输出 YES or NO 查找有则YES 否则NO 。
输入描述:
输入有多组数据。
每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m,n<=100)。
输出描述:
如果在n个数组中输出YES否则输出NO。

#include<iostream>
using namespace std;
int main()
{
    int n,m,a[100],b[100];
    while(cin>>n)
    {
        int i,j;
        for(i=0;i<n;i++)
            cin>>a[i];
        cin>>m;
        for(j=0;j<m;j++)
            cin>>b[j];
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(b[i]==a[j])
                {
                   cout<<"YES"<<endl;
                    break;
                }
                if(j==n-1)
                    cout<<"NO"<<endl;
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值