• 3个简单的编程水题


    //4、输入n个数,按从小到大进行排序并输出。
    #include<iostream>
    #include<algorithm>
    #define N 1000
    int a[N];
    using namespace std;
    int main()
    {
    int n;
    while (~scanf("%d",&n))
    {
    for (int i = 0; i < n; i++)
    {
    cin >> a[i];
    }
    sort(a, a + n);//直接调用sort函数
    for (int i = 0; i < n; i++)
    {
    cout << a[i] << " ";
    }
    cout << endl;
    }
    return 0;
    }

    //5、输入一个长整型的数,从低位起取出奇数位组成一个新的数输出。

    #include<stdio.h>
    int main()
    {
    long n,k,i,j,newnum;
    i = j = newnum = 0;
    scanf("%ld", &n);
    while (n)
    {
    k = n % 10;
    //i用来记录是newnum的第几位,是第几位的就乘以多少个10
    for ( j = 0; j < i; j++)
    {
    k *= 10;//为了按照原顺序输出,高位还是高位,低位还是低位
    }
    newnum += k;
    n /= 100;
    i++;//每位数字上数字的权值  

    }
    printf("%ld ", newnum);
    return 0;
    }

    //6.输入n个字符串,将它们按字母由小到大的顺序排列并输出
    #include<iostream>
    #include<string>
    #include<algorithm>

    using namespace std;
    //比较字符串中字母大小的函数,字符串可以直接比较大小
    bool cmp(const string &a, const string &b)
    {
    return a < b;
    }

    int main()
    {
    int n;
    while (cin>>n)
    {
    string *p = new string[n];
    for (int i = 0; i < n; i++)
    {
    cin >> p[i];
    }
    sort(p, p + n, cmp);
    for (int i = 0; i < n; i++)
    {
    cout << p[i]<<" ";
    }
    cout << endl;
    delete[]p;
    }
    return 0;
    }

    //另解,不使用sort而是使用冒泡法排序
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    int n;
    int i,j;
    cin >> n;
    string *pt = new string[n];
    string temp;
    for ( i = 0; i < n; i++)
    {
    cin >> pt[i];
    }
    //冒泡法排序,相邻的大的往后放
    for ( i = 0; i < n; i++)
    {
    for (j = 0; j < n - 1 - i;j++)
    {
    if (pt[j]>pt[j+1])
    {
    //同类string赋值,可以直接用“=”
    //交换
    temp = pt[j];
    pt[j] = pt[j + 1];
    pt[j + 1] = temp;
    }
    }

    }
    for (int i = 0; i < n; i++)
    {
    cout << pt[i] << " ";
    }
    cout << endl;
    delete[]pt;

    return 0;
    }

    参考博客:http://blog.csdn.net/hackbuteer1/article/details/6667026

    一生有所追!
  • 相关阅读:
    JSLint报错翻译
    vue 选城市三级联动
    npm 安装 sass-loader 失败的解决办法
    Metasploit Framework(6)客户端渗透(上)
    Metasploit Framework(5)弱点扫描
    Metasploit Framework(4)信息收集
    Metasploit Framework(3)Meterpreter
    Metasploit Framework(2)Exploit模块、Payload使用
    Metasploit Framework(1)基本命令、简单使用
    Kali学习笔记22:缓冲区溢出漏洞利用实验
  • 原文地址:https://www.cnblogs.com/BlueBlue-Sky/p/8469212.html
Copyright © 2020-2023  润新知