1.將羅馬數字存到陣列,區分五跟十的倍數。
2.以五為分界,前四做前輸出,後四做後輸出。
3.以迴圈做對應輸出。
注意:陣列存的方式會改變輸出時的陣列編號。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string one[4] = { "I","X","C","M" };
string five[3] = { "V","L","D" };
string s;
int n;
cin >> n;
getline(cin, s);
for (int k = 0; k < n; k++)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
int subNum = s[i] - '0';
int num = 0;
if (subNum < 5)
num = subNum % 4;
else if (subNum > 5)
num = (subNum - 5) % 4;
if (subNum >= 1 && subNum <= 4)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << five[s.length() - i - 1];
}
else
{
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
else if (subNum == 5)
cout << five[s.length() - i - 1];
else if (subNum >= 6 && subNum <= 9)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << one[s.length() - i];
}
else
{
cout << five[s.length() - i - 1];
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
}
cout << endl;
}
return 0;
}
2018年9月13日 星期四
itsa66 3字串切割
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ':' || s[i] == ',' || s[i] == ';')
s[i] = ' ';
i++;
}
}
int main()
{
int N, x = 0;
string s, str[100];
cin >> N;
getline(cin, s);
for (int i = 0; i < N; i++)
{
x = 0;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> str[x])
x++;
cout << "Tokens found:\n";
for (int j = 0; j < x; j++)
cout << str[j] << endl;
}
return 0;
}
#include<string>
#include<sstream>
using namespace std;
void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ':' || s[i] == ',' || s[i] == ';')
s[i] = ' ';
i++;
}
}
int main()
{
int N, x = 0;
string s, str[100];
cin >> N;
getline(cin, s);
for (int i = 0; i < N; i++)
{
x = 0;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> str[x])
x++;
cout << "Tokens found:\n";
for (int j = 0; j < x; j++)
cout << str[j] << endl;
}
return 0;
}
itsa66 2未知數
1.無條件捨去,引用<cmath>的floor。
2.字串轉整數或小數,要先轉字元, "c_str()"可以將字串轉字元,但要看情況使用。
例:1111 2222 333
s[ 0 1 2 ]
s[0][1] = 1 這時有[][]就不能用s[0][1].c_str() 要不然會錯喔!
注意:可以為小數,運算要正確。
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
double ans;
string s1, s2, s3, s4, s5;
while (cin >> s1 >> s2 >> s3 >> s4 >> s5)
{
ans = 0;
if (s1 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s3.c_str());
else if (s2 == "-")
ans = atof(s5.c_str()) + atof(s3.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s3.c_str());
else
ans = atof(s5.c_str()) * atof(s3.c_str());
}
else if (s3 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s1.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s5.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s1.c_str());
else
ans = atof(s1.c_str()) / atof(s5.c_str());
}
else
{
if (s2 == "+")
ans = atof(s1.c_str()) + atof(s3.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s3.c_str());
else if (s2 == "*")
ans = atof(s1.c_str()) * atof(s3.c_str());
else
ans = atof(s1.c_str()) / atof(s3.c_str());
}
ans = floor(ans * 10) / 10;
cout << fixed << setprecision(1) << ans << endl;
}
return 0;
}
2.字串轉整數或小數,要先轉字元, "c_str()"可以將字串轉字元,但要看情況使用。
例:1111 2222 333
s[ 0 1 2 ]
s[0][1] = 1 這時有[][]就不能用s[0][1].c_str() 要不然會錯喔!
注意:可以為小數,運算要正確。
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
double ans;
string s1, s2, s3, s4, s5;
while (cin >> s1 >> s2 >> s3 >> s4 >> s5)
{
ans = 0;
if (s1 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s3.c_str());
else if (s2 == "-")
ans = atof(s5.c_str()) + atof(s3.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s3.c_str());
else
ans = atof(s5.c_str()) * atof(s3.c_str());
}
else if (s3 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s1.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s5.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s1.c_str());
else
ans = atof(s1.c_str()) / atof(s5.c_str());
}
else
{
if (s2 == "+")
ans = atof(s1.c_str()) + atof(s3.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s3.c_str());
else if (s2 == "*")
ans = atof(s1.c_str()) * atof(s3.c_str());
else
ans = atof(s1.c_str()) / atof(s3.c_str());
}
ans = floor(ans * 10) / 10;
cout << fixed << setprecision(1) << ans << endl;
}
return 0;
}
itsa66 1過半元素
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int arr[30];
int N, count, a;
string str;
while (getline(cin, str))
{
a = 0;
N = 0;
stringstream ss(str);
while (ss >> arr[N])
N++;
N--;
for (int i = 0; i < N; i++)
{
count = 0;
for (int j = 0; j < N; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > N / 2)
{
a = 1;
cout << arr[i] << endl;
break;
}
}
if (count <= N / 2 && a == 0)
cout << "NO" << endl;
}
return 0;
}
#include<string>
#include<sstream>
using namespace std;
int main()
{
int arr[30];
int N, count, a;
string str;
while (getline(cin, str))
{
a = 0;
N = 0;
stringstream ss(str);
while (ss >> arr[N])
N++;
N--;
for (int i = 0; i < N; i++)
{
count = 0;
for (int j = 0; j < N; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > N / 2)
{
a = 1;
cout << arr[i] << endl;
break;
}
}
if (count <= N / 2 && a == 0)
cout << "NO" << endl;
}
return 0;
}
2018年9月8日 星期六
itsa [C_AR030-中]文字頻率分析
註: tUp英文大寫次數 tLow英文小寫次數 tw字次數
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int c, w, count = 0, x = 0, y = 0, z = 0, g = 0, a = 0, b = 0, check = 0;
int tUp[26] = { 0 }, tLow[26] = { 0 }, tw[100] = { 0 };
string prefix, suffix, s, str[100], pre[100], suf[100], wo[100];
cin >> prefix;
cin >> suffix;
cin >> c;
cin >> w;
getline(cin, s);
getline(cin, s);
stringstream ss(s);
while (ss >> str[x])
x++;
for (int i = 0; i < x; i++)
{
check = 0;
count = 0;
if (str[i].length() >= prefix.length())
{
for (int j = 0; j < prefix.length(); j++)
{
if (str[i][j] == prefix[j])
count++;
else
break;
}
if (count == prefix.length())
{
for (int k = 0; k < y; k++)
if (pre[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
pre[y++] = str[i];
}
}
}
for (int i = 0; i < x; i++)
{
a = 0;
count = 0;
check = 0;
if (str[i].length() >= suffix.length())
{
for (int j = str[i].length() - suffix.length(); j < str[i].length(); j++)
{
if (str[i][j] == suffix[a++])
count++;
else
break;
}
if (count == suffix.length())
{
for (int k = 0; k < z; k++)
if (suf[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
suf[z++] = str[i];
}
}
}
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < s.length(); j++)
{
if (s[i] == s[j])
{
if (s[i] >= 97 && s[i] <= 122)
tLow[s[i] - 'a']++;
else if (s[i] >= 65 && s[i] <= 90)
tUp[s[i] - 'A']++;
s[j] = ' ';
}
}
}
for (int i = 0; i < x; i++)
{
b = 0;
check = 0;
for (int j = 0; j < x; j++)
{
count = 0;
if (str[i].length() == str[j].length())
{
for (int k = 0; k < str[i].length(); k++)
{
if (str[i][k] == str[j][k])
count++;
else
break;
}
}
if (count == str[i].length())
b++;
}
if (b >= w)
{
for (int k = 0; k < g; k++)
if (wo[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
{
wo[g] = str[i];
tw[g] = b;
g++;
}
}
}
for (int i = 0; i < g - 1; i++)
{
for (int j = i + 1; j < g; j++)
{
if (wo[i].compare(wo[j]) == 1)
{
string tmp = wo[i];
wo[i] = wo[j];
wo[j] = tmp;
int temp = tw[i];
tw[i] = tw[j];
tw[j] = temp;
}
}
}
cout << "prefix of " << prefix << ":" << endl;
for (int i = 0; i < y; i++)
cout << pre[i] << endl;
cout << "suffix of " << suffix << ":" << endl;
for (int i = 0; i < z; i++)
cout << suf[i] << endl;
cout << "character frequency over " << c << ":" << endl;
for (int i = 0; i < 26; i++)
{
if (tUp[i] >= c)
cout << (char)(i + 'A') << ',' << tUp[i] << endl;
}
for (int i = 0; i < 26; i++)
{
if (tLow[i] >= c)
cout << (char)(i + 'a') << ',' << tLow[i] << endl;
}
cout << "word frequency over " << w << ":" << endl;
for (int i = 0; i < g; i++)
cout << wo[i] << ',' << tw[i] << endl;
return 0;
}
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int c, w, count = 0, x = 0, y = 0, z = 0, g = 0, a = 0, b = 0, check = 0;
int tUp[26] = { 0 }, tLow[26] = { 0 }, tw[100] = { 0 };
string prefix, suffix, s, str[100], pre[100], suf[100], wo[100];
cin >> prefix;
cin >> suffix;
cin >> c;
cin >> w;
getline(cin, s);
getline(cin, s);
stringstream ss(s);
while (ss >> str[x])
x++;
for (int i = 0; i < x; i++)
{
check = 0;
count = 0;
if (str[i].length() >= prefix.length())
{
for (int j = 0; j < prefix.length(); j++)
{
if (str[i][j] == prefix[j])
count++;
else
break;
}
if (count == prefix.length())
{
for (int k = 0; k < y; k++)
if (pre[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
pre[y++] = str[i];
}
}
}
for (int i = 0; i < x; i++)
{
a = 0;
count = 0;
check = 0;
if (str[i].length() >= suffix.length())
{
for (int j = str[i].length() - suffix.length(); j < str[i].length(); j++)
{
if (str[i][j] == suffix[a++])
count++;
else
break;
}
if (count == suffix.length())
{
for (int k = 0; k < z; k++)
if (suf[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
suf[z++] = str[i];
}
}
}
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < s.length(); j++)
{
if (s[i] == s[j])
{
if (s[i] >= 97 && s[i] <= 122)
tLow[s[i] - 'a']++;
else if (s[i] >= 65 && s[i] <= 90)
tUp[s[i] - 'A']++;
s[j] = ' ';
}
}
}
for (int i = 0; i < x; i++)
{
b = 0;
check = 0;
for (int j = 0; j < x; j++)
{
count = 0;
if (str[i].length() == str[j].length())
{
for (int k = 0; k < str[i].length(); k++)
{
if (str[i][k] == str[j][k])
count++;
else
break;
}
}
if (count == str[i].length())
b++;
}
if (b >= w)
{
for (int k = 0; k < g; k++)
if (wo[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
{
wo[g] = str[i];
tw[g] = b;
g++;
}
}
}
for (int i = 0; i < g - 1; i++)
{
for (int j = i + 1; j < g; j++)
{
if (wo[i].compare(wo[j]) == 1)
{
string tmp = wo[i];
wo[i] = wo[j];
wo[j] = tmp;
int temp = tw[i];
tw[i] = tw[j];
tw[j] = temp;
}
}
}
cout << "prefix of " << prefix << ":" << endl;
for (int i = 0; i < y; i++)
cout << pre[i] << endl;
cout << "suffix of " << suffix << ":" << endl;
for (int i = 0; i < z; i++)
cout << suf[i] << endl;
cout << "character frequency over " << c << ":" << endl;
for (int i = 0; i < 26; i++)
{
if (tUp[i] >= c)
cout << (char)(i + 'A') << ',' << tUp[i] << endl;
}
for (int i = 0; i < 26; i++)
{
if (tLow[i] >= c)
cout << (char)(i + 'a') << ',' << tLow[i] << endl;
}
cout << "word frequency over " << w << ":" << endl;
for (int i = 0; i < g; i++)
cout << wo[i] << ',' << tw[i] << endl;
return 0;
}
2018年9月2日 星期日
itsa [C_AR029-難]文字編碼
#include<iostream>
#include<string>
using namespace std;
int main()
{
int m = 0, x = 0, check = 0;
char arr[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
arr[i][j] = ' ';
string s;
getline(cin, s);
for (int i = 1; i * i <= s.length(); i++)
m = i;
if (m * m < s.length())
m++;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = s[x++];
if (x == s.length())
break;
}
if (x == s.length())
break;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
cout << arr[j][i];
cout << endl;
return 0;
}
#include<string>
using namespace std;
int main()
{
int m = 0, x = 0, check = 0;
char arr[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
arr[i][j] = ' ';
string s;
getline(cin, s);
for (int i = 1; i * i <= s.length(); i++)
m = i;
if (m * m < s.length())
m++;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = s[x++];
if (x == s.length())
break;
}
if (x == s.length())
break;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
cout << arr[j][i];
cout << endl;
return 0;
}
2018年8月23日 星期四
itsa [RU22-中]Dice throwing
#include<iostream>
using namespace std;
long long gcd(long long x, long long y)
{
if (x == 0)
return y;
if (y == 0)
return x;
while (y != 0)
{
long long tmp = y;
y = x % y;
x = tmp;
}
return x;
}
int main()
{
int N, X;
long long a, b, GCD;
long long diceP[11][71], total[11] = { 0 };
for (int i = 0; i < 11; i++)
for (int j = 0; j < 71; j++)
diceP[i][j] = 0;
diceP[0][0] = 1;
total[0] = 1;
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= i; j--)
{
for (int k = 1; k <= 6 && j - k >= 0; k++)
diceP[i][j] += diceP[i - 1][j - k];
}
total[i] = total[i - 1] * 6;
}
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= 0; j--)
diceP[i][j] += diceP[i][j + 1];
}
while (cin >> N >> X)
{
if (N == 0 && X == 0)
break;
a = diceP[N][X];
b = total[N];
GCD = gcd(a, b);
a /= GCD;
b /= GCD;
if (b == 1)
cout << a << endl;
else
cout << a << "/" << b << endl;
}
return 0;
}
using namespace std;
long long gcd(long long x, long long y)
{
if (x == 0)
return y;
if (y == 0)
return x;
while (y != 0)
{
long long tmp = y;
y = x % y;
x = tmp;
}
return x;
}
int main()
{
int N, X;
long long a, b, GCD;
long long diceP[11][71], total[11] = { 0 };
for (int i = 0; i < 11; i++)
for (int j = 0; j < 71; j++)
diceP[i][j] = 0;
diceP[0][0] = 1;
total[0] = 1;
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= i; j--)
{
for (int k = 1; k <= 6 && j - k >= 0; k++)
diceP[i][j] += diceP[i - 1][j - k];
}
total[i] = total[i - 1] * 6;
}
for (int i = 1; i <= 10; i++)
{
for (int j = 6 * i; j >= 0; j--)
diceP[i][j] += diceP[i][j + 1];
}
while (cin >> N >> X)
{
if (N == 0 && X == 0)
break;
a = diceP[N][X];
b = total[N];
GCD = gcd(a, b);
a /= GCD;
b /= GCD;
if (b == 1)
cout << a << endl;
else
cout << a << "/" << b << endl;
}
return 0;
}
訂閱:
文章 (Atom)