简单题 分享给有问题的同学借鉴
#include <iostream>
#include <string>
using namespace std;
void P_4_13()
{
//1_99奇数和
int ans=0;
for(int i=1;i<100;i++)
{
if(i%2!=0){
ans+=i;
}
}
cout<<ans<<endl;
}
void P_4_14()
{
//计算字符串 字母个数和数字个数
//string s;
char s[1000];
cin>>s;
int words=0;
int number=0;
for(int i=0;s[i]!='\0';i++)
{
char a=s[i];
if(a>='a'&&a<='z'||a>='A'&&a<='Z') words++;
if(a>='0'&&a<='9') number++;
}
cout<<words<<","<<number<<endl;
}
int gcd(int a,int b)//欧几里得转展相除法
{
return b==0?a:gcd(b,a%b);
}
void P_4_16()
{
//辗转相除法
int a,b;
cin>>a>>b;
cout<<gcd(a,b)<<endl;
}
void P_4_19()
{
//猴子吃桃子
int peach=1;
for(int i=10;i>=1;i--)
{
peach=(peach+1)*2;
}
cout<<peach<<endl;
}
int fb(int a)//递归算法
{
if(a==2) return 2;
else if(a==1) return 1;
else return fb(a-1)+fb(a-2);
}
void P_4_20()
{
//Fibonacci
int a=1,b=2;
int t;
cout<<1<<" "<<2<<" ";
for(int i=3;i<=10;i++)
{
t=a+b;
a=b;
b=t;
cout<<t<<" ";
}
cout<<endl;
cout<<fb(10)<<endl;
}
void _12()
{
//n阶乘
int n;
cin>>n;
int ans=1;
while(n){
ans*=n--;
}
cout<<ans<<endl;
}
void _13()
{
//水仙花数
for(int i=100;i<1000;i++){
int a[3];
a[0]=i%10;//123%10=3
a[1]=i/10%10;//123 12%10=2
a[2]=i/100;
if(a[0]*a[0]*a[0]+a[1]*a[1]*a[1]+a[2]*a[2]*a[2]==i){
cout<<i<<" ";
}
}
cout<<endl;
}
void _14()
{
//十个数 求最大值
int max,num;
cin>>max;
int n=10-1;
while(n--)
{
cin>>num;
if(num>max) max=num;
}
cout<<max<<endl;
}
void _15()
{
double a=2,b=1,t,ans=0;//分母 分子
int n=2;
while(n--)
{
ans+=a/b;
t=a+b;
int tamp=a;
a=t;
b=tamp;
}
cout<<ans<<endl;
}
void _16()
{
//求 1 1+2 +1+2+3 + 1+2+3+4 +n 的 add·
int n=4;
int add=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
{
add+=j;
}
}
cout<<add<<endl;
}
void _16_dp()
{
int n=4;
int dp[11];
dp[0]=0;
int i;
for( i=1;i<=n;i++){
dp[i]=i;
}
for( i=1;i<=n;i++){
dp[i]=dp[i]+dp[i-1];
dp[0]+=dp[i];
}
cout<<dp[0]<<endl;
}
int main()
{
//P_4_13();
//P_4_14();
//P_4_16();
//P_4_19();
//P_4_20();
//_12();
//_13();
//_14();
_16();
_16_dp();
return 0;
}
本文作者:Author: 寒光博客
文章标题:[c++]第五周上机
本文地址:https://dxoca.cn/C/296.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
本文地址:https://dxoca.cn/C/296.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
为寒光同学点个赞!一直在坚持学习!
噗呲 我只是有空记录一下 不都是在学习嘛|´・ω・)ノ~~