# Dain's CPE練習
## 100 - The 3n + 1 problem
```c=
#include <bits/stdc++.h>
using namespace std;
int main(){
int i, j;
while(cin>>i>>j){
int next,ans=0,temp;
cout<<i<<" "<<j<<" ";
if(j<i){
temp=i;
i=j;
j=temp;
}
for(int t=i;t<=j;t++){
int k=t;
next=1;
while(k>1){
if(k%2==0){
k=k/2;
next++;
}
else{
k=3*k+1;
next++;
}
}
if(next>ans){
ans=next;
}
}
cout<<ans<<endl;
}
return 0;
}
```
#### input
```text=
1 10
10 1
100 200
201 210
900 1000
1 99999
99999 99999 13 36 23 88
```
#### output
```text=
1 10 20
10 1 20
100 200 125
201 210 89
900 1000 174
1 99999 351
99999 99999 227
13 36 112
23 88 116
```
## 10035 - Primary Arithmetic
```C=
#include <iostream>
using namespace std;
int main()
{
long int a,b;
while(cin>>a>>b)
{
if(a==0 && b==0)
{
break;
}
int c=0,counter=0;
while(a!=0 || b!=0)
{
if(a%10+b%10+c>=10)
{
counter++;
c=1;
}
else
{
c=0;
}
a=a/10;
b=b/10;
}
if(counter==0)
{
cout<<"No carry operation."<<endl;
}
else if(counter==1)
{
cout<<"1 carry operation."<<endl;
}
else
{
cout<<counter<<" carry operations."<<endl;
}
}
return 0;
}
```
## 10222 - Decode the Mad man
```C=
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1=" `1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./";
//建立比對用的字串,先按空白鍵,之後從鍵盤左邊按到右邊就對了。
string s2;
while(getline(cin,s2)){//輸入
for(int i=0;i<s2.length();i++){//開始一個一個比對
if(s2[i]>='A'&&s2[i]<='Z')//先大寫換成小寫
s2[i]=s2[i]+32;
for(int t=0;t<s1.length();t++){//逐一比對s1
if(s2[i]==s1[0]){//空白鍵不用轉換
cout<<" ";
break;
}
else if(s2[i]==s1[t]){//輸出左邊兩個鍵盤的字
cout<<s1[t-2];
break;
}
}
}
cout<<endl;//換行
}
return 0;
}
```
#### input
```
k[r dyt i[o
p '[nt ]y[jyd.
```
#### output
```
how are you
i love program
```
## UVa10019:Funny Encryption Method
```c=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,in1,cn1;
cin>>n;//輸入測資數量
while(n--)
{
int b1=0,b2=0;
cin>>in1;//輸入測資
cn1=in1;
while(cn1>0)//十進制轉二進制
{
if(cn1%2==1)//利用短除法
b1=b1+1;//計算有幾個1
cn1=cn1/2;
}
cn1=in1; //cn1變回原本的輸入
while(cn1>0)//十六進制轉二進制
{ //利用1個位數轉4個位數的方法
int temp=cn1%10;//先取最左邊的位數
while(temp>0)//再利用10進位轉2進位的方法
{
if(temp%2==1)
b2++;
temp=temp/2;
}
cn1=cn1/10;
}
cout<<b1<<" "<<b2<<endl;//輸出
}
return 0;
}
```
#### input
```
3
265
111
1234
```
#### output
```
3 5
6 3
5 5
```
## UVa10038:Jolly Jumpers
```c=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;//n輸入用 i方便用
while(cin>>n)
{
int num[n]={0},count[n]={0};//建立輸入用陣列 ,記錄用字串
int jolly=1;//最後判斷是不是jolly
for(i=0;i<n;i++)//輸入兼運算
{
cin>>num[i];//輸入數列
if(i>0)//i要大於0才可以運算
{
int temp=abs(num[i]-num[i-1]);//計算兩兩相鄰的數的差的絕對值
if(temp<n)//temp必須要小於n不然就不是jolly
{
count[temp]++;//計算這個差出現幾次
if(count[temp]>1||temp==0)//這個差為0或者出現超過一次就不是jolly
jolly=0;
}
else
jolly=0;
}
}
if(jolly==1)//輸出
cout<<"Jolly"<<endl;
else
cout<<"Not jolly"<<endl;
}
}
```
#### input
```
4 1 4 2 3
5 1 4 2 -1 6
```
#### output
```
Jolly
Not jolly
```
## UVA10041 - Vito's Family
```c=
#include <bits/stdc++.h>
using namespace std;
int main(){
int testcase; //測資數量
cin>>testcase;
int d[500]={0};
for(int i=0;i<testcase;i++){
int r; //親戚數量
cin>>r;
int s[500]; //每個親戚的門牌號碼,上限500人
int best(0);
for(int j=0;j<r;j++)
cin>>s[j];
sort(s,s+r);
best=s[(int)r/2]; //取中位數
for(int j=0;j<r;j++)
d[i]+=abs(s[j]-best);
}
for (int i=0;i<testcase;i++)
cout<<d[i]<<endl;
return 0;
}
```
#### input
```
2
2 2 4
3 2 4 6
```
#### output
```
2
4
```
## 10055 - Hashmat the Brave Warrior
```c=
#include <bits/stdc++.h>
using namespace std;
int main ()
{
long long in1,in2,num;
while(cin>>in1>>in2)//輸入測資
{
num=abs(in1-in2);//相減取絕對值
cout<<num<<endl;//輸出
}
return 0;
}
```
#### input
```
10 12
10 14
100 200
4294967296 1
1 4294967296
11264 29580
20092 4195
```
#### output
```
2
4
100
4294967295
4294967295
18316
15897
```
## 10062 - Tell me the frequencies!
```c=
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
int i, j;
bool flag = false;
while(getline(cin,str)){
int count[128] = {0};
flag = true;
for(i = 0; i < str.length(); i++)
count[str[i]]++;
for(i = 1; i <= str.length(); i++){ // the number of each ASCII
for(j = 127; j >= 32; j--){ // counter of ASCII
if(count[j] == i)
cout<<j<<" "<<i<<endl;
}
}
}
return 0;
}
```
#### input
```
AAABBC
122333
```
#### output
```
67 1
66 2
65 3
49 1
50 2
51 3
```
## UVA10071:Back to High School Physics
```c=
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, v;
while(cin >> t >> v)
cout << 2*t*v << endl;
return 0;
}
```
#### input
```
0 0 5 12 -100 0 -100 200 94 86
```
#### output
```
0
120
0
-40000
16168
```
## 10170:The Hotel with Infinite Rooms
```c=
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long s,d;//數字很大用long long
while(cin>>s>>d){//輸入測資
while(d>0){//d一直減s直到d小於等於0這時s就是答案
d=d-s;
s++;
}
cout<<s-1<<endl;//因為d小於0的那次迴圈s仍會加1所以必須減回來
}
return 0;
}
```
#### input
```
```
#### output
```
```
## 10242 - Fourth Point
```c=
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,t;
double x[4],y[4],ax,ay;
while(cin>>x[0]>>y[0]){//輸入測資
ax=x[0];//全部的點相加
ay=y[0];//全部的點相加
for(i=1;i<4;i++){//輸入測資
cin>>x[i]>>y[i];
ax+=x[i];//全部點相加
ay+=y[i];//全部點相加
}
for(i=0;i<4;i++){
for(t=i+1;t<4;t++){
if(x[i]==x[t]&&y[i]==y[t]){//找到一樣的點
cout<<fixed<<setprecision(3);
//精確到小數點下三位
cout<<ax-3*x[i]<<" ";//輸出x座標
cout<<ay-3*y[i]<<endl;//輸出y座標
}
}
}
}
return 0;
}
```
#### input
```
0.000 0.000 0.000 1.000 0.000 1.000 1.000 1.000
1.000 0.000 3.500 3.500 3.500 3.500 0.000 1.000
1.866 0.000 3.127 3.543 3.127 3.543 1.412 3.145
```
#### output
```
1.000 0.000
-2.500 -2.500
0.151 -0.398
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```
----------------------------------------------------
##
```c=
```
#### input
```
```
#### output
```
```