---
# System prepended metadata

title: ITSA線上程式競賽基礎題目

---

ITSA線上程式競賽基礎題目
=======================
## 矩陣數字顯示 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int input=0;
	string num[10][5][5]={{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
	,{{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
	,{{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"},{"*"," "," "," "," "},{"*","*","*","*","*"}}
	,{{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"}}
	,{{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
	,{{"*","*","*","*","*"},{"*"," "," "," "," "},{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"}}
	,{{"*"," "," "," "," "},{"*"," "," "," "," "},{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
	,{{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
	,{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
	,{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}};
		
	while(cin>>input)//持續讀入 
	{
		for(int i=0;i<5;i++)//將每個數字固定由上排到下排執行 
		{
			int output=input;//會重複執行，所以新設變數已重複利用 
			int w=1000;//取高位的除數，一樣要重複利用而至此 
			for(int k=0;k<4;k++)//四個數字 
    		{
    		int number=0;
			number=output/w;//找出最高(左)位數   
			output%=w;//得到剩下的位數 
			w/=10;// 將除數遞減 
		        for(int j=0;j<5;j++)//輸出一排 
        	    {
				cout<<num[number][i][j];//輸出指定位置 
				}
			if(k!=3)cout<<" ";//如果非第四個數字則每排之後空一格	
    		}
    	cout<<endl;//每排結束需換行 
		}
	}
system("pause");
return 0;	
} 
```
## 英哩轉公里
```cpp=
#include<iostream>
#include<iomanip>
using namespace std;

int main(){
	int input;
	while(cin>>input)//讀入整數 
	{
		cout<<fixed<<setprecision(1)<<(input*1.6L*10/10)<<endl;
		/*輸出<<指定浮點數表示<<小數點後幾位(預設為6)
		<<整數轉換小數(乘L)並確保小數點後僅一位<<換行*/ 
	}
	system("pause");
	return 0;
}
```
## 停車費計算 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int startH=0,startM=0,endH=0,endM=0;
	
	cin>>startH>>startM;//讀入開始時間 
	cin>>endH>>endM;//讀入結束時間
	int time=(endH*60+endM)-(startH*60+startM);//計算總時數 
	if(time<=120&&time>=0) cout<<time/30*30<<endl;//2小時內的費用 
	else if(time>120&&time<=240) cout<<(time-120)/30*40+120<<endl;//4小時內的費用+2小時的費用
	else cout<<(time-240)/30*60+120+160<<endl;//4小時外的費用+4小時的費用+2小時的費用
	
	system("pause");
	return 0;
} 
```
## 判斷座標是否在圓形的範圍內 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int inputx=0,inputy=0;
	while(cin>>inputx>>inputy)//依序讀入X，Y值 
	{
		if(inputx*inputx+inputy*inputy <= 10000) cout<<"inside"<<endl;
		//若X平方+Y平方小於半徑(100)的平方，輸出"inside" 
		else cout<<"outside"<<endl;
	}
	system("pause");
	return 0;
} 
```
## 十進制轉二進制 
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int input=0,Bin[8];
	
	cin>>input;
	if(input<0) input+=256;//負數則推移至正整數 
	for(int i=7;i>=0;i--)//逆向遞減輸入 
    {
    	Bin[i]=input%2;//將餘數結果存入 
    	input/=2;//每算出一位數則留商數繼續往下算 
    }
    for(int i=0;i<8;i++)//順向依序輸出 
    {
    	if(i==7)cout<<Bin[i]<<endl;//若為最後一位則換行 
    	else  cout<<Bin[i];//否則緊接在後輸出 
    }	
	system("pause");
	return 0;
}
```
## 季節判定 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int input=0;
	
	cin>>input;
	switch(input/3)//取商數為區分點 
	{
		case 1:cout<<"Spring"<<endl;break;//商數為1，(3,4,5) 
		case 2:cout<<"Summer"<<endl;break;//商數為2，(6,7,8)
		case 3:cout<<"Autumn"<<endl;break;//商數為3，(9,10,11)
		default :cout<<"Winter"<<endl;//其餘(12(商數為4),1,2)皆為冬季 
	} 
	system("pause");
	return 0;
} 
```
## 複數運算 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int input=0;
	
	cin>>input;
	while(input--)//依據輸入的行數遞減計算次數 
	{
		int ai=0,a=0,bi=0,b=0;
		char w;//運算符號 
		
		cin>>w>>ai>>a>>bi>>b;
		switch(w)//依據運算符號來決定計算方式 
		{//依據運算符號輸出計算公式的答案 
			case '+':cout<<ai+bi<<" "<<a+b<<endl;break;
			case '-':cout<<ai-bi<<" "<<a-b<<endl;break;
			case '*':cout<<ai*bi-a*b<<" "<<a*bi+ai*b<<endl;break;
			case '/':cout<<(ai*bi+a*b)/(bi*bi+b*b)<<" "<<a*bi-ai*b/(bi*bi+b*b)<<endl;break;
		}
	}
	system("pause");
	return 0;
} 
```
## 質數判別 
```cpp=
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	int input=0,output=0;
	bool end=0;
	
	while(cin>>input)
	{
		input==1?end=true:end=false;//判斷是否為1
		output=sqrt(input)*10/10;//根號找出其因數列的中位數(最接近的數，不一定就是中位數) 
		for(int i=output;i>1;i--)//開始往下尋找能整除的數 
		{
		input%i==0?end=true:end=false;
		if(end==true) break;//找到則跳脫迴圈 
		}
		end?cout<<"NO"<<endl:cout<<"YES"<<endl;//輸出結果 
	}
	system("pause");
	return 0;
}
```
## 計算正整數被3整除之數值之總和 
```cpp=
#include<iostream>
using namespace std;

int main()
{
	int input=0,i=0;
	
	cin>>input;
	for(i=input;i>=input-2;i--)//找出最近的3的倍數 
	{
    	if(i%3) continue;
    	else break;
	}
	cout<<i/3*(3+i)/2<<endl;//得到總和(等差級數和公式) 
	system("pause");
	return 0;
} 
```
## 輾轉相除法 
```cpp=
#include<iostream>
using namespace std;

int euc(int a,int b);//輾轉相除法遞迴函式宣告 
int main()
{
	int input=0,input2=0,c=0;
	
	cin>>input>>input2;
	if(input<input2)//確認大小順序 
	{
		c=input;
		input=input2;
		input2=c;
	}
	cout<<euc(input,input2)<<endl;
	return 0;
}
int euc(int a,int b)
{
	int f=a%b;
	if(f==0) return b;
	else return euc(b,f);
}
```
## 矩陣反轉
```cpp=
#include<iostream>
#include<string>
#include<sstream> 
using namespace std;

int main()
{
	int inputBus=0,inputLine=0;
	
	cin>>inputBus>>inputLine;//輸入陣列大小 
	cin.ignore(1,'\n');//整數後讀字串，需先消除隱藏於後的"\n" 
	int reArray[inputLine][inputBus];//產生陣列 
	for(int bus=0;bus<inputBus;bus++)//讀入次數即列數 
	{
		string nums;
		getline(cin,nums);
		istringstream cutStr(nums);
		for(int line=0;line<inputLine;line++)//字串分割數即行數 
		{
			string save;
			getline(cutStr,save,' ');
			istringstream chType(save);
			chType>>reArray[line][bus];//一一讀入指定的反矩陣座標 
		}
	}
	for(int i=0;i<inputLine;i++)//輸出反矩陣座標 
	{
		for(int j=0;j<inputBus;j++)
		{
			if(j==inputBus-1) cout<<reArray[i][j]<<endl;
			else cout<<reArray[i][j]<<" ";
		}
	}
	system("pause");
	return 0;
}
```
## 遞迴程式練習 
```cpp=
#include<iostream>
#include<cmath>
using namespace std;

int m (int input);
int main()
{
	int input=0;
	
	cin>>input;//輸入整數 
	cout<<m(input)<<endl;//輸出函式的值 
	system("pause");
	return 0;
}
int m (int input)
{
	if(input==0||input==1)//當輸入值為1或0 
	{
		return input+1;
	}
	else return m(input-1)+m(ceil(input/2));
	//因題目寫明，輸入值為大於1的整數，所以不需擔心負值 
} 
```
## 撲克牌大小 
```cpp=
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

void sortPoke (vector<string>& a,int n);
int main()
{
	int input=0,n=0;
	string Str,s;
	vector<string> pokeS;

	cin>>input;//輸入測試資料筆數
	cin.ignore(1,'\n');//消除於字串後的換行符號
	for(int i=0;i<input;i++)//依資料筆數決定處理次數
	{
		getline(cin,Str);//讀入一整行
		istringstream cutStr(Str);//轉換成可分割型態
		while(getline(cutStr,s,' '))//分割並推入向量
		{
			pokeS.push_back(s);
		}
		sortPoke(pokeS,pokeS.size());//排序
		for(int j=0;j<pokeS.size();j++)//輸出排序結果
		{
			if(j==pokeS.size()-1) cout<<pokeS[j]<<endl;
			else cout<<pokeS[j]<<" ";
		}
		pokeS.clear();//清空向量給下一筆測資
	}
	return 0;
}
void sortPoke (vector<string>& a,int n)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			string t;
			if(a[i][0]>a[j][0]){t=a[i];a[i]=a[j];a[j]=t;}
		}
	}
	//以上為泡沫排序法
	for(int i=0;i<n;i++)
	{
		for(int j=0+i;j<n;j++)
		{
			if(a[i][0]==a[j][0])
            {//針對其數字不同進行轉換型態後比較
                string t,bN=a[i].substr(1,2),sN=a[j].substr(1,2);
                istringstream bigN(bN);
                istringstream smaN(sN);
                int b=0,s=0;
                bigN>>b;smaN>>s;
                if(b<s){t=a[i];a[i]=a[j];a[j]=t;}
            }
		}
	}
	//最後進行同花色間的排序
}
```
## 判斷是否為迴文 
```cpp=
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string input,output;
	bool a=1; 
	
	cin>>input;
	for(int i=input.length()-1;i>=0;i--)//將字串逆序傳到output 
	{
		output[input.length()-1-i]=input[i];
	}
	for(int j=0;j<input.length();j++)//比較是否有異 
	{
		if(input[j]!=output[j]){a=0;break;}//發現有異即時跳出 
		else a=1;
	}
	a?cout<<"YES"<<endl:cout<<"NO"<<endl;//輸出結果 
	return 0;
} 
```
## 文字及字母出現次數 
```cpp=
#include<iostream>
#include<string>
#include<sstream>
#include<map>
using namespace std;

int main()
{
	string input,save;
	int wordsNum=0;
	map<char,int> wF;
	string w="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
	
	getline(cin,input);
	istringstream cutStr(input);
	while(getline(cutStr,save,' '))
	{
		wordsNum++;//計算單字數 
		for(int j=0;j<save.length();j++)//統計字數 
		{
			if(wF.count(save[j])==0){wF.insert(make_pair(save[j],1));}
			else wF[save[j]]+=1;
		}
	}
	cout<<wordsNum<<endl;//輸出單字數 
	for(int k=0;k<52;k++)//調整為題目輸出順序(原順序為先大寫再小寫) 
	{
		for(map<char,int>::iterator p=wF.begin();p!=wF.end();p++)
		{
		if(w[k]==p->first&&p->second!=0)
		cout<<p->first<<" "<<":"<<" "<<p->second<<endl;
		}
	}
	return 0;
} 
```
## 子字串出現次數
```cpp=
#include<iostream>
#include<string>
using namespace std;
int appearNum(string& s,int p,string& l);
int main()
{
	string inputS,inputL,save;
	
	cin>>inputS>>inputL;
	cout<<appearNum(inputS,0,inputL)<<endl;
	return 0;
}
int appearNum(string& s,int p,string& l)//字串遞迴(檢索字串，起始位置，被檢索字串) 
{
	int a=0;
	if(p>l.length())return a=0;//若偏移長度大於被檢索字串長度則回傳0 
	for(int i=0;i<s.length();i++)//依檢索字串長度進行迴圈 
	{
		if(l[i+p]==s[i])continue;//若比對符合，則持續比對至完全符合 
		else return a+=appearNum(s,p+1,l);//比對不符回傳當前符合次數 
	}
	a++;return a+=appearNum(s,p+1,l);//迴圈完畢表示比對完全符合，計次後往右偏移再測試 
}
```
## 英文斷詞 
```cpp=
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
	string input,save;
	vector<string> count;
	
	getline(cin,input);
	istringstream cutStr(input);
	while(getline(cutStr,save,' '))
	{
		for(int j=0;j<save.length();j++)
		{
			save[j]=tolower(save[j]);
		}
		count.push_back(save); 
	}//將讀入的整行字串拆解並讀入 
	for(int i=count.size()-1;i>0;i--)//從尾開始往前尋找重複的並移除 
	{
		for(int j=i-1;j>=0;j--)
		{
			if(count[i]==count[j]){count.erase(count.begin()+i);break;}
		}
	}
	for(int i=0;i<count.size();i++)//整理後依序輸出 
	{
		if(i==count.size()-1)cout<<count[i]<<endl;
		else cout<<count[i]<<" ";
	}
	return 0;
} 
```
## QWERTY 
```cpp=
#include<iostream>
#include<map>
#include<cctype>
#include<string>
using namespace std;

int main()
{
	string input;
	map<char,char> key;
	//因會將大寫轉小寫，固不必將大寫加入字典，其餘依題目要求輸入 
	key.insert(make_pair(' ',' '));key.insert(make_pair('!','@'));
	key.insert(make_pair('"','"'));key.insert(make_pair('#','$'));
	key.insert(make_pair('$','%'));key.insert(make_pair('%','^'));
	key.insert(make_pair('&','*'));key.insert(make_pair('\'','\''));
	key.insert(make_pair('(',')'));key.insert(make_pair(')','_'));
	key.insert(make_pair('*','('));key.insert(make_pair('+','+'));
	key.insert(make_pair(',','.'));key.insert(make_pair('-','='));
	key.insert(make_pair('.','/'));key.insert(make_pair('/','/'));
	key.insert(make_pair('0','-'));key.insert(make_pair('1','2'));
	key.insert(make_pair('2','3'));key.insert(make_pair('3','4'));
	key.insert(make_pair('4','5'));key.insert(make_pair('5','6'));
	key.insert(make_pair('6','7'));key.insert(make_pair('7','8'));
	key.insert(make_pair('8','9'));key.insert(make_pair('9','0'));
	key.insert(make_pair(':','\"'));key.insert(make_pair(';','\''));
	key.insert(make_pair('<','>'));key.insert(make_pair('=','='));
	key.insert(make_pair('>','?'));key.insert(make_pair('?','?'));
	key.insert(make_pair('@','#'));key.insert(make_pair('a','s'));
	key.insert(make_pair('b','n'));key.insert(make_pair('c','v'));
	key.insert(make_pair('d','f'));key.insert(make_pair('e','r'));
	key.insert(make_pair('f','g'));key.insert(make_pair('h','j'));
	key.insert(make_pair('g','h'));key.insert(make_pair('i','o'));
	key.insert(make_pair('j','k'));key.insert(make_pair('k','l'));
	key.insert(make_pair('l',';'));key.insert(make_pair('m',','));
	key.insert(make_pair('n','m'));key.insert(make_pair('o','p'));
	key.insert(make_pair('p','['));key.insert(make_pair('q','w'));
	key.insert(make_pair('r','t'));key.insert(make_pair('s','d'));
	key.insert(make_pair('t','y'));key.insert(make_pair('u','i'));
	key.insert(make_pair('v','b'));key.insert(make_pair('w','e'));
	key.insert(make_pair('x','c'));key.insert(make_pair('y','u'));
	key.insert(make_pair('z','x'));key.insert(make_pair('[',']'));
	key.insert(make_pair('\\','\\'));key.insert(make_pair(']','\\'));
	key.insert(make_pair('^','&'));key.insert(make_pair('_','='));
	key.insert(make_pair('`','1'));key.insert(make_pair('{','}'));
	key.insert(make_pair('|','|'));key.insert(make_pair('}','|'));
	key.insert(make_pair('~','!'));
	getline(cin,input);
	for(int i=0;i<input.length();i++)//把輸入的字串全轉小寫 
	{
		input[i]=tolower(input[i]);
	}
	for(int i=0;i<input.length();i++)//依字串的順序比對並輸出 
	{
		for(map<char,char>::iterator p=key.begin();p!=key.end();p++)
		{
			if(input[i]==p->first)cout<<p->second;
			if(input[i]==p->first&&i==input.length()-1)cout<<endl;
		}
	}
	return 0;
} 
```
## 最少派車數 
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int inputN=0,t[24]={0};//一天24小時 
	cin>>inputN;
	for(int i=0;i<inputN;i++)//因為(返回時間恆大於出發時間)，故依輸入時間依序填格子 
	{
		int a=0,b=0;
		cin>>a>>b;
		for(int j=a-1;j<b-1;j++){t[j]++;}
	}
	inputN=t[0];//將不需要的空間再次利用 
	for(int i=0;i<24;i++){if(inputN<t[i])inputN=t[i];}//尋最大值 
	cout<<inputN<<endl;
	return 0;
}
```
## 大整數加法 (java)
```java=
import java.math.BigInteger;  
import java.util.*;    
public class Itsa20 {  
  public static void main(String[] args) {  
	  Scanner sc=new Scanner(System.in);  
	  int n=sc.nextInt();  
	  BigInteger arr,tot;  
	  for(int i=0;i<n;i++){  
		  arr=sc.nextBigInteger();  
		  tot=sc.nextBigInteger();  
		  System.out.println(tot.add(arr));   
		  }  
	  }  
  }  
```
## 最大值與最小值 (java)
```java=
import java.util.*;
import java.text.DecimalFormat;
public class Itsa21 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		DecimalFormat f=new DecimalFormat("0.00");//要注意到用"#"的話，0會被省略掉
		float input=0,ar[]=new float[10],max=ar[0],min=ar[0];
		for(int i=0;i<10;i++)
		{
			input=sc.nextFloat();
			ar[i]=input;
			if(ar[i]>max)max=ar[i];
			if(ar[i]<min)min=ar[i];
		}
		System.out.println("maximum:"+f.format(max));
		System.out.println("minimum:"+f.format(min));
	}
}
```
## 圈圈叉叉 
```cpp=
#include<iostream>
using namespace std;
int main(){
	int arr[3][3];//開井字陣列 
	for(int i=0;i<3;i++)//3排讀三次 
	{
		cin>>arr[i][0]>>arr[i][1]>>arr[i][2];//依序輸入 
		if(arr[i][0]==arr[i][1]&&arr[i][1]==arr[i][2])//直接進行整排檢查 
		{
		cout<<"True"<<endl;
		return 0;}
	}
	for(int x=0;x<3;x++)//直行檢查 
	{
	if(arr[0][x]==arr[1][x]&&arr[1][x]==arr[2][x]) 
	{
	cout<<"True"<<endl;return 0;}}
	if(arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2])//反斜線檢查 
	{
	cout<<"True"<<endl;return 0;}
	else if(arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0])//斜線檢查 
	{
	cout<<"True"<<endl;return 0;}
	else cout<<"False"<<endl;return 0;//都沒有 
}
```
## 找零錢問題 
```cpp=
#include<iostream>
#include<sstream>
using namespace std;
int main(){
	string input,str;
	int mon[4];
	
	getline(cin,input);
	istringstream cut(input);
	for(int i=0;i<4;i++)//逐一拆字讀入陣列 
	{
		getline(cut,str,',');
		istringstream sa(str);
		sa>>mon[i];
	}
	if(mon[0]>mon[1]*15+mon[2]*20+mon[3]*30)//判定零錢是否足夠,同時重複利用陣列空間 
	{
		mon[0]=mon[0]-mon[1]*15-mon[2]*20-mon[3]*30;//結算剩餘金額 
		mon[2]=mon[0]/50;mon[0]%=50;
		mon[1]=mon[0]/5;mon[0]%=5;
		cout<<mon[0]<<","<<mon[1]<<","<<mon[2]<<endl;return 0;
	}
	else cout<<"0"<<endl;return 0;//金額不足 
}
```
## 計算複利 
```cpp=
#include<iostream>
#include<cmath>
using namespace std;
void m(double r,int p,int n);
int main(){
	double r=0;
	int n=0,p=0;
	cin>>r>>n>>p;
	m(r,p,n);
	return 0;
}
void m(double r,int p,int n)
{
	int s=p,pm[30],pt[30],rr=floor(r*1000);;
	
	for(int i=0;i<30;i++)
	{
		if(i>=0&&i<11)pm[i]=0;
		else {pm[i]=p%10;p/=10;}
		pt[i]=0;
	}
	for(int k=0;k<n;k++)
	{	
		for(int i=0;i<30;i++)//p=p*r
		{
			pt[i]+=pm[i]*rr;
		}
		for(int i=0;i<30;i++)//整數進位 
		{
			pt[i+1]+=pt[i]/10;
			pt[i]%=10;
		}
		for(int i=3;i<30;i++)//整數移位 
		{
			pt[i-3]=pt[i];
		}
		for(int i=0;i<30;i++)//p+=p
		{
			pt[i]+=pm[i];
		}
		for(int i=0;i<30;i++)//整數進位 
		{
			pt[i+1]+=pt[i]/10;
			pt[i]%=10;
		} 	
		if(k==n-1)//是否繼續計算複利 
		{
			bool t=0;//判定是否為不輸出的空值 
			for(int i=29;i>=11;i--)
			{
				if(pt[i]!=0||t!=0)//尋找首個非零值並輸出之後的零 
				{
				t++;
				cout<<pt[i];
				if(i==11)cout<<endl;//結尾則換行 
				}
			else continue;
			}
		}
		else
		{
			pt[11]+=s;
			for(int i=0;i<30;i++)
			{
			pm[i]=pt[i];
			pt[i]=0;
			}
		}	
	}
}
```
## 字串之編碼數值總和 
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int input=0;
	cin>>input;
	cin.ignore(1,'\n');//整數後讀字串需消除\n 
	while(input--)
	{
		string s;
		int sum=0;
		getline(cin,s);
		for(int i=0;i<s.length();i++)//依字串長度進行字元轉換ASCII碼並加總 
		{
			sum+=(int)s[i];
		}
		cout<<sum<<endl;
	}
	return 0;
} 
```
## 各位數和排序
```cpp=
#include<iostream>
#include<sstream>
using namespace std;
void sort(int c[],int& m);
int main()
{
	int in=0;
	cin>>in;
	int ar[in];//太早設導致"ar[0]"，衍伸"無效記憶體引用" 
	for(int i=0;i<in;i++){cin>>ar[i];}
	sort(ar,in);
	for(int i=in-1;i>=0;i--){if(i==0){cout<<ar[i]<<endl;}else cout<<ar[i]<<" ";}
	return 0;
}
void sort(int c[],int& m)
{
	int b[m],a=0;
	for(int j=0;j<m;j++)//依序求出總和值 
	{
		b[j]=0;a=c[j];
		while(a>0)
		{
			b[j]+=a%10;
			a/=10;
		}
	}
	for(int i=0;i<m;i++)//泡泡排序
	{
		for(int j=0;j<m;j++)
		{
			if(b[i]>b[j]||(b[i]==b[j]&&c[i]>c[j])) 
			{
				int tc=0,tb=0;
				tb=b[i];b[i]=b[j];b[j]=tb;
				tc=c[i];c[i]=c[j];c[j]=tc;
			}
		} 
	}
}
```
## 猜數字
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int an=0,ana[4],ain[4];
	cin>>an;
	for(int i=3;i>=0;i--)//逆向讀入陣列 
	{
		ana[i]=0;ain[i]=0;
		ana[i]=an%10;an/=10;
	}
	while(true)//保持無限迴圈狀態 
	{
		int in=0,a=0,b=0;
		cin>>in;
		an=0;//解除無限迴圈用 
		for(int i=3;i>=0;i--)//確保位置與答案相同 
		{
			ain[i]=in%10;in/=10;//逆向讀入陣列 
			if(ain[i]==0)an++;//判定0的個數 
			if(ain[i]==ana[i]){a++;continue;}//確認同位置且同答案 
			else {for(int j=0;j<4;j++){if(ain[i]==ana[j]&&j!=i){b++;break;}}}//尋找其餘3個的位置是否有同樣答案 
		}
		if(an==4)break;//判定是否4個0 
		else cout<<a<<"A"<<b<<"B"<<endl;//輸出答案 
	}
	return 0;
} 
```
## 統一發票對獎 (java)
```java=
import java.util.*;
public class itsa28 {

	public static void main(String[] args) 
	{
		Scanner s=new Scanner(System.in);
		String f,st[]=new String[4];
		for(int i=0;i<4;i++) {f=s.next();st[i]=f;}
		int n=s.nextInt(),mon=0,m[]=new int[7];
		String inp[]=new String[n];
		for(int i=0;i<n;i++) {f=s.next();inp[i]=f;}
		for(int i=0;i<n;i++) 
		{
			if (st[0].equals(inp[i])) {m[0]++;mon+=2000000;continue;}
			for (int j=0;j<=5;j++) 
			{
			    if (inp[i].indexOf(st[1].substring(j),j)>=0){m[j+1]++;break;} 
			    else if (inp[i].indexOf(st[2].substring(j),j)>=0){m[j+1]++;break;}
			    else if (inp[i].indexOf(st[3].substring(j),j)>=0) {m[j+1]++;break;}
			}
		}
		for(int i=0;i<7;i++) {if(i==6) {System.out.println(m[i]);}else {System.out.print(m[i]+" ");}}
		mon+=200000*m[1]+40000*m[2]+10000*m[3]+4000*m[4]+1000*m[5]+200*m[6];
		System.out.println(mon);
	}
}
```
## 身分證驗證器 
```cpp=
#include<iostream>
using namespace std;
int main()
{
	string in;
	getline(cin,in);
	if(((int)in[1]-'0')<1||((int)in[1]-'0')>2){cout<<"WRONG!!!"<<endl;return 0;}//性別檢測 
	for(int i=1;i<9;i++)//數字區域是否為數字 
	{
		if(in[i]<'0'||in[i]>'9'){cout<<"WRONG!!!"<<endl;return 0;}
	}
	int p=0,loc=0;
	loc=(int)in[0]; 
	if(loc<'A'||loc>'Z'){cout<<"WRONG!!!"<<endl;return 0;}//區域碼轉換 
	else if(loc<=72){loc-=55;}//A~H
	else if(loc==73){loc=34;}//I
	else if(loc>73&&loc<=78){loc-=56;}//J~N 
	else if(loc==79){loc=35;}//O 
	else if(loc>79&&loc<=86||loc==90){loc-=57;}//P~V && Z 
	else if(loc==87){loc=32;}//W 
	else if(loc>87&&loc<=89){loc-=58;}//X、Y 
	p=(loc/10)+9*(loc%10);//取十位數再加上個位數*9 
	for(int i=1;i<9;i++){p+=(9-i)*((int)in[i]-'0');}//加上之後的公式總和 
	if(10-(p%10)!=((int)in[9]-'0')){cout<<"WRONG!!!"<<endl;return 0;}//檢查碼判定 
	else {cout<<"CORRECT!!!"<<endl;return 0;}	
}
```
## 星座查詢
```cpp=
#include<iostream>
using namespace std;
int main()
{
	string in;//因輸入的月有"0"在，故改字串 
	getline(cin,in);
	int m=10*((int)in[0]-'0')+((int)in[1]-'0');//月轉數字 
	int d=10*((int)in[3]-'0')+((int)in[4]-'0');//日轉數字
	switch(m)
	{
		case 1:d<21?cout<<"Capricorn"<<endl:cout<<"Aquarius"<<endl;break;
		case 2:d<19?cout<<"Aquarius"<<endl:cout<<"Pisces"<<endl;break;
		case 3:d<21?cout<<"Pisces"<<endl:cout<<"Aries"<<endl;break;
		case 4:d<21?cout<<"Aries"<<endl:cout<<"Taurus"<<endl;break;
		case 5:d<22?cout<<"Taurus"<<endl:cout<<"Gemini"<<endl;break;
		case 6:d<22?cout<<"Gemini"<<endl:cout<<"Cancer"<<endl;break;
		case 7:d<23?cout<<"Cancer"<<endl:cout<<"Leo"<<endl;break;
		case 8:d<24?cout<<"Leo"<<endl:cout<<"Virgo"<<endl;break;
		case 9:d<24?cout<<"Virgo"<<endl:cout<<"Libra"<<endl;break;
		case 10:d<24?cout<<"Libra"<<endl:cout<<"Scorpio"<<endl;break;
		case 11:d<23?cout<<"Scorpio"<<endl:cout<<"Sagittarius"<<endl;break;
		case 12:d<22?cout<<"Sagittarius"<<endl:cout<<"Capricorn"<<endl;break;
	}
	return 0;
} 
```
## 過半元素
```cpp=
#include<iostream>
#include<sstream>
#include<map>
using namespace std;
int main()
{
	map<int,int> c;
	
	string num,ss;
	while(getline(cin,num))//題目說明測資不只一筆(多行)，加上長短不一 
	{
		int a=0;//計算該筆測資長度 
		string n;
		istringstream cut(num);
		while(getline(cut,n,' '))//推入向量的同時並計數 
		{
			int t=0;
			istringstream ss(n);
			ss>>t;a++;
			if(c.count(t)==0){c.insert(make_pair(t,1));}
			else c[t]+=1;
		}
		int maxf=0,maxs=0;
		for(map<int,int>::iterator p=c.begin();p!=c.end();p++)
		{//走訪尋找最多次者 
			if(p->second>maxs){maxs=p->second;maxf=p->first;}
		}
		maxs>a/2?cout<<maxf<<endl:cout<<"NO"<<endl;//判定是否過半 
		c.clear();
	}
	return 0;
} 
```
## 字母往後移
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string in;
	getline(cin,in);
	int p=0;cin>>p;
	for(int i=0;i<in.length();i++)
	{
		if((int)in[i]>='A'&&(int)in[i]<='Z')
		{
			in[i]=(char)(((int)(in[i]-'A')+p)%26+'A');
		}
		if((int)in[i]>='a'&&(int)in[i]<='z')
		{
			in[i]=(char)(((int)(in[i]-'a')+p)%26+'a');
		}
		if((int)in[i]>='0'&&(int)in[i]<='9')
		{
			in[i]=(char)(((int)(in[i]-'0')+p)%10+'0');
		}
	}
	cout<<in<<endl;in=" "; 
	return 0;
}
```
## 一整數序列所含之整數個數及平均值 (java)
```java=
import java.text.*;
import java.util.*;
public class itsa33 {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		while(true) //因多行唯一筆測資
		{
			String nums=s.nextLine(),n[]=nums.split(" ");
			double sum=0;
			for(int i=0;i<n.length;i++){int input=Integer.parseInt(n[i]);sum+=input;}
			DecimalFormat f = new DecimalFormat("0.000");
			System.out.println("Size: "+n.length);
			System.out.println("Average: "+f.format(sum/n.length));
		}
	}
}
```
## 標準體重計算 (java)
```java=
import java.text.DecimalFormat;
import java.util.*;
public class itsa34 {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		while(true) 
		{
			String st=s.nextLine(),in[]=st.split(" ");
			DecimalFormat f = new DecimalFormat("0.0");
			float h=Float.parseFloat(in[0]);
			int se=Integer.parseInt(in[1]);
			if(se==1)System.out.println(f.format((h-80)*0.7));
			else System.out.println(f.format((h-70)*0.6));
		}
	}
}
```
## 買獎品
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int tn=0;cin>>tn;
	while(tn--)
	{
		int T=0,m=0,k=0;
		cin>>T>>m>>k;
		int ks[k];
		for(int i=0;i<k;i++){ks[i]=0;cin>>ks[i];}
		for(int i=0;i<k;i++)
		{
			for(int j=0;j<k;j++)
			{
				if(ks[i]<ks[j]){int t=0;t=ks[i];ks[i]=ks[j];ks[j]=t;}
			}
		}
		k=0;
		for(int i=0;i<m;i++){k+=ks[i];}
		cout<<k<<endl;
		k>T?cout<<"Impossible"<<endl:cout<<k<<endl;
	}	
	return 0;
}
```
## 平、閏年判定
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int in=0;
	while(cin>>in)
	{
		if(in%400==0)cout<<"Bissextile Year"<<endl;
		else
		{
			if(in%100==0)cout<<"Common Year"<<endl;
			else
			{
				in%4==0?cout<<"Bissextile Year"<<endl:cout<<"Common Year"<<endl;
			}
		}
	}
	return 0;
} 
```
## 18啦遊戲設計
```cpp=
#include<iostream>
using namespace std;
int main()
{
	int ds[6],in=0;
	for(int i=0;i<6;i++){ds[i]=0;}
	for(int j=0;j<4;j++){
		cin>>in;
		for(int i=0;i<6;i++){
			if(i+1==in){ds[i]++;break;}}}
	int d[3];in=2;
	for(int i=0;i<3;i++){d[i]=0;}
	for(int i=0;i<6;i++){
		if(ds[i]>0){d[in]=(i+1)*10+ds[i];in--;}}
	if(d[2]%10==4){cout<<"WIN"<<endl;return 0;}
	if(d[2]%10==3){cout<<"R"<<endl;return 0;}
	if(d[2]%10==2){
		if(d[1]%10==2){cout<<d[1]/10*2<<endl;return 0;}
		else {cout<<d[1]/10+d[0]/10<<endl;return 0;}}
	if(d[2]%10==1){
		if(d[1]%10==3){cout<<"R"<<endl;return 0;}
		else if(d[1]%10==2){cout<<d[2]/10+d[0]/10<<endl;return 0;}
		else {
			if(d[0]%10==2){cout<<d[2]/10+d[1]/10<<endl;return 0;}
			else {cout<<"R"<<endl;return 0;}}}
} 
```
## 計算電費
```cpp=
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
	int t=0;cin>>t;
	while(t--){
		double in,nSu=0,Su=0;
		cin>>in;
		if(in>700) {Su+=(in-700)*5.63;nSu+=(in-700)*4.5;in=700;}
		if(in>500) {Su+=(in-500)*4.97;nSu+=(in-500)*4.01;in=500;}
		if(in>330) {Su+=(in-330)*4.39;nSu+=(in-330)*3.61;in=330;}
		if(in>120) {Su+=(in-120)*3.02;nSu+=(in-120)*2.68;in=120;}
		if(in>0){Su+=in*2.1;nSu+=in*2.1;}
		cout<<fixed<<setprecision(2)<<"Summer months:"<<Su<<endl;
		cout<<fixed<<setprecision(2)<<"Non-Summer months:"<<nSu<<endl;
		}
	return 0;
}
```
## 計算電費 (java)
```java=
import java.text.*;
import java.util.*;
public class itsa38 {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		DecimalFormat f = new DecimalFormat("0.00");
		while(s.hasNext()) 
		{double in=s.nextFloat(),nSu=0,Su=0;
		if(in>700) {Su+=(in-700)*5.63;nSu+=(in-700)*4.5;in=700;}
		if(in>500) {Su+=(in-500)*4.97;nSu+=(in-500)*4.01;in=500;}
		if(in>330) {Su+=(in-330)*4.39;nSu+=(in-330)*3.61;in=330;}
		if(in>120) {Su+=(in-120)*3.02;nSu+=(in-120)*2.68;in=120;}
		if(in>0){Su+=in*2.1;nSu+=in*2.1;}
		System.out.println("Summer months:"+f.format(Su));
		System.out.println("Non-Summer months:"+f.format(nSu));
		}}}
```
## 考試測驗 (java)
```java=
import java.util.*;
public class itsa39 {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int in=s.nextInt();
		while(in--!=0) 
		{
			int p[]=new int[4];p[3]=0;
			for(int i=0;i<3;i++){p[i]=0;p[i]=s.nextInt();p[3]+=p[i];}
			for(int i=0;i<3;i++)
			{
				for(int j=0;j<3;j++){if(p[i]<p[j]){int t=0;t=p[i];p[i]=p[j];p[j]=t;}}
			}
			if(p[0]>=60)System.out.println("P");
			else if(p[3]>=220)System.out.println("P"); 
			else if(p[3]<220&&p[1]>=60)System.out.println("M");
			else if(p[3]<199&&p[2]>=80)System.out.println("M");
			else System.out.println("F");	
		}
	}
}
```
## ISBN驗證
```cpp=
#include<iostream>
#include<string> 
#include<sstream>
using namespace std;

int main()
{
	string input;
	int fir[10],twi[10],thr[10];
	
	while(getline(cin,input))//讀入一行字串 
	{
		istringstream cutString(input);//轉成可分割的型態 
		string output;//儲存被分割的字串 
		for(int i=0;i<10;i++)//10個字 
		{
			getline(cutString,output,' ');//將切割的字串以空白為分割依次存入 
			if(output=="X"||output=="x") output="10";//若找到X則改成10 
			istringstream chanInt(output);//將一段字串轉換型態 
			chanInt >> fir[i];//一個個存入第一次陣列	
		}
		for(int i=0;i<10;i++)//依照題目給的計算規則得到第二、三的陣列 
    	{
    		i==0?twi[i]=fir[i]:twi[i]=twi[i-1]+fir[i]; 
			i==0?thr[i]=twi[i]:thr[i]=thr[i-1]+twi[i];
    	} 
		thr[9]%11?cout<<"NO"<<endl:cout<<"YES"<<endl;//以最後求得的數進行判斷 
	}	
	system("pause");
	return 0;
} 
```
