1 给定一个句子(只包含字母和空格),将句子单词反序,单词间用空格间隔,前后没有空格
输入:"hello xiao mi"
输出:"mi xiao hello"
#include <stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
int ReverseWords(char *str, char *resultWords[])//记录单词个数及每个单词开始的首地址
{
int wordStart = 0;//标记单词开始位
int wordCount = 0;//计数单词个数
char *tempWord[256];//存放每个单词的其起始地址
for (int i = 0; str[i] != '\0'; i++)//遍历字符串,寻找单词
{
if (str[i] == ' ')//对空格进行标记,且将所有空格变为'\0',方便保存每一个单词
{
wordStart = 0;
str[i] = '\0';
}
else
{
if (wordStart == 0)//若前一个是空格
{
wordStart = 1;
tempWord[wordCount++] = &str[i];//存放单词开始的首地址
}
}
}
for (int j = 0; j < wordCount; j++)//遍历单词首地址,方便逆序输出
{
resultWords[j] = tempWord[wordCount - 1 - j];
}
return wordCount;
}
int main(void)
{
char str[] = { '\0' };
while (cin.getline(str, 256))
{
char *resultWords[256];//指针数组,用来存放结果单词的起始地址
int cnt = ReverseWords(str, resultWords);//调用函数
for (int i = 0; i < cnt; i++)
{
cout << resultWords[i] << ' ';
}
cout << endl;
}
system("pause");
return 0;
}
<span style="font-size:18px;">简便操作:将字符串完全反转,再反转单词!</span>
<span style="font-size:18px;"></span><pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
using namespace std;
#define N 256
int main()
{
char str[N] = { '\0' };
char temp;
cin.getline(str, 256);//输入一行字符串方式!!!
int j = strlen(str) - 1;
int i = 0, begin, end;
//全盘逆序输出
while(j>i)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
i = 0;
while (str[i])//遍历整个数组
{
if (str[i] != ' ')//找单词的起终位,并进行反转
{
begin = i;
while (str[i] && str[i] != ' ')
i++;
i = i - 1;
end = i;
}
while (begin < end)//单词调换位置
{
temp = str[begin];
str[begin] = str[end];
str[end] = temp;
begin++;
end--;
}
i++;
}
cout << str << endl;
system("pause");
return 0;
}
2 输入的第一行表示节点个数为n,节点的编号为0到n-1组成,下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号
输出树的高度,为一个整数。样例输入:
5
0 1
0 2
1 3
1 4
样例输出: 3
#include <iostream>
#include<stdlib.h>
using namespace std;
int main() {
int a, b, n, m = 0;
int tree[1000] = { 0 }; /* 最大结点数+1 */
scanf_s("%d", &n);
while (scanf_s("%d%d", &a, &b) > 0) //输入过程中,输入结束以EOF结束,输出结果
{
tree[b] = tree[a] + 1;
if (m < tree[b]) m = tree[b];
}
printf("%d", m + 1);
system("pause");
return 0;
}
3 手机号码分身功能:首先将手机号码中的每个数字加8取个位,然后使用对应大写字母代替("ZERO","TWO","THREE"......"NINE"),然后随机打乱这些字母,所生成的字符串即为电话号码对应的分身
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
while (cin >> num)
{
string str;
for (int i = 0; i<num; i++)
{
cin >> str;
int arr[10] = { 0 };
int len = str.length();
int a = 0;
while (a<len)
{
if (str.find('Z', a) != string::npos)
{
arr[0]++;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('O', a) != string::npos&&str.find('N', a) != string::npos&&str.find('E', a) != string::npos)
{
arr[1]++;
a = (str.find('O', a)<str.find('N', a)) ? str.find('N', a) : str.find('O', a);
a = (a<str.find('E', a)) ? str.find('E', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('T', a) != string::npos&&str.find('W', a) != string::npos&&str.find('O', a) != string::npos)
{
arr[2]++;
a = (str.find('T', a)<str.find('W', a)) ? str.find('W', a) : str.find('T', a);
a = (a<str.find('O', a)) ? str.find('O', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('T', a) != string::npos&&str.find('H', a) != string::npos&&str.find('R', a) != string::npos)
{
arr[3]++;
a = (str.find('T', a)<str.find('H', a)) ? str.find('H', a) : str.find('T', a);
a = (a<str.find('R', a)) ? str.find('R', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('F', a) != string::npos&&str.find('O', a) != string::npos&&str.find('U', a) != string::npos)
{
arr[4]++;
a = (str.find('F', a)<str.find('O', a)) ? str.find('O', a) : str.find('F', a);
a = (a<str.find('U', a)) ? str.find('U', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('F', a) != string::npos&&str.find('I', a) != string::npos&&str.find('V', a) != string::npos)
{
arr[5]++;
a = (str.find('F', a)<str.find('I', a)) ? str.find('I', a) : str.find('F', a);
a = (a<str.find('V', a)) ? str.find('V', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('S', a) != string::npos&&str.find('I', a) != string::npos&&str.find('X', a) != string::npos)
{
arr[6]++;
a = (str.find('S', a)<str.find('I', a)) ? str.find('I', a) : str.find('S', a);
a = (a<str.find('X', a)) ? str.find('X', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('S', a) != string::npos&&str.find('E', a) != string::npos&&str.find('V', a) != string::npos)
{
arr[7]++;
a = (str.find('S', a)<str.find('E', a)) ? str.find('E', a) : str.find('S', a);
a = (a<str.find('V', a)) ? str.find('V', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('E', a) != string::npos&&str.find('I', a) != string::npos&&str.find('G', a) != string::npos)
{
arr[8]++;
a = (str.find('E', a)<str.find('I', a)) ? str.find('I', a) : str.find('E', a);
a = (a<str.find('G', a)) ? str.find('G', a) : a;
a++;
}
else
{
a = len;
}
}
a = 0;
while (a<len)
{
if (str.find('N', a) != string::npos&&str.find('I', a) != string::npos&&str.find('E', a) != string::npos)
{
arr[9]++;
a = (str.find('N', a)<str.find('I', a)) ? str.find('I', a) : str.find('N', a);
a = (a<str.find('E', a)) ? str.find('E', a) : a;
a++;
}
else
{
a = len;
}
}
for (int j = 0; j<10; j++)
{
for (int k = 0; k<arr[j]; k++)
{
if (j<8)
{
cout << j + 10 - 8;
}
else
{
cout << j - 8;
}
}
}
cout << endl;
}
}
return 0;
}