C/C++每日一练(20230305)

简介: C/C++每日一练(20230305)

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.csdn.net/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。


输出

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


样例输入

1. 4
2. 1 2 3 4
3. 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

目录
相关文章
|
Linux 监控 Ubuntu
Linux 终端操作命令(1)
Linux 终端操作命令(1)
231 1
Linux 终端操作命令(1)
|
算法 Java Go
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
206 1
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
|
Linux 监控 Shell
Linux 终端命令之文件浏览(4) head, tail
Linux 终端命令之文件浏览(4) head, tail
200 0
Linux 终端命令之文件浏览(4) head, tail
|
Shell Linux 机器学习/深度学习
Linux 终端操作命令(3)内部命令用法
Linux 终端操作命令(3)内部命令用法
227 0
Linux 终端操作命令(3)内部命令用法
|
Python Linux Ubuntu
Linux系统部署Python语言开发运行环境
Linux系统部署Python语言开发运行环境
522 0
Linux系统部署Python语言开发运行环境
|
Go Unix 开发者
Go语言time库,时间和日期相关的操作方法
Go语言time库,时间和日期相关的操作方法
391 0
Go语言time库,时间和日期相关的操作方法
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
220 0
力扣C++|一题多解之数学题专场(2)
|
Go 机器学习/深度学习 Rust
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
318 0
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
|
Java Go C++
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
150 0
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
|
Java Go C++
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
192 0
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort