字符逆序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;
}
}