T1107 输出亲朋字符串 #计蒜客 C++

T1107 输出亲朋字符串

题目描述

编写程序,求给定字符串 s 的亲朋字符串 s1。

亲朋字符串 s1定义如下:
给定字符串 s 的第一个字符的 ASCII 值加第二个字符的ASCII 值,得到第一个亲朋字符;给定字符串 s 的第二个字符的 ASCII 值加第三个字符的 ASCII 值,得到第二个亲朋字符;依此类推,直到给定字符串 s 的倒数第二个字符。亲朋字符串的最后一个字符由给定字符串 s 的最后一个字符 ASCII 值加 s 的第一个字符的 ASCII 值。

输入格式

输入一行,一个长度大于等于 22,小于等于 100 的字符串。字符串中每个字符的 ASCII 值不大于 63。

输出格式

输出一行,为变换后的亲朋字符串。输入保证变换后的字符串只有一行。

样例输入

1234

样例输出

cege
#include <iostream>
#include <string>
using namespace std;

int main(){
    
    
	char s1[105];
	string s;
	getline(cin,s); //
	int len = s.length(); 

	for(int i=0; i<len; i++){
    
    
		if(i>=0 && i<len-1){
    
    
			s1[i] = s[i]+s[i+1];
		}
		else 
			s1[i] = s[i]+s[0];
	}
	
	for(int i=0; i<len; i++)
		cout << s1[i];
	return 0;
}

总结:

一般使用 cin >> s 输入字符串,如果遇到需要输入空格的时候就要使用 getline() 或者 gets()
getline() 的头文件是 iostreamstringgets() 的头文件是 cstdio

猜你喜欢

转载自blog.csdn.net/qq_44524918/article/details/108651419