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

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

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;
}




输出:











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