刷题记录03-HDOJ

目录

一、2011多项式求和--一题多解

二、1720进制转换

三、1062字符串反转


一、2011多项式求和--一题多解

解法一:直接求解

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
    
 int  m;
 int n;
 int a[101];

 while (cin>>m)
 {
     for(int i=0;i<m;i++)
     {
         cin>>n;
        a[i]=n;
     }
     for (int  i = 0; i < m; i++)
     {
          double sum=0;
         for(int j=1;j<=a[i];j++)
         {
             if ((j+1)%2==0)
             {
                 sum+=1/double(j);
             }
             else
             {
                 sum-=1/double(j);
             }

         }
      cout<<setiosflags(ios::fixed)<<setprecision(2)<<sum<<endl;   
     }
     
 }

 解法二:递归

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
double manysum(int n)
{
    if(n==1)return 1.00;
    else return (manysum(n-1)+(1.00/n)*pow(-1,n-1));

}
int main()
{
    int m;
    int a[101];
    while (cin>>m)
    {
        for (int i = 0; i <m; i++)
        {
            cin>>a[i];
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<manysum(a[i])<<endl;
        }
        
    }
    return 0;
}

二、1720进制转换

补充C++处理不同进制数的方法。

cin>>oct>>n; 输入八进制
cin>>hex>>n; 输入十六进制.

cout<<dec<<x+y;输出十进制

#include <iostream>
using namespace std;
int main(){
int x,y;
while (cin>>hex>>x>>y)
{
    cout<<dec<<x+y<<endl;
}
return 0;

}

三、1062字符串反转

 AC代码如下:

#include <iostream>
#include<string>
#include<cmath>
using namespace std;
int main(){
 int m;

 
 cin>>m;
 cin.get();    //接收\n
 while (m--)
 {
     string str;
     getline(cin,str);    //输入字符串
     int len=str.length();    //获得字符串长度
     int temp=0;    //用于统计每个单词中字母的个数
     for (int i = 0; i <=len; i++)
     {
        if (i==len)//当到改字符串结尾单词时
        {
            for (int j=i-1;j>=i-temp;j--)
            {
                cout<<str[j];//倒序输出
            }
            cout<<endl;//最后换行
            break;
            
        }
        if(!isspace(str[i]))    //如果不是空格 说明在一个单词内
        {
            ++temp;    //统计所在单词的字母个数
        }
        else
        {
            for (int j = i-1; j>=i-temp; j--)//j从i-1开始,进行倒序输出,一直到该单词的开头位 
                                             //置即i-temp
            {
                cout<<str[j];
            }
            cout<<" ";//在else这种情况中由于所处最后一个字符串数内存的是空格,则需要最后单独输 
                      //出空格
            temp=0;
        }
     }
     
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值