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/