CINTA学前准备

1.写一个插入排序的函数,即输入一个数组,完成排序。

#include<iostream>
using namespace std;
void func(int*, int);
void func(int* x, int n) {
    int i, j, t;
    for (i = 0; i < n; i++)
        for (j = i + 1; j < n; j++)
            if (x[i] < x[j]) {
                t = x[i];
                x[i] = x[j];
                x[j] = t;
            }
}

void main() {
    int a[100], i = 0;
    char b;
    while ((b = getchar()) != '\n') {
        cin >> a[i];
        i++;
        func(a, i);
    }
    for (int j = 0; j < i; j++) {
        cout << a[j] << " ";
    }
    system("pause");
    cout << endl;
}

2.完成一个函数,输入值为整数,输出该值的二进制。 

#include <iostream>
#include <bitset>
using namespace std;

void BinaryRecursion(int n)
{
    int a;
    a = n % 2;
    n = n>>1;
    if (n == 0)
        ;
    else
        BinaryRecursion(n);
    cout <<a;
}

int main() {
    BinaryRecursion(18);
    cout << endl;
    
    system("pause");
    return 0;
}

3.输入一个整数,判断其是否为素数。 

#include<iostream>
using namespace std;

int prime(int x)
{
    int n;
    for (n = 2; n <= x / 2; n++)
    {
        if (x % n == 0)
            return 0;

    }
    return 1;
}

int main()
{
    int a, b;
    cout << "please input 1 number:";
    cin >> a;
    if (a <= 1)
        cout << "输入有误";
    else
    {
        b = prime(a);
        {
            if (b == 0)
                cout << a << "是素数" << endl;
            else
                cout << a << "bu是素数" << endl;
        }
    }
    system("pause");
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值