三道题分别如下:
1.输入一个由字母和空格组成的句子,可能会出现一个或多个空格,不区分大小写
(1)输出该句子中字母的个数;
(2)输出该句子中单词的个数;
(3)输出该句子中出现次数最多的字母和次数
例如:输入:This is my love
输出:
字母个数:12
单词个数:4
出现次数最多的字母: i s
出现次数:2
第一题简单。但是我写的有点复杂了。。应该直接用一个52长度的数组存字母的。。然后sort找到最大值就好了。
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
string tmp_s = "";
map<char, int> char_map;
int char_counter = 0, word_counter = 0;
char a[200];
gets(a);
string str = a;
for(int i = 0; i < str.size(); i++)
{
if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
{
char_counter++;
tmp_s.push_back(str[i]);
char_map[str[i]]++;
}
else
{
if(!tmp_s.empty())
{
word_counter++;
tmp_s = "";
}
}
}
if(!tmp_s.empty())
word_counter++;
cout<<"字母的个数:"<<char_counter<<endl;
cout<< "单词的个数:"<<word_counter<<endl;
int max = -1;
string max_char = "";
for(map<char, int>::iterator it = char_map.begin(); it != char_map.end(); it++)
{
if(max < it->second)
max = it->second;
}
for(map<char, int>::iterator it = char_map.begin(); it != char_map.end(); it++)
{
if(max == it->second)
max_char.push_back(it->first);
}
cout<<"句子中出现次数最多的字母是:"<<max_char<<endl;
cout<<"出现的次数为:"<<max<<endl;
return 0;
}
2.输入一个十二进制数(a和b代表10和11),按要求输出
(1)第一行输出每一位的十进制下的数
(2)第二行输出这个十二进制数的十进制数
(3)第三行输出转换后的十进制数在内存中的每一位二进制数(共四个字节,每个字节以空格隔开)
例如:输入:a2
输出:10 2
122
00000000 00000000 00000000 01111010
第二题:注意用移位操作计算二进制就好了!
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int numd = 0;//a代表转换后的10进制数
cin>>s;
for(int i = 0; i < s.size(); i++)
{
if(s[i] == 'a')
{
cout<<10<<" ";
numd = numd * 12 + 10;
}
else if(s[i] == 'b')
{
cout<<11<<" ";
numd = numd * 12 + 11;
}
else
{
cout<<s[i]<<" ";
numd = numd * 12 + s[i] - '0';
}
}
cout<<endl;
cout<<numd<<endl;
//计算二进制表示
string tmp = "";
for(int i = 0; i < 32; i++) //移位计算二进制
{
tmp.push_back(((numd & 1) + '0'));
numd = numd >> 1;
}
for(int i = 31; i >= 0; i--) //输出二进制表示
{
if(i != 31 && (i+1) % 8 == 0)
cout<<" ";
cout<<tmp[i];
}
return 0;
}
3.输入一个带除法运算的字符串,输出运算结果
(1)若能除尽,则直接输出
(2)若为循环小数,则输出前几位并用括号括起来
(3)若无法除尽,则保留小数用括号括起来
如输入:8/5 输出1.6
输入:1/3 输出0.(3)
输入:11/13 输出0.(846153)
这道题的主要难点是:如何找到循环体呢?
比如:
81 / 26 = 3.1(153846)
76 / 23 = 3.(3043478260869565217391)
1 / 3 = 0.(3)
我的思路是:用余数来确定循环体。因为如果两次余数是相同的,那个接下来的运算(不管是商,还是余数)都一定相同!因此,算法如下:
1.用string shang存储商(结果), 用vector<int> yu存储余数;
2.当我们计算出一个余数的时候,在vector中从后向前寻找,如果前面存在一个相同的余数,那个这一段一定是循环体,对应着去string中insert上括弧就好了。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int getSameIndex(vector<int> yu)
{
int a = yu[yu.size()-1];
int i;
for(i = yu.size()-2; i >= 0; i--)//从后向前找,第一个相同的
if(yu[i] == a)
return i;
return -1;
}
int main()
{
int a, b;
cin>>a>>b;
vector<int> yu;//存放余数
string r = ""; // 存放结果
int index;
int shang = a / b;
int shangCount = 0;//记录商的是一个几位数
while(shang != 0)
{
shangCount++;
r.insert(r.begin(), (shang % 10 + '0'));
shang = shang / 10;
}
if(r == "") //shang = 0
{
r = "0";
shangCount = 1;
}
if(a % b != 0) //如果余数不为0,说明有小数部分,加上“.”,否则直接输出结束
r = r + ".";
else
{
cout<<r<<endl;
return 0;
}
a = a % b;
yu.push_back(a);
while(a != 0)
{
a = a * 10;
shang = a / b + '0';
r.insert(r.end(), shang);//安排好商
a = a % b;
yu.push_back(a); //安排好余数
index = getSameIndex(yu);//找之前有没有出现过这个余数
if(index != -1)//出现了相同的余数,说明开始出现循环体了
{
r.insert(r.begin() + shangCount + 1 + index, '('); //+1 是小数点
r.insert(r.end(), ')');
break;
}
}
cout<<r<<endl;
}
第一版(有错误,仅做备份!)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int getSameIndex(vector<int> yu)
{
int a = yu[yu.size()-1];
int i;
for(i = yu.size()-2; i >= 0; i--)//从后向前找,第一个相同的
if(yu[i] == a)
return i;
return -1;
}
int main()
{
int a, b;
cin>>a>>b;
vector<int> yu;//存放余数
string r = ""; // 存放结果
char shang;
int index;
shang = a / b + '0';
r.insert(r.end(), shang);//先把整数部分存上
if(a % b != 0) //如果余数不为0,说明有小数部分,加上“.”,否则直接输出结束
r = r + ".";
else
{
cout<<r<<endl;
return 0;
}
a = a % b;
yu.push_back(a);
while(a != 0)
{
a = a * 10;
shang = a / b + '0';
r.insert(r.end(), shang);//安排好商
a = a % b;
yu.push_back(a); //安排好余数
index = getSameIndex(yu);//找之前有没有出现过这个余数
if(index != -1)//出现了相同的余数,说明开始出现循环体了
{
r.insert(r.begin()+2+index, '(');
r.insert(r.end(), ')');
break;
}
}
cout<<r<<endl;
}