1.

/*回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。花花非常喜欢这种拥有对称美的回文串,生日的时候她得到两个礼物分别是字符串A和字符串B。现在她非常好奇有没有办法将字符串B插入字符串A使产生的字符串是一个回文串。你接受花花的请求,帮助她寻找有多少种插入办法可以使新串是一个回文串。如果字符串B插入的位置不同就考虑为不一样的办法。
例如:
A = “aba”,B = “b”。这里有4种把B插入A的办法:
* 在A的第一个字母之前: "baba" 不是回文
* 在第一个字母‘a’之后: "abba" 是回文
* 在字母‘b’之后: "abba" 是回文
* 在第二个字母'a'之后 "abab" 不是回文
所以满足条件的答案为2

输入描述:
每组输入数据共两行。
第一行为字符串A
第二行为字符串B
字符串长度均小于100且只包含小写字母


#include<iostream>
#include<iostream>
#include<string>
using namespace std;
bool judge(string str)
{
int n=str.size();
int begin=0;
int end=n-1;
while(begin<=end){
if(str[begin]==str[end])
{
begin++;
end--;
}
else
break;

}
if (begin<end)
return false;
else
return true;
}
int main(){

string str1,str2;
int count=0;
cin>>str1;
cin>>str2;
int n=str1.size();
for(int i=0;i<=n;i++){
string str3;
str3=str1;
str3.insert(i,str2);

if(judge(str3)){count++;}
}
cout<<count<<endl;
return 0;


}*/
/*#include<iostream>
#include<string>
using namespace std;
bool judge(string str)
{
int begin=0;
int end=str.size()-1;
while(begin<=end)
{
if(str[begin]==str[end])
{
begin++;
end--;
}
else
break;
}
if(begin<end)
return false;
else
return true;
}

int main()
{
string str1,str2;
while(cin>>str1&&cin>>str2)
{
int cnt=0;
for(int i=0;i<=str1.size();i++)//i=0时插入最前边,i=str1.size()时插入最后边
{
string str3=str1;
str3.insert(i,str2);
if(judge(str3))
cnt++;
}
cout<<cnt<<endl;
}

}*/

2.


/*如果一个数字序列逆置之后跟原序列是一样的就称这样的数字序列为回文序列。例如:
{1, 2, 1}, {15, 78, 78, 15} , {112} 是回文序列,
{1, 2, 2}, {15, 78, 87, 51} ,{112, 2, 11} 不是回文序列。
现在给出一个数字序列,允许使用一种转换操作:
选择任意两个相邻的数,然后从序列移除这两个数,并用这两个数字的和插入到这两个数之前的位置(只插入一个和)。
现在对于所给序列要求出最少需要多少次操作可以将其变成回文序列。

输入描述:
输入为两行,第一行为序列长度n ( 1 ≤ n ≤ 50)
第二行为序列中的n个整数item[i] (1 ≤ iteam[i] ≤ 1000),以空格分隔。


输出描述:
输出一个数,表示最少需要的转换次数

输入例子:
4
1 1 1 3

输出例子:
2
*/
/*思想:首尾元素相比较,若首元素大于尾元素则前两个元素相加,反之,则后两个元素相加。
相等则继续向下比*/
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int *data=new int[n];
for(int i=0;i<n;i++){
cin>>data[i];
}
int begin=0;
int end=n-1;
int count=0;
while(begin<=end)
{
if (data[begin]==data[end])
{
begin++;
end--;
}
else if(data[begin]<data[end]){
data[begin+1]+=data[begin];
begin++;
count++;

}
else if(data[begin]>data[end]){
data[end-1]+=data[end];
end--;
count++;

}
}
cout<<count;
return 0;
}

 

posted on 2016-11-16 17:14  smilesky314  阅读(346)  评论(0编辑  收藏  举报