C/C++每日一练(20230416) 数列第n项、整数转英文、数组最大值及索引

简介: C/C++每日一练(20230416) 数列第n项、整数转英文、数组最大值及索引

1. 求数列第n项值

求数列第n项值:1,2,3,6,11,20,37,68,125,230,.....例如:第7项为37,第9项为125。

出处:

https://edu.csdn.net/practice/25740845

代码:

#include <stdio.h>
int main(void) { 
    int n;
    printf("请输入n的值:");
    scanf("%d",&n);
    if(n==1){
        printf("第1项为1\n");
    }else if(n==2){
        printf("第2项为2\n");
    }else if(n==3){
        printf("第3项为3\n");
    }else{
        int f1=1,f2=2,f3=3;
        int i,fn;
        for(i=4;i<=n;i++){
            fn=f1+f2+f3;
            f1=f2;
            f2=f3;
            f3=fn;
        }
        printf("第%d项为%d\n",n,fn);
    }
    return 0;
}

输出:

略,这是扩展版的斐波那契数列:第4项起都是前三项之和。


2. 整数转换英文表示

将非负整数 num 转换为其对应的英文表示。

示例 1:

输入:num = 123

输出:"One Hundred Twenty Three"


示例 2:

输入:num = 12345

输出:"Twelve Thousand Three Hundred Forty Five"


示例 3:

输入:num = 1234567

输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"


示例 4:

输入:num = 1234567891

输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"


提示:

  • 0 <= num <= 2^31 - 1

出处:

https://edu.csdn.net/practice/25740846

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    const int Mod[3] = {1000000000, 1000000, 1000};
    string H[3] = {"Billion", "Million", "Thousand"},
           M[8] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"},
           L[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                    "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    void update(string &ans)
    {
        ans += ans == "" ? "" : " ";
    }
    string numberToWords2(int num)
    {
        if (num < 20)
            return L[num];
        string ans;
        if (num >= 100)
            ans += L[num / 100] + " Hundred", num %= 100;
        if (num == 0)
            return ans;
        else if (num < 20)
            update(ans), ans += L[num];
        else
        {
            update(ans), ans += M[num / 10 - 2], num %= 10;
            if (num == 0)
                return ans;
            else
                update(ans), ans += L[num];
        }
        return ans;
    }
    string numberToWords(int num)
    {
        if (num < 20)
            return L[num];
        string ans;
        for (int i = 0; i < 3; ++i)
            if (num >= Mod[i])
                update(ans), ans += numberToWords2(num / Mod[i]) + " " + H[i], num %= Mod[i];
        if (num)
            update(ans), ans += numberToWords2(num);
        return ans;
    }
};
int main()
{
  Solution s;
    cout << s.numberToWords(123) << endl;
    cout << s.numberToWords(12345) << endl;
  cout << s.numberToWords(1234567) << endl;
    cout << s.numberToWords(1234567891) << endl;
    return 0;
}

输出:

One Hundred Twenty Three

Twelve Thousand Three Hundred Forty Five

One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven

One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One


3. 数组中找出最大值及索引位置。

任意输入10数,存入数组,找出显示最大值,并且标记所在位置。

出处:

https://edu.csdn.net/practice/25740847

代码:

#include <stdio.h>
int main()
{
    int a[10],i,max,maxindex;
    for(i =0;i<10;i++)
        scanf("%d",&a[i]);
    max = a[0];  
    maxindex = 0;
    for (i =1;i<10;i++)
    {
        if(a[i] > max)
        {
            max = a[i];    
            maxindex = i;  
        }
    }
    printf("最大值%d,索引:%d\n",max,maxindex);
    return 0;
}

输出:


🌟 每日一练刷题专栏 🌟

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

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

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

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

主页:https://hannyang.blog.csdn.net/


目录
相关文章
|
5月前
|
搜索推荐 编译器 C语言
【C++核心】特殊的元素集合-数组与字符串详解
这篇文章详细讲解了C++中数组和字符串的基本概念、操作和应用,包括一维数组、二维数组的定义和使用,以及C风格字符串和C++字符串类的对比。
113 4
|
24天前
|
存储 算法 搜索推荐
【C++面向对象——群体类和群体数据的组织】实现含排序功能的数组类(头歌实践教学平台习题)【合集】
1. **相关排序和查找算法的原理**:介绍直接插入排序、直接选择排序、冒泡排序和顺序查找的基本原理及其实现代码。 2. **C++ 类与成员函数的定义**:讲解如何定义`Array`类,包括类的声明和实现,以及成员函数的定义与调用。 3. **数组作为类的成员变量的处理**:探讨内存管理和正确访问数组元素的方法,确保在类中正确使用动态分配的数组。 4. **函数参数传递与返回值处理**:解释排序和查找函数的参数传递方式及返回值处理,确保函数功能正确实现。 通过掌握这些知识,可以顺利地将排序和查找算法封装到`Array`类中,并进行测试验证。编程要求是在右侧编辑器补充代码以实现三种排序算法
36 5
|
3月前
|
机器学习/深度学习 存储 数据挖掘
Python 编程入门:理解变量、数据类型和基本运算
【10月更文挑战第43天】在编程的海洋中,Python是一艘易于驾驭的小船。本文将带你启航,探索Python编程的基础:变量的声明与使用、丰富的数据类型以及如何通过基本运算符来操作它们。我们将从浅显易懂的例子出发,逐步深入到代码示例,确保即使是零基础的读者也能跟上步伐。准备好了吗?让我们开始吧!
46 0
|
4月前
|
人工智能 C++
第十四届省赛大学B组(C/C++)接龙数列
第十四届省赛大学B组(C/C++)接龙数列
|
5月前
|
C++
C++(十一)对象数组
本文介绍了C++中对象数组的使用方法及其注意事项。通过示例展示了如何定义和初始化对象数组,并解释了栈对象数组与堆对象数组在初始化时的区别。重点强调了构造器设计时应考虑无参构造器的重要性,以及在需要进一步初始化的情况下采用二段式初始化策略的应用场景。
|
6月前
|
算法 C++
c++学习笔记04 数组
这篇文章是C++学习笔记4,主题是数组。
53 4
|
6月前
|
算法 数据处理 Python
Python中的集合的运算
Python中的集合的运算
|
6月前
|
C++ 索引
C++数组、vector求最大值最小值及其下标
C++数组、vector求最大值最小值及其下标
208 0
|
6月前
|
安全 编译器 C语言
C++入门-数组
C++入门-数组
|
4月前
|
存储 编译器 索引
Python 序列类型(2)
【10月更文挑战第8天】
Python 序列类型(2)