[HW] OJ记录20题之二

本文列举了20道编程题目,涵盖了字符串排序、DNA序列分析、成绩排序、查找最长公共子串等算法挑战,旨在提升编程思维与字符串处理能力。每个题目附有简单的解题思路。
摘要由CSDN通过智能技术生成

1 查找第一个只出现一次的字符

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

bool findChar(char *str, char *ch);

int main()
{
    char str[100];
    while (cin.getline(str, 100))
    {
        char ch;
        if (findChar(str, &ch))
        {
            cout<<ch<<endl;
        }
        else
        {
            cout<<"."<<endl;
        }
    }
}

bool findChar(char *str, char *ch)
{
    int c[256] = {
  0};
    for (int i = 0; i < strlen(str); i++)
    {
        c[str[i]]++;
    }

    for (int i = 0; i < strlen(str); i++)
    {
        if (c[str[i]] == 1)
        {
            *ch = str[i];
            return true;
        }
    }

    return false;
}

2 字符串排序

编写一个程序,将输入字符串中的字符按如下规则排序。
规则1:英文字母从A到Z排列,不区分大小写。
如,输入:Type 输出:epTy
规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入:BabA 输出:aABb
规则3:非英文字母的其它字符保持原来的位置。
如,输入:By?e 输出:Be?y
样例:
输入:
A Famous Saying: Much Ado About Nothing(2012/8).
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

解题思路就是冒泡排序。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

void sort(char *str);
bool isChar(char c);

int main()
{
    char str[100];
    while (cin.getline(str, 100))
    {
        sort(str);
        cout<<str<<endl;
    }

    return 0;
}

void sort(char *str)
{
    for (int i = 0; i < strlen(str) - 1; i++)
    {
        for (int j = 0; j < strlen(str) - i - 1; j++)
        {
            int m = j;
            // 增加这两个while是因为除字母外的其他字符要保持原位
            while (!isChar(str[m]) && m > 0)
            {
                m--;
            }
            int n = j + 1;
            while (!isChar(str[n]) && n < strlen(str) - 1)
            {
                n++;
            }
            int nm = str[m] >= 97 ? str[m] : str[m] + 32;
            int nn = str[n] >= 97 ? str[n] : str[n] + 32;
            if (isChar(str[m]) && isChar(str[n]) && nm > nn)
            {
                char c = str[m];
                str[m] = str[n];
                str[n] = c;
            }
        }
    }
}

bool isChar(char c)
{
    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
    {
        return true;
    }
    return false;
}

3 DNA序列

输入字符串和子串长度,得到CG比例最高的子串。
(建立一个长度为子串长度的窗口,然后向右移动窗口)

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

void fun(char *dest, const char *src, int n);

int main()
{
    char str[100];
    while (cin.getline(str, 100))
    {
        int n;
        cin>>n;
        char *dest = new char[n+1];
        fun(dest, str, n);
        cout<<dest<<endl;
        delete [] dest;
    }

    return 0;
}

void fun(char *dest, const char *src, int n)
{
    int len = strlen(src);
    int i = 0;
    int max_count = 0;
    int max_left = 0;
    int count = 0;
    int left = 0;
    while (i < n)
    {
        if (src[i] == 'C' || src[i] == 'G')
        {
            count++;
        }
        i++;
    }
    max_count = count;

    while (i < len)
    {
        int b1 = (src[i] == 'C' || src[i] == 'G');
        int b2 = (src[i - n] == 'C' || src[i - n] == 'G');
        left = i - n + 1;
        count += b1 - b2;
        if (count > max_count)
        {
            max_count = count;
            max_left = left;
        }
        i++;
    }

    while (n--)
    {
        *dest++ = src[max_left++];
    }
    *dest = '\0';
}

4 整形数组合并

未排序的数组合并,合并时要去除重复值

#include <iostream>
using namespace std;

void CombineBySort(int* pArray1,int iArray1Num,int* pArray2,int iArray2Num,int* pOutputArray,int *iOutputNum);
void sort(int *nums, int len);
void print(int *nums, int len);

int main()
{
    int n1;
    while (cin>>n1)
    {
        int *array1 = new int[n1];
        int i1 = 0;
        while(i1 < n1)
        {
            cin>>array1[i1++];
        }

        int n2;
        cin>>n2;
        int *array2 = new int[n2];
        int i2 = 0;
        while(i2 < n2)
        {
            cin>>array2[i2++];
        }

        int *array = new int[n1+n2];
        int n;
        CombineBySort(array1, n1, array2, n2, array, &n);
        print(array, n);

        delete [] array1;
        delete [] array2;
        delete [] array;
    }
    return 0;
}

void CombineBySort(int* pArray1,int iArray1Num,int* pArray2,int iArray2Num,int* pOutputArray,int* iOutputNum)
{
    sort(pArray1, iArray1Num);
    sort(pArray2, iArray2Num);
    int i1 = 0;
    int i2 = 0;
    int index = 0;
    int n;
    if (pArray1[i1] < pArray2[i2])
    {
        n = pArray1[i1];
        i1++;
        pOutputArray[index++] = n;
    }
    else
    {
        n = pArray2[i2];
        i2++;
        pOutputArray[index++] = n;
    }

    while (i1 < iArray1Num && i2 < iArray2Num)
    {
        if (pArray1[i1] <= pArray2[i2])
        {
            n = pArray1[i1];
            i1++;
        }
        else
        {
            n = pArray2[i2];
            i2++;
        }
        if (n != pOutputArray[index-1])
        {
            pOutputArray[index++] = n;
        }
    }

    while (i1 < iArray1Num)
    {
        if (pArray1[i1] != pOutputArray[index-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值