C/C++每日一练(20230305) 整数分解、字符数组、找x

文章提供了三个编程问题:1)输入正整数进行7进制分解;2)判断字符数组是否包含另一个字符数组并返回起始索引;3)在给定数组中查找特定数值的下标。每个问题都有代码示例和输入输出示例。

目录

1. 整数分解 

2. 字符数组 ★

3. 找x ★


1. 整数分解 

输入一个正整数,将其按7进制位分解为各乘式的累加和。

示例 1:

输入:49
输出:49=7^2

示例 2:

输入:720
输出:720=6*7^0+4*7^1+2*7^3

代码:

#include<stdio.h>
#define X 7

int main()
{
    int i = 0;
    int mod, num;
    scanf("%d", &num);
    printf("%d=", num);
    while(num)
    {
        mod = num % X;
        num /= X;
        if(mod > 0)
            printf("%d*7^%d%c", mod, i, num > 0 ? '+' : '\n');
        i++;
    }
    return 0;
}

输入输出:

720
720=6*7^0+4*7^1+2*7^3


2. 字符数组

编写一个以两个字符数组作为输入的函数。
如果第二个数组包含在第一个数组中,则函数返回第一个数组中第二个数组开始的第一个索引。
如果第二个数组不被包含在第一个数组,然后函数应该return -1
输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’l’,’l’] 就 return 1.
输入 [’c’,’a’,’l’,’l’,’i’,’n’,’g’] 和 [’a’,’n’] 就 return -1.

以下程序实现了这一功能,请你补全空白处内容:

```c++
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char a[128], b[128];
    int numA, numB;
    cout << "请输入第一个数组元素个数:";
    cin >> numA;
    cout << "请输入第一个数组元素:";
    for (int i = 0; i < numA; ++i)
        cin >> a[i];
    cin.clear();
    cin.sync();
    cout << "请输入第二个数组元素个数:";
    cin >> numB;
    cout << "请输入第二个数组元素:";
    for (int i = 0; i < numB; ++i)
        cin >> b[i];
    int num = 0;
    string index;
    for (int j = 0; j < numB; j++)
    {
        for (int k = 0; k < numA; k++)
        {
            if (b[j] == a[k])
            {
                __________________;
            }
        }
    }
    if (num == numB)
    {
        cout << "第二个数组包含在第一个数组中" << endl;
        cout << "第一个数组中第二个数组开始的第一个索引为:" << index.substr(0, 1) << endl;
    }
    else
        cout << "第二个数组不被包含在第一个数组";
    system("pause");
    return 0;
}
```

出处:

https://edu.youkuaiyun.com/practice/27308140

代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    char a[128], b[128];
    int numA, numB;
    cout << "请输入第一个数组元素个数:";
    cin >> numA;
    cout << "请输入第一个数组元素:";
    for (int i = 0; i < numA; ++i)
        cin >> a[i];
    cin.clear();
    cin.sync();
    cout << "请输入第二个数组元素个数:";
    cin >> numB;
    cout << "请输入第二个数组元素:";
    for (int i = 0; i < numB; ++i)
        cin >> b[i];
    int num = 0;
    string index;
    for (int j = 0; j < numB; j++)
    {
        for (int k = 0; k < numA; k++)
        {
            if (b[j] == a[k])
            {
                index += to_string(k);
                num++;
                break;
            }
        }
    }
    if (num == numB)
    {
        cout << "第二个数组包含在第一个数组中" << endl;
        cout << "第一个数组中第二个数组开始的第一个索引为:" << index.substr(0, 1) << endl;
    }
    else
        cout << "第二个数组不被包含在第一个数组";
    system("pause");
    return 0;
}

输出:


3. 找x

题目描述

输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。

输入

测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。

输出

对于每组输入,请输出结果。

样例输入

4
1 2 3 4
3

样例输出

2

代码:

#include <iostream>
using namespace std;
int main()
{
    int n = 0;
    cin >> n;
    int *ptr = new (nothrow) int[n];
    for (auto i = 0; i < n; i++)
    {
        cin >> ptr[i];
    }
    int x = 0;
    cin >> x;
    auto j = 0;
    auto status = 0;
    for (; j < n; ++j)
    {
		if (ptr[j] == x)
		{
			status = 1;
			break;
		}
    }
    if (status == 0)
    {
        j = -1;
    }
    cout << j << endl;
    delete[] ptr;
    cin.get();
    cin.get();
    return 0;
}

输入输出:

4
1 2 3 4
3
2


🌟 每日一练刷题专栏

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

 收藏,你的青睐是我努力的方向! 

✏️ 评论,你的意见是我进步的财富!  

C/C++每日一练 专栏

Python每日一练 专栏


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值