習題3-1 分數統計
使用map,可以很好地統計每個詞出現的次數,而不用擔心用數組時元素爲浮點型的問題。
大致過程:先讀入到map,map會自動按照key排序,遍歷一次map獲得value的最大值,再遍歷一次獲得value=maxvalue的key並輸出。
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main()
{
ifstream fin("stat.in");
ofstream fout("stat.out");
map<double,int> scoreMap;
double score;
while(fin >> score){
scoreMap[score]++;
}
map<double,int>::iterator it;
int maxnum = 0;
for(it=scoreMap.begin() ; it!=scoreMap.end() ; it++){
if(it->second > maxnum){
maxnum = it->second;
}
}
int first = 1;
for(it=scoreMap.begin() ; it!=scoreMap.end() ; it++){
if(it->second == maxnum){
if(first == 1){
fout << it->first;
first = 0;
}
else {
fout << " " << it->first;
}
}
}
fout << endl;
return 0;
}
習題3-2 單詞的長度
每次讀入一個字符串,記錄其長度,並記錄總個數,用總長度除以總個數即可。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("word.in");
ofstream fout("word.out");
string s;
int length =0 ,counter = 0;
while(fin >> s){
length += s.length();
counter ++;
}
fout << (double)length / counter;
return 0;
}
習題 3-3 乘積的末3位
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream fin("product.in");
ofstream fout("product.out");
string s;
int result = 1;
while(fin >> s){
if(s[0]>='A' && s[0]<='Z'){
continue;
}
else{
int num = atoi(s.c_str());
result = result *(num % 1000) % 1000;
}
}
fout << result << endl;
return 0;
}
習題3-4 計算器
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("calculator.in");
ofstream fout("calculator.out");
int a,b;
char cal;
int result;
while(fin >> a >> cal >> b){
switch(cal){
case '+' : result = a + b; break;
case '-' : result = a - b;break;
case '*' : result = a * b; break;
}
fout << result << endl;
}
return 0;
}
習題 3-5 旋轉
實質爲矩陣元素的操作。
實現代碼:
#include <iostream>
#include <fstream>
#define MAX 20
using namespace std;
char num[MAX][MAX];
char result[MAX][MAX];
int main()
{
ifstream fin("rotate.in");
ofstream fout("rotate.out");
int cal,col;
while(fin >> cal >> col){
//input
for(int i=0 ; i<cal ; i++){
for(int j=0 ; j<col ; j++){
fin >> num[i][j];
}
}
//
for(int j=0 ; j<col ; j++){
for(int i=cal-1 ; i>=0 ; i--){
if(i == cal-1){
fout << num[i][j];
}
else {
fout << " " << num[i][j];
}
}
fout << endl;
}
}
return 0;
}
習題 3-6 進制轉換1
實現代碼:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("basel.in");
ofstream fout("basel.out");
int b,n;
int result[10];
int index = -1;
while(fin >> b >> n){
index = -1;
while(n != 0){
result[++index] = n % b;
n = n / b;
}
for(int i=index ; i>=0 ; i--){
fout << result[i];
}
fout << endl;
}
return 0;
}
習題 3-7 進制轉換2
實現代碼:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("base2.in");
ofstream fout("base2.out");
int b,n;
while(fin >> b >> n){
int base = 1;
int result = 0;
while(n != 0){
int temp = n % 10;
result += temp * base;
base *= b;
n = n / 10;
}
fout << result << endl;
}
return 0;
}
習題3-8手機鍵盤
跟題目可能有點不符
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("keyboard.in");
ofstream fout("keyboard.out");
string inputStr;
int myMap[] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};
fin >> inputStr;
// cout << inputStr << inputStr.length() << endl;
for(string::size_type i=0 ; i<inputStr.length() ; i++){
fout << inputStr[i]<<myMap[inputStr[i]-'a'];
}
fout << endl;
return 0;
}