目录
给定一个字符串,反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
3.给定字符串,求字符串中第一多和第二多的字母,若两个字母一样多,输出前一个
给定一个字符串,反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例 1:
输入: “Let’s take LeetCode contest”
输出: “s’teL ekat edoCteeL tsetnoc”
string reverseWords(string str){
int len = str.size();
for (int i = 0; i < len; i++){
if (str[i] != ' '){
int j = i;
for (; j < len&&str[j] != ' '; j++);
reverse(str.begin() + i, str.begin() + j);
i = j ;
}
}
return str;
}
1.二分递归查找
#include <iostream>
#include<vector>
using namespace std;
int binarySearch(vector<int> &a, int low, int high, int value) //二分递归查找
{
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (a[mid] == value)
return mid;
if (a[mid] < value)
return binarySearch(a, mid + 1, high, value);
else
return binarySearch(a, low, mid - 1, value);
}
int main()
{
int N, value;
avector<int> a;
cin >> N; //输入
cin >> value;
for (int i = 0; i<N; i++) //定义向量vector<int> a代替数组,用push_back赋值
{
int num;
cin >> num;
a.push_back(num);
}
int result = binarySearch(a, 0, N - 1, value);
cout << result << endl; //输出
return 0;
}
2.最短回文串(后加)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//判断是否是回文串
int IsPalindrome(char* s, int len)
{
int i, j;
for (i = 0, j = len - 1; i <= j; i++, j--)
if (s[i] != s[j])
return 0;
return 1;
}
char * shortestPalindrome(char * s) //最短回文串判断
{
int len = strlen(s);
if (len == 0)
return s;
int i = len;
for (; i>0; --i)
{
int ret = IsPalindrome(s, i);
if (ret == 1) //找到从0开始的回文串
break;
}
if (i == 0)
i = 1;
int addLen = len - i; //新增长度
if (addLen == 0)
return s;
int newStrlen = len + addLen; //新串长度
char* newStr = (char*)malloc(sizeof(char)*newStrlen + 1);
int cpIndex = len - 1;
for (int i = 0; i<addLen; i++)
{
newStr[i] = s[cpIndex]; //将后几位倒置放入newStr
--cpIndex;
}
for (int i = 0; i<len; i++)
newStr[addLen + i] = s[i];
newStr[addLen + len] = 0;
return newStr;
}
//字符串倒置
char * daozhi(char * s)
{
int len = strlen(s);
int i, j, temp;
for (i = 0, j = len - 1; i <= j; i++, j--)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
return s;
}
int main()
{
int i, j;
char a[10000];
scanf_s("%s", a);
char *s1 = daozhi(a);
char *s2 = shortestPalindrome(s1);
printf("%s", s2);
system("pause");
return 0;
}
3.给定字符串,求字符串中第一多和第二多的字母,若两个字母一样多,输出前一个
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000];
gets_s(s);
int len = strlen(s), i,k[26] = { 0 };
if (s == NULL)
return NULL;
for (i = 0; i < len; i++) //利用哈希表表示每个字母的数量
k[s[i] - 'a']++;
int max1 = 0;
for (i = 0; i < 26; i++) //找出哈希表中第一多的
{
if (k[i]>k[max1])
max = i;
}
printf("第一多的字母是:%c\n", 'a' + max1);
k[max1] = 0; //将哈希表中第一多的位置置0,再找出第二多的
max2 = 0;
for (i = 0; i < 26; i++)
{
if (k[i]>k[max2])
max2 = i;
}
printf("第二多的字母是:%c\n", 'a' + max2);
system("pause");
return 0;
}
4.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int aaa(char* str)
{
int m = 0, k = 0;
if ((str[0]<'a' || str[0]>'z')&& (str[0]<'A' || str[0]>'Z'))
return 0;
for (int i = 0; i < strlen(str); i++)
{
if ((str[i]<'a' || str[i]>'z') && (str[i]<'A' || str[i]>'Z') && (str[i]<'0' || str[i]>'9'))
return 0;
if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A'&& str[i] <= 'Z')
m++;
if (str[i] >= '1' && str[i] <= '9')
k++;
}
if (k < 1 || m < 1)
return 0;
return 1;
}
int main()
{
char s[100];
int n,m;
scanf_s("%d\n", &n);
for (int i = 0; i < n; i++)
{
gets_s(s);
m = aaa(s);
if (m == 0)
printf("%s\n", "Wrong");
if (m==1)
printf("%s\n", "Accept");
}
system("pause");
return 0;
}
5.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int x, y;
scanf("%d %d\n", &x, &y);
char str[10000];
gets(str);
for (int i = 0; str[i]; i++){
if (str[i] == 'U')
y++;
if (str[i] == 'D')
y--;
if (str[i] == 'L')
x--;
if (str[i] == 'R')
x++;
}
printf("%d %d\n", x, y);
system("pause");
return 0;
}
6.
#include<iostream>
using namespace std;
class lipin //礼品类
{
public:
int price;
int weigh;
int v;
};
int main()
{
int n, m, k; //纪念品件数n,重量m,预算k
cin >> n >> m >> k;
class lipin li[100000];
class lipin temp[100000];
for (int i = 0; i < n; i++)
{
cin >> li[i].price >> li[i].weigh >> li[i].v;
}
for (int i = 0; i < n - 1; i++){ //冒泡排序,根据心动值v,价格price,yiji重量weigh的先后次序
for (int j = 0; j < n - i - 1; j++){
if (li[j].v == li[j + 1].v){
if (li[j].price == li[j + 1].price){
if (li[j].weigh > li[j + 1].weigh){
temp[j] = li[j]; li[j] = li[j + 1]; li[j + 1] = temp[j]; //交换
}
}
if (li[j].price > li[j + 1].price){
temp[j] = li[j]; li[j] = li[j + 1]; li[j + 1] = temp[j]; //交换
}
}
if (li[j].v < li[j + 1].v){
temp[j] = li[j]; li[j] = li[j + 1]; li[j + 1] = temp[j]; //交换
}
}
}
int z_weigh = 0;
int z_price = 0;
int z = 0;
while (z_weigh <= m&&z_price <= k){ //直接加,直到重量或价格超出
z_price += li[z].price;
z_weigh += li[z].weigh;
z++;
}
cout<<z-1<<endl;
system("pause");
return 0;
}
7.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int mid(vector<int>a,int i){ //每次多去除一个元素求中值
vector<int>c(a);
c.erase(c.begin() + i);
int n = c.size();
sort(c.begin(),c.end());
return c[n / 2];
}
int main(){
int n,k,m;
cin >> n;
vector<int>a;
for (int i = 0; i < n; i++){
cin >> k;
a.push_back(k);
}
for (int i = 0; i < n; i++){
m=mid(a, i);
if (i != n - 1)
cout << m<<endl;
else
cout << m;
}
system("pause");
return 0;
}
8.
#include<iostream>
#include<vector>
#include<list>
//#include<algorithm>
using namespace std;
int main(){
list<int>p1;
list<int>p2;
int n, m;
int num1, num2;
cin >> n; //输入p1
for (int i = 0; i < n; i++){
cin >> num1;
p1.push_back(num1);
}
cin >> m; //输入p2
for (int i = 0; i <m; i++){
cin >> num2;
p1.push_back(num2);
}
list<int>::iterator i = p1.begin(); //迭代器遍历链表
list<int>::iterator j = p2.begin();
vector<int> res;
while (i != p1.end() && j != p2.end()){
if (*i > *j)
i++;
else if (*i < *j)
j++;
else{
res.push_back(*i);
i++;
j++;
}
}
for (int i = 0; i < res.size(); i++){
if (i != res.size() - 1)
cout << res[i] << " ";
else
cout << res[i];
}
return 0;
}
9.求两个数的最大公约数
#include<iostream>'
using namespace std;
int main(){
int n,m,res;
cin >> n>>m;
if (m > n){ //m比n大,交换
int temp = m; m = n; n = temp;
}
else if (m == 0) //小的为0 返回大的
return n;
else{
while (n){ //余数为0 结束循环
int t = m;
m = n;
n = t%m;
}
}
cout << m << endl;
system("pause");
return 0;
}
10.求字符串最多子回文串数量(只有一个字符不是回文串)
#include<iostream>
#include<string>
using namespace std;
bool IsPalindromicStr(string str) { //判断是否是回文串
for (int i = 0, j = str.size()-1; i < j; i++,j--) {
if (str[i] != str[j]) return false;
}
return true;
}
int countPalindromicSubstr(string str) { //得到回文子串数量(字符串长度>1)
int count = 0;
for (int i = 0; i<str.size(); i++) {
for (int j = 2; j<=str.size() - i; j++) {
string substring = str.substr(i, j);
if (IsPalindromicStr(substring)) count++;
}
}
return count;
}
int main()
{
string testStr;
cout << "Enter string: " << endl;
cin >> testStr;
cout << countPalindromicSubstr(testStr) << endl;
system("pause");
return 0;
}