2018年5月24日 星期四

C++最佳銷售策略

【最佳銷售策略】



​小明的爸爸中年失業在家,小明一家頓時失去唯一的經濟來源。這時小明的媽媽想要擺攤賣包子貼補家用,媽媽的手藝很好可以做出好吃的包子,但是不太會經營包子攤,小明想要幫忙媽媽。小明觀察媽媽的包子賣得越便宜的話可以賣越多。假設媽媽做包子的成本為c,販售包子價格為p,每天可賣出m個包子,每天收益為profit = ( p – c ) * m。 例如: c=9,  p=13, m=500則profit = 2000。請寫出一個c++的程式能夠為小明找出profit最大的最佳銷售策略。

輸入說明:
輸入n+1行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表有n組測試資料。第二行開始有n組輸入,每一組輸入依順序為數字c,數字k, 接著連續輸入k對的定價及銷售量 p1,m1,p2,m2 .. ,p,mk

輸出說明:

輸出最佳銷售策略的p, m及profit值。輸出最後有跳行,每一行最後面不能有空格。
【輸入範例】【輸出範例】
2
9 2 13 500 30 100
12 2 15 1000 30 100
30 100 2100
15 1000 3000

#include<iostream>
using namespace std;

int main()
{
int n, b, c, k, max;
cin >> n;
for (int i = 0; i < n; i++)
{
int p[30] = { 0 };
int m[30] = { 0 };
int profit[30] = { 0 };
max = 0;
b = 0;
cin >> c >> k;
for (int j = 0; j < k; j++)
{
cin >> p[j] >> m[j];
profit[j] = (p[j] - c) * m[j];
}
for (int a = 0; a < k; a++)
{
if (profit[a] > max)
{
max = profit[a];
b = a;
}
}
cout << p[b] << " " << m[b] << " " << max << endl;
}
return 0;
}

2018年5月20日 星期日

C++質因數

【質因數】


問題描述:

非質數的正整數m可被分解成數個因數,而m可被其任何一個因數整除,例如:12的因數有6、4、3、2。但其中有些因數為質數稱為質因數。例如:12的質因數有3、2。

輸入說明:

程式的輸入包含n+1行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表有n筆測試資料。第二行開始輸入n筆測試資料m,m ≤ 30000000000000。

輸出說明:

由小到大輸出n筆測試資料m的質因數,質因數之間以空格隔開,每一筆輸出最後有跳行,每一行最後面不能有空格,如果m不能因數分解則輸出1。
【輸入範例】【輸出範例】
4
8
12
100000
19
2
2 3
2 5
1


  1. 時間需大致控制在1秒內
  2. 須考慮到大數運算
#include<iostream>
using namespace std;

int main()
{
int n,a,b;
long long m,c;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> m;
a = 0;
b = 0;
c = m;
for (long long j = 2;j * j <= c; j++)
{
a = 0;
if (j % 2 == 0 && j > 2)
continue;
if (c % j == 0)
{
while (c % j == 0)
{
c /= j;
a = 1;
}
}
if (a == 1)
{
if (b > 0)
cout << " ";
cout << j;
b++;
}
}
if (b == 0)
cout << 1;
else if (c > 1 && c != m)
{
if (b > 0)
cout << " ";
cout << c;
b++;
}
cout << endl;
}
return 0;
}

2018年5月13日 星期日

C++取最長共同子字串

Longest Common Substring
「最長共同子字串」。出現於每一個字串的子字串,長度最長者。可能有許多個。
s1: aabbebbccc
s2: aacccbbeb

 s1 s2   Longest Common Substring 就是 bbeb

#include <iostream>
#include <string>
using namespace std;

int main()
{
bool ischeck = false;
string s1, s2, lstr, sstr;
int a, b;
a = 0;
b = 0;
cin >> s1 >> s2;
lstr = s1;
sstr = s2;
if (s1.length() < s2.length())
{
lstr = s2;
sstr = s1;
}
for (int j = sstr.length(); j > 0 && !ischeck; j--)
{
for (int i = 0; i <= sstr.length() - j && !ischeck; i++)
{
lstr.find(sstr.substr(i, j));
if (string::npos != lstr.find(sstr.substr(i, j)))
{
cout << "Longest Common Substring:" << sstr.substr(i, j) << endl;
a = i;
b = j;
ischeck = true;
}
}
}
return 0;
}

C++取最長相同數列

請寫一個程式將一個正整數數列中最長的相同正整數數列輸出,負數代表輸入結束       
例如:
輸入: 1   2   2   3   3   3   5   9   9   -1
輸出: 3   3   3

#include <iostream>
using namespace std;

int main()
{
int i,n,t,k,a,b,c,x,y;
cin >> i;
a = i;
x = 0;
t = 1;

while (i > 0)
{
cin >> i;
if (i == a)
{
b = a;     //新相同數列
t++;       //計算個數
y = t;     //新長度
if (x > n)
    {
    n = x;  //舊跟前比大小
    }
    if (y > n)
    {
    k = y;  //max
    }
    if (y < n) 
    {
    b = c;  //換成舊數
    k = n;
    }
}
if (b != c)
{
n = t;  //儲存舊長度
}
if ((i != a) && i > 0)
{
a = i;  //new
c = b;  //保留前數
x = t;  //保留前長度
t = 1;  //歸1
}
}
for (int j = 0;j < k; j++) //k可轉成輸出長度
{
if (j > 0)
cout << " ";
    cout << b;
}
return 0;
}

C++業績獎金

【業績獎金】


問題描述 :

企業發放的獎金根據利潤提成。利潤(p)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成。

輸入說明 :

程式的輸入包含n+1行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表有n筆測試資料。第二行開始有n筆輸入p分別代表利潤。

輸出說明 :

輸出n筆測試資料計算出的應發放獎金。
【輸入範例】【輸出範例】
3
80000
120000
220000
8000
11500
18500

#include<iostream>
using namespace std;

int main()
{
int n,p;
double sum;
sum = 0;
cin >> n;
for (int i = 0;i < n; i++)
{
sum = 0;
cin >> p;
if (p <= 100000)
sum = p * 0.1;
else if (p > 100000 && p <= 200000)
sum = 100000 * 0.1 + (p - 100000) * 0.075;
else if (p > 200000 && p <= 400000)
sum = 100000 * 0.175 + (p - 200000) * 0.05;
else if (p > 400000 && p <= 600000)
sum = 100000 * 0.175 + 200000 * 0.05 + (p - 400000) * 0.03;
else if (p > 600000 && p <= 1000000)
sum = 100000 * 0.175 + 200000 * 0.08 + (p - 600000) * 0.015;
else
sum = 100000 * 0.175 + 200000 * 0.08 + 400000 * 0.015 + (p - 1000000) * 0.01;
cout << sum << endl;
}
return 0;
}

2018年5月12日 星期六

C++計算星期幾(蔡勒公式)

#include<iostream>
using namespace std;

int main()
{
int n,y,m,d,w,a,b,c;
w = 0;
a = 0;
b = 0;
c = 0;
cin >> n;
for (int i = 0;i < n; i++)
{
w = 0;
a = 0;
b = 0;
c = 0;
cin >> y >> m >> d;
if (m == 1 || m == 2)
{
a = (y - 1) % 100;
c = (y - 1) / 100;
m += 12;
}
else
{
a = y % 100;
c = y / 100;
}
b = a + (a / 4) + (c / 4) - (2 * c) + (26 * (m + 1) / 10) + d - 1;
if (b < 0)
w = (b % 7 + 7) % 7;
else
w = b % 7;
cout << w << endl;
}
return 0;
}

C++計算星期幾

【計算星期幾】



 問題描述 :

已知1900年1月1日為星期一,寫一個C++程式,以算出西元某年某月某日為星期幾。

輸入說明 :

程式的輸入包含n+1行數字,第一行包含一個正整數n,1 ≤ n ≤ 30,代表有n筆測試資料。第二行開始有n筆輸入yyyy, mm, dd分別代表西元年月日以空格隔開,其中的3000≧yyyy≧1900。

輸出說明 :

輸出n筆測試資料日期的星期順序,週日為0週一為1其餘依此類推。
【輸入範例】【輸出範例】
3
1900 1 1
2000 1 1
2018 5 5
1
6
6

#include<iostream>
using namespace std;

int main()
{
int n,y,m,d,sum;
int arr[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
cin >> n;
for (int i = 0;i < n; i++)
{
sum = 0;
cin >> y >> m >> d;
for (int j = 1900;j < y; j++)
{
if ((j % 4 == 0 && j % 100 != 0) || j % 400 == 0)
sum += 366;
else
sum += 365;
}
for (int k = 0;k < m - 1; k++)
{
if (((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) && k == 1)
sum++;
sum += arr[k];
}
sum += d;
cout << sum % 7 << endl;
}
return 0;
}

itsa [C_MM072-易]心得報數

#include <iostream>
using namespace std;

int main()
{
int n,N,K,P,a,c,j,tmp;
int *num;
cin >> n;
for (int i = 0;i < n; i++)
{
j = 0;
    c = 0;
cin >> N;
cin >> K;
tmp = N;
num = new int[N];
for (int a = 0;a < N; a++)
{
num[a] = 1;
}
for (int j = 0;j <= N; j++)
{
if (j == N)
{
j = 0;
}
if (num[j] == 0)
{

}
else if (num[j] == 1)
c++;
if (c == K)
{
num[j] = 0;
c = 0;
tmp--;
}
if (num[j] == 1 && tmp == 1)
    {
    P = j + 1;
    break;
        }
}
cout << P << endl;
delete []num;
}
return 0;
}

itsa [C_MM067-易]三角形面積

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int A, B, C, D;
cin >> A >> B;
cin >> C >> D;

int x, y;
long double m, a, c;
x = A;           
y = B;
m = (1.0 * (D - B)) / (C - A);
c = y - m * x;

int x1, y2;
long double y1, x2;
x1 = 0; 
y1 = c;
y2 = 0;
x2 = -c / m;
if (x2 < 0)
{
x2 = -x2;
}
if (y1 < 0)
{
y1 = -y1;
}
a = (x2 * y1) / 2;
cout << fixed << setprecision(5) << a << endl;
return 0;
}

itsa [C_AR04-易]邊緣偵測

#include<iostream>
using namespace std;

int main()
{
int N,n,m;
int arr[102][102] = {0};
cin >> N;
for (int i = 0;i < N; i++)
{
cin >> n >> m;
for (int a = 1;a <= n; a++)
{
for (int b = 1;b <= m; b++)
{
cin >> arr[a][b];
}
}
if (i > 0)
cout << endl;
for (int a = 1;a <= n; a++)
{
for (int b = 1;b <= m; b++)
{
if (arr[a][b] == 1)
{
if (arr[a - 1][b] == 0 || arr[a + 1][b] == 0 || arr[a][b - 1] == 0 || arr[a][b + 1] == 0)
cout << "0 ";
else
cout << "_ ";
}
else
cout << "_ ";
}
cout << endl;
}
}
return 0;
}

itsa63 5Decryption

#include<iostream>
#include<string>
using namespace std;

int main()
{
int sum,a,b;
string str;
sum = 0;
a = 0;
b = 0;
while(getline(cin,str))
{
sum += (str[0] - 48) * 1;
sum += (str[2] - 48) * 2;
sum += (str[3] - 48) * 3;
sum += (str[4] - 48) * 4;
sum += (str[6] - 48) * 5;
sum += (str[7] - 48) * 6;
sum += (str[8] - 48) * 7;
sum += (str[9] - 48) * 8;
sum += (str[10] - 48) * 9;
a = sum % 11;
if (str[12] != 'X')
b = str[12] - 48;
else
b = str[12] - 88;
if (a == b)
cout << "Pass" << endl;
else
cout << "Fail" << endl;
}
return 0;
}

itsa63 4正方形

#include<iostream>
using namespace std;

int main()
{
int n,sum,ans;
int m[1000];
sum = 0;
ans = 0;
cin >> n;
for (int i = 0;i < n; i++)
{
cin >> m[i];
sum += m[i];
}
for (int j = 0;j < n; j++)
{
if (m[j] > sum / 4)
ans = 1;
}
if (ans == 0)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}

itsa63 3衣服尺寸

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int height,weight,age,n;
double hat,clothes,pants;
hat = 0;
clothes = 0;
pants = 0;
n = 0;
while (n == 0)
{
cin >> height >> weight >> age;
hat = (1.0 * weight) / height * 41.2;
clothes = height * weight / 634.4;
pants = weight * 0.4;
if (age >= 28)
{
pants += ((age - 28) / 2) * 0.25;
if (age >= 30)
{
clothes += ((age - 30) / 5) * 0.15;
}
}
cout << fixed << setprecision(2) << hat << " " << clothes << " " << pants << endl;
cin >> n;
}
return 0;
}

itsa63 2括號匹配問題

#include<iostream>
#include<string>
using namespace std;

int main()
{
int n,a,b;
string str;
cin >> n;
cin.ignore();
for (int i = 0;i < n; i++)
{
a = 0;
b = 0;
getline(cin,str);
for (int j = 0;j < str.length(); j++)
{
if (str[j] == '(')
{
a++;
}
else
{
b++;
}
}
if (a == b)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

itsa63 1星座查詢

#include<iostream>
using namespace std;

int main()
{
int n,month,day;
cin >> n;
for (int i = 0;i < n; i++)
{
cin >> month >> day;
switch (month)
{
case 1:
if (day >= 1 && day <= 20)
cout << "Capricorn" << endl;
else if (day >= 21 && day <= 31)
cout << "Aquarius" << endl;
break;
case 2:
if (day >= 1 && day <= 18)
cout << "Aquarius" << endl;
else if (day >= 19 && day <= 28)
cout << "Pisces" << endl;
break;
case 3:
if (day >= 1 && day <= 20)
cout << "Pisces" << endl;
else if (day >= 21 && day <= 31)
cout << "Aries" << endl;
break;
case 4:
if (day >= 1 && day <= 20)
cout << "Aries" << endl;
else if (day >= 21 && day <= 31)
cout << "Taurus" << endl;
break;
case 5:
if (day >= 1 && day <= 21)
cout << "Taurus" << endl;
else if (day >= 22 && day <= 31)
cout << "Gemini" << endl;
break;
case 6:
if (day >= 1 && day <= 21)
cout << "Gemini" << endl;
else if (day >= 22 && day <= 31)
cout << "Cancer" << endl;
break;
case 7:
if (day >= 1 && day <= 22)
cout << "Cancer" << endl;
else if (day >= 23 && day <= 31)
cout << "Leo" << endl;
break;
case 8:
if (day >= 1 && day <= 23)
cout << "Leo" << endl;
else if (day >= 24 && day <= 31)
cout << "Virgo" << endl;
break;
case 9:
if (day >= 1 && day <= 23)
cout << "Virgo" << endl;
else if (day >= 24 && day <= 31)
cout << "Libra" << endl;
break;
case 10:
if (day >= 1 && day <= 23)
cout << "Libra" << endl;
else if (day >= 24 && day <= 31)
cout << "Scorpio" << endl;
break;
case 11:
if (day >= 1 && day <= 22)
cout << "Scorpio" << endl;
else if (day >= 23 && day <= 31)
cout << "Sagittarius" << endl;
break;
case 12:
if (day >= 1 && day <= 21)
cout << "Sagittarius" << endl;
else if (day >= 22 && day <= 31)
cout << "Capricorn" << endl;
break;
}
}
return 0;
}

itsa62 5Frog Jumping

#include<iostream>
using namespace std;

int main()
{
int t,X,N,L,a,b,count,ans;
int arr[60000] = {0};
cin >> t;
for (int i = 0;i < t; i++)
{
cin >> X;
cin >> N;
for (int j = 0;j < N; j++)
{
cin >> a >> b;
for (int x = a + 1;x <= b - 1; x++)
arr[x] = 1;
}
for (L = 1;L <= X; L++)
{
ans = 0;
count = L;
for (int y = count;y <= X*2; y += count)
{
if (arr[y] == 1)
{
ans = 1;
break;
}
else if (ans == 0 && y >= X && y <= X*2)
{
cout << L << endl;
break;
}
}
if (ans == 0)
break;
}
}
return 0;
}

itsa62 4盜墓驚魂

#include<iostream>
using namespace std;

int main()
{
int n,m,num,sum,flag;
int arr[8] = {0};
cin >> n;
for (int i = 0;i < n; i++)
{
num = 0;
sum = 0;
cin >> m;
int p[100];
int b[100] = {0};
for (int j = 0;j < m; j++)
{
cin >> p[j];
}
for (int k = 0;k < m; k++)
{
for (int x = 7;x >= 0; x--)
{
arr[x] = p[k] % 2;
p[k] /= 2;
}
int y = 1;
for (int x = 7;x >= 0; x--)
{
b[k] += arr[x] * y;
y *= 10;
}
}
for (int z = 0;z < m; z++)
{
if (z == 0)
num = b[z];
else
num = num ^ b[z];
}
flag = num ^ 0;
if (flag != 0)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}

itsa62 3猜數字的判別

#include <iostream>
using namespace std;

int main()
{
int n,i,a,a1,a2,a3,a4,A,B;
cin >> n;
for (int k = 0;k < n; k++)
{
cin >> a;
a4 = a % 10;
a3 = (a / 10) % 10;
a2 = ((a / 10) / 10) % 10;
a1 = ((a / 10) / 10) / 10;
A = 0;
B = 0;
cin >> i;
for (int j = 0;j < 4; j++)
{
if (i % 10 == a1)
{
if (j == 3)
{
A++;
}
else
{
B++;
}
}
else if (i % 10 == a2)
{
if (j == 2)
{
A++;
}
else
{
B++;
}
}
else if (i % 10 == a3)
{
if (j == 1)
{
A++;
}
else
{
B++;
}
}
else if (i % 10 == a4)
{
if (j == 0)
{
A++;
}
else
{
B++;
}
}
i /= 10;
if (j == 3)
    {
    cout << A << "a" << B << "b" << endl;
    }
}
}
return 0;
}

itsa62 2高頻字元

#include<iostream>
#include<string>
using namespace std;

int main()
{
string str;
int n,count,x,y,z,j;
cin >> n;
cin.ignore();
for (int i = 0;i < n; i++)
{
x = 0;
y = 0;
z = 0;
j = 0;
count = 0;
getline(cin,str);
for (int a = 0;a < str.length(); a++)
{
count = 0;
for (int b = 0;b < str.length(); b++)
{
if (str[a] == str[b])
count++;
}
if (count > x)
{
if (count > y)
{
z = count;
j = a;
}
y = count;
}
if (a == str.length() - 1)
{
cout << str[j] << endl;
break;
}
x = count;
}
}
return 0;
}

itsa62 1圈圈叉叉

#include<iostream>
using namespace std;

int main()
{
int count,sum,a,b,n;
int arr[3][3];
a = 0;
b = 0;
cin >> n;
for (int k = 0;k < n; k++)
{
a = 0;
b = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < 3; i++)
{
count = 0;
for (int j = 0; j < 3; j++)
{
count += arr[i][j];
}
if (count == 0 || count == 3)
{
a = 1;
break;
}
}
for (int j = 0; j < 3; j++)
{
count = 0;
for (int i = 0; i < 3; i++)
{
count += arr[i][j];
}
if (count == 0 || count == 3)
{
a = 1;
break;
}
}
count = 0;
sum = 0;
count = arr[0][0] + arr[1][1] + arr[2][2];
sum = arr[0][2] + arr[1][1] + arr[2][0];
if (count == 0 || count == 3)
b = 1;
else if (sum == 0 || sum == 3)
b = 1;
if (a == 1 && b == 0)
cout << "True" << endl;
else if (b == 1 && a == 0)
cout << "True" << endl;
else
cout << "False" << endl;
}
return 0;
}

itsa61 5Partition of set into labeled set

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void replaceD(string &str)
{
int i = 0;
while(i < str.length())
{
if (str[i] == ',')
str[i] = ' ';
i++;
}
}
long double f(int a)
{
if (a == 1 || a == 0)
return 1;
else
return (a * f(a - 1));
}
long double F(int m,int n)
{
if (n == 1)
return 1;
else if (n == 0 || n > m)
return 0;
return f(m) / f(m - n);
}
int main()
{
int t,m,n;
string str;
cin >> t;
getline(cin,str);
for (int i = 0;i < t; i++)
{
getline(cin,str);
replaceD(str);
stringstream ss(str);
ss >> m >> n;
cout << F(m,n) << endl;
}
return 0;
}

itsa61 4一個媽媽的旅行

#include<iostream>
using namespace std;

int arr[1000][1000];
int dist[1000][1000];

int main()
{
int N,M,count;
count = 0;
cin >> N;
for (int i = 0;i < N; i++)
{
cin >> M;
count = 0;

for (int a = 0;a < M; a++)
{
for (int b = 0;b < M; b++)
{
cin >> arr[a][b];
}
}

for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
{
if (arr[i][j] == 1)
dist[i][j] = 1;
else
dist[i][j] = 999999999;
}

for (int i = 0; i < M; i++)
{
for (int k = 0; k < M; k++)
{
for (int j = 0; j < M; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
cout << dist[0][M - 1]<<endl;
}
return 0;
}

itsa61 2找錢問題

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void replaceD(string &str)
{
int i = 0;
while(i < str.length())
{
if (str[i] == ',')
str[i] = ' ';
i++;
}
}

int main()
{
int N;
string str;
cin >> N;
getline(cin,str);
for (int i = 0;i < N; i++)
{
int k,a1,a2,sum,b1,b2,b3,b4,c;
getline(cin,str);
replaceD(str);
stringstream ss(str);
ss >> k >> a1 >> a2;
b1 = 0;
b2 = 0;
b3 = 0;
b4 = 0;
c = 1;
if (a1 == 1)
{
sum = k - (a2 * 17);
}
else if (a1 == 2)
{
sum = k - (a2 * 25);
}
b1 = sum / 50;
b2 = (sum % 50) / 10;
b3 = ((sum % 50) % 10) / 5;
b4 = ((sum % 50) % 10) % 5;
for (int j = 0;j < 4; j++)
{
if (j == 0 && b4 != 0)
{
cout << "Coin 1:" << b4;
c++;
}
else if (j == 1 && b3 != 0)
{
if (c - 1 > 0)
    cout << ",";
cout << "Coin 5:" << b3;
c++;
}
else if (j == 2 && b2 != 0)
{
if (c - 1 > 0)
    cout << ",";
cout << "Coin 10:" << b2;
c++;
}
else if (j == 3 && b1 != 0)
{
if (c - 1 > 0)
    cout << ",";
cout << "Coin 50:" << b1;
c++;
}
}
cout << endl;
}
return 0;
}

itsa61 1計算電費

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int N,a;
double x,y;
cin >> N;
for (int i = 0;i < N; i++)
{
cin >> a;
if (a > 0)
{
if (a <= 120)
    {
    x = a * 2.1;
    y = a * 2.1;
    }
    else if (a > 120 && a <= 330)
        {
            x = 120 * 2.1 + (a - 120) * 3.02;
            y = 120 * 2.1 + (a - 120) * 2.68;
        }
        else if (a > 330 && a <= 500)
        {
        x = 120 * 2.1 + 210 * 3.02+ (a - 330) * 4.39;
        y = 120 * 2.1 + 210 * 2.68+ (a - 330) * 3.61;
        }
        else if (a > 500 && a <= 700)
        {
        x = 120 * 2.1 + 210 * 3.02+ 170 * 4.39+ (a - 500) * 4.97;
        y = 120 * 2.1 + 210 * 2.68+ 170 * 3.61+ (a - 500) * 4.01;
        }
        else if (a > 700)
        {
        x = 120 * 2.1 + 210 * 3.02+ 170 * 4.39+ 200 * 4.97+ (a - 700) * 5.63;
        y = 120 * 2.1 + 210 * 2.68+ 170 * 3.61+ 200 * 4.01+ (a - 700) * 4.50;
        }
        cout << "Summer months:" << fixed << setprecision(2) << x << endl;
        cout << "Non-Summer months:" << fixed << setprecision(2) << y << endl;
}
}
return 0;
}

itsa60 4計算分數

#include<iostream>
#include<string>
#include<sstream>
#include<cmath>
using namespace std;
void replaceD(string &str)
{
int i = 0;
while(i < str.length())
{
if (str[i] == '/' || str[i] == ',')
str[i] = ' ';
i++;
}
}
int gcd(int a1, int a2)
{
while (a1 != a2)
{
if (a1 > a2)
a1 = a1 - a2;
else
a2 = a2 - a1;
}
return a1;
}

int main()
{
int S;
int a1, a2, b1, b2, s1, s2;
string str;
cin >> S;
getline(cin, str);
for (int i = 0; i < S; i++)
{
getline(cin, str);
replaceD(str);
stringstream ss(str);
ss >> a1 >> a2 >> b1 >> b2;
s2 = a2 * b2;
s1 = a1 * b2 + a2 * b1;
cout << s1 / gcd(s1, s2) << "/" << s2 / gcd(s1, s2) << endl;
}
return 0;
}

itsa60 3吃麵包

#include<iostream>
using namespace std;

int main()
{
int S,N,M,Q,R;
static int Narr[100001];
cin >> S;

for (int i = 0; i < S; i++)
{
cin >> N >> M;
for (int j = 0; j < N; j++)
cin >> Narr[j];
for (int j = 0; j < M; j++)
{
long long sum = 0;
cin >> Q >> R;
for (int k = Q; k <= R; k++)
sum += Narr[k - 1];
cout << sum << endl;
}
}
//system("pause");
return 0;
}

itsa60 2解讀神秘的密碼

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
int N,ilength;
char array[50] = {0};
cin >> N;
for (int i = 0;i < N; i++)
{
cin >> array;
ilength = strlen(array);
for(int j = ilength - 1;j >= 0; j--)

            cout << array[j];
        }
        cout << endl;
}
return 0;
}

itsa60 1平閏年判定

#include<iostream>
using namespace std;

int main()
{
int N,year;
cin >> N;

for (int i = 0;i < N; i++)
{
cin >> year;
if (year % 400 == 0)
{
cout << "Bissextile Year" << endl;
}
else if (year % 4 == 0 && year % 100 != 0)
{
cout << "Bissextile Year" << endl;
}
else
{
cout << "Common Year" << endl;
}
}
return 0;
}

itsa59 5Factorize a positive integer

#include<iostream>
using namespace std;

int main()
{
int N,P,q;
cin >> N;
for (int k = 0;k < N; k++)
{
q = 0;
cin >> P;
for (int i = 2; i < P; i++)
{
if (P % i == 0)
{
q++;
}
}
if (q % 2 == 0)
cout << q / 2 << endl;
else if (q == 1)
cout << q << endl;
else
cout << (q + 1) / 2 << endl;
}
return 0;
}

itsa59 3我該怎麼走

#include <iostream>
using namespace std;

int main()
{
int m;
int arr[10][10];
cin >> m;
for (int a = 0;a < m; a++)
{
int degree[10] = {0};
int dist[10][10];
int path[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cin >> arr[i][j];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
degree[i] += arr[i][j];
}
for (int i = 0; i < 10; i++)
dist[i][i] = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
if (arr[i][j] == 1)
dist[i][j] = degree[j];
else
dist[i][j] = 999999999;
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr[i][j] == 1)
path[i][j] = i;
}
}
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
for (int j = 0; j < 10; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = path[k][j];
}
}
}
}
int End = 9;
int num[10] = {0};
for (int j = 0; j < 10; j++)
{
int node = path[0][End];
num[node] = 1;
End = node;
if (node == 0) {
break;
}
}
for (int i = 0;i < 10; i++)
if (num[i] != 0)
cout << i << "-";
cout << 9;
cout << " : " << dist[0][9] + degree[0] << endl;
}
return 0;
}

itsa59 2迴文結構數字

#include <iostream>
using namespace std;

int main()
{
int m,a,b,x,y,z,k;
int arr[100] = {0};
cin >> m;
for (int i = 0;i < m; i++)
{
k = 0;
cin >> a >> b;
if (a > 0 && a < b && b < 500)
{
for (int j = a;j <= b; j++)
    {
x = j % 10;
y = (j / 10) % 10;
    z = ((j / 10) / 10) % 10;
    if (j < 10)
    {
    arr[k] = j;
    k++;
}
else if ((j >= 10) & (j < 100))
{
if (x == y)
{
    arr[k] = j;
    k++;
}
}
else if ((j >= 100) & (j < 500))
{
if (x == z)
{
arr[k] = j;
k++;
}
}
j++;
}
for (int t = 0;t < k; t++)
{
cout << arr[t];
if (t < k - 1)
cout << " ";
}
if (k > 0)
cout << endl;
else if (k == 0)
cout << "0" << endl;
}
}
return 0;
}

itsa59 1買獎品

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
int n, T, m, k,sum;
int a[100];
cin >> n;   
for (int i = 0; i < n; i++)
{
cin >> T >> m >> k;
sum = 0;
for (int j = 0; j < k; j++)
{
cin >> a[j];
}
sort(a,a + k);
for (int j = 0; j < m; j++)
{
sum += a[j];
if (sum > T)
break;
}
if (sum > T)
{
cout << "Impossible" << endl;
}
else
{
cout << sum << endl;
}
}
return 0;
}

itsa58 5Set partition

#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
void replaceD(string &str)
{
int i = 0;
while(i < str.length())
{
if (str[i] == ',')
str[i] = ' ';
i++;
}
}
long double F(int m,int n)
{
if (n == 1|| m == n)
return 1;
else if (n == 0)
return 0;
return F(m - 1,n - 1) + n * F(m - 1,n);
}
int main()
{
int N,m,n;
long long sum = 0;
string str;
cin >> N;
getline(cin,str);
for (int i = 0;i < N; i++)
{
getline(cin,str);
replaceD(str);
stringstream ss(str);
ss >> m >> n;
sum = F(m,n);
cout << fixed << setprecision(0) << sum << endl;
}
return 0;
}

itsa基 40 ISBN驗證

#include<iostream>
using namespace std;

int main()
{
int arr[10];
char input;
for(int j = 0;j < 10; j++) 
    { 
        cin >> input; 
        if(input != 'X') 
            arr[j] = input- '0'; 
        else 
            arr[j] = input- 78; 
    } 
    for(int k = 0;k < 2; k++) 
    { 
        for(int j = 1;j < 10; j++) 
        { 
            arr[j] = arr[j - 1] + arr[j]; 
        } 
    } 
    if(arr[9] % 11 == 0) 
        cout<< "YES" << endl; 
    else 
        cout<< "NO" << endl;
return 0;
}

itsa基 39 考試測驗

#include <iostream>
using namespace std;

int main ()
{
int N,sum;
int a,b,c;
cin >> N;

for (int i = 0; i < N; i ++)
{
cin >> a >> b >> c;
sum = a + b + c;
if (a >= 60 && b >= 60 && c >= 60)
{
cout << "P" << endl;
}
else if (((a < 60 && b >= 60 && c >= 60) || (b < 60 && c >= 60 && a >= 60) || (c < 60 && a >= 60 && b >= 60)) && sum >= 220)
{
cout << "P" << endl;
}
else if (((a < 60 && b >= 60 && c >= 60) || (b < 60 && c >= 60 && a >= 60) || (c < 60 && a >= 60 && b >= 60)) && sum < 220)
{
cout << "M" << endl;
}
else if (((a < 60 && b < 60 && c >= 80) || (b < 60 && c < 60 && a >= 80) || (c < 60 && a < 60 && b >= 80)))
{
cout << "M" << endl;
}
else
{
cout << "F" << endl;
}
}
return 0;
}

itsa基 38 計算電費

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int a;
double x,y;
cin >> a;
if (a > 0)
{
if (a <= 120)
{
x = a * 2.1;
y = a * 2.1;
}
else if (a > 120 && a <= 330)
    {
        x = 120 * 2.1 + (a - 120) * 3.02;
        y = 120 * 2.1 + (a - 120) * 2.68;
    }
    else if (a > 330 && a <= 500)
    {
    x = 120 * 2.1 + 210 * 3.02+ (a - 330) * 4.39;
    y = 120 * 2.1 + 210 * 2.68+ (a - 330) * 3.61;
    }
    else if (a > 500 && a <= 700)
    {
    x = 120 * 2.1 + 210 * 3.02+ 170 * 4.39+ (a - 500) * 4.97;
    y = 120 * 2.1 + 210 * 2.68+ 170 * 3.61+ (a - 500) * 4.01;
    }
    else if (a > 700)
    {
    x = 120 * 2.1 + 210 * 3.02+ 170 * 4.39+ 200 * 4.97+ (a - 700) * 5.63;
    y = 120 * 2.1 + 210 * 2.68+ 170 * 3.61+ 200 * 4.01+ (a - 700) * 4.50;
    }
    cout << "Summer months:" << fixed << setprecision(2) << x << endl;
    cout << "Non-Summer months:" << fixed << setprecision(2) << y << endl;
}
return 0;
}