華科2018複試上機題

三道題分別如下:

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 = ""; // 存放結果
	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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章