字符逆序0-字符逆序1-等差數(shù)列

字符逆序0

題目描述

將一個(gè)字符串str的內(nèi)容顛倒過來,并輸出。str的長(zhǎng)度不超過100個(gè)字符。 如:輸入“I am a student”,輸出“tneduts a ma I”。

輸入?yún)?shù):

inputString:輸入的字符串
返回值:
輸出轉(zhuǎn)換好的逆序字符串

示例

輸入
I am a student
輸出
tneduts a ma I

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    string str;
    int begin = 0, end = 0, pos = -1;
    while (getline(cin, str)) {
        reverse(str.begin(), str.end());
        cout << str << endl;
    }
}

字符逆序1

題目描述

將一個(gè)字符串str的每個(gè)單詞顛倒過來,并輸出。str的長(zhǎng)度不超過100個(gè)字符。 如:輸入“I am a student”,輸出“student a am I”。

輸入?yún)?shù):

inputString:輸入的字符串
返回值:
輸出轉(zhuǎn)換好的逆序字符串

示例

輸入
I am a student
輸出
student a am I

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    string str;
    int begin = 0, end = 0, pos = -1;
    while (getline(cin, str)) {
        reverse(str.begin(), str.end());
        begin = pos + 1;
        pos = str.find(' ');
        end = pos - 1;
        while(begin <end){
            swap(str[begin], str[end]);
            begin++, end--;
        }
        cout << str << endl;
    }
}

等差數(shù)列

題目描述

功能:等差數(shù)列 2,5,8,11,14.....
輸入:正整數(shù)N >0
輸出:求等差數(shù)列前N項(xiàng)和
返回:轉(zhuǎn)換成功返回 0 ,非法輸入與異常返回-1
本題為多組輸入,請(qǐng)使用while(cin>>)等形式讀取數(shù)據(jù)

輸入描述:

輸入一個(gè)正整數(shù)。

輸出描述:

輸出一個(gè)相加后的整數(shù)。

#include<iostream>
using namespace std;
int main() {
    int n;
    int a = 2, d = 3;
    while (cin >> n) {
        int ret = -1;
        ret = 2*n * a + n * (n - 1) * d;
        ret /= 2;
        cout << ret << endl;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容