算法训练(一)

明明的随机数:
问题描述:明明想在学校找同学做调查问卷,他先用计算机生成N个1到1000之间的随机整数,对于其中重复的数字只保留一个,不同的数字对应着不同的学生的学号,把相同的去掉,然后把这些数从小到大排序,按照排好的顺序找同学做调查。

//数据去重、排序的操作,桶排序的思想
#include <stdio.h>
int main(void)
{
    int N, a[1001] = {0}, t;
    int count = 0;

    scanf_s("%d", &N);
    for(int i = 0; i<N; i++)
    {
        scanf("%d", &t);
        if(a[t] == 0)
        {
            a[t] = t;
            count++;
        }
    }
    printf("%d\n", count);
    for(int i = 0; i<1001; i++)
    {
        if(a[i]!=0)
            printf("%d\n", a[i]);
    }
    return 0;
}

十六进制转十进制

#include<stdio.h>
int main(void)
{
    char s[20];
    char * p;
    p = s;
    int n = 0, t = 0;
    gets(s);
    while(*p != '\0');
    {
        if(*p <= '9')
            t = *p - '0';
        else
            t = *p - 'A' + 10;
        n = n * 16 + t;
        p++;
    }
    printf("%d", n);
    return 0;
}
//去掉重复字符并排序
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

void sort1(string s)
{
    char tmp[100];
    int len = s.size();
    int i, j, count = 0;

    for(i = 0; i<len; i++)
    {
        for(j = i+1; j<len; j++)
        {
            if(s[i] == s[j])
                s[j] = '0';
        }
    }
    for(i = 0; i<len; i++)
    {
        if(s[i] >= 'a' && s[i] <= 'z')
            tmp[count++] = s[i];
    }
    sort(tmp, tmp + count);
    for(i = 0; i < count; i++)
        cout << tmp[i];
}

void main()
{
    string s;
    cin >> s;
    sort1(s);
}

字符串拆分:
连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组,不足8的在其后补0.

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

int main(void)
{
    char ch[10000];
    while(gets(ch))
    {
        strcat(ch, "0000000");
        for(int i = 0; strlen(&ch[i]) >= 8; i += 8)
        printf("%.8s\n", &ch[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值