- C++
信奥1班2024暑假集训day5-真题算法专项演练-字符串算法
- 2024-7-4 15:47:56 @
1922:【03NOIP普及组】乒乓球
#include <iostream>
#include <algorithm>
using namespace std;
void work(string str, int score)
{
int a = 0, b = 0;
for (int i = 0; i < str.size() && str[i] != 'E'; i ++ )
{
if (str[i] == 'W') a ++ ;
else b ++ ;
if (max(a, b) >= score && abs(a - b) >= 2)
{
printf("%d:%d\n", a, b);
a = b = 0;
}
}
printf("%d:%d\n", a, b);
}
int main()
{
string str, s;
while (cin >> s) str += s;
work(str, 11);
puts("");
work(str, 21);
return 0;
}
1942:【08NOIP普及组】ISBN号码
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
cin >> str;
int sum = 0;
for (int i = 0, j = 1; i + 1 < str.size(); i ++ )
if (str[i] != '-')
{
sum += (str[i] - '0') * j;
j ++ ;
}
sum %= 11;
char c = 'X';
if (sum < 10) c = '0' + sum;
if (c == str.back()) puts("Right");
else
{
str.back() = c;
cout << str << endl;
}
return 0;
}
1977:【08NOIP普及组】立体图
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500;
int n, m;
char box[6][8] = {
"..+---+",
"./ /|",
"+---+ |",
"| | +",
"| |/.",
"+---+.."
};
char g[N][N];
int h[N][N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &h[i][j]);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
g[i][j] = '.';
int up = N, right = 0;
for (int x = 0; x < n; x ++ )
for (int y = 0; y < m; y ++ )
for (int z = 0; z < h[x][y]; z++)
{
int X = 499 - 2 * (n - 1 - x) - 3 * z;
int Y = 2 * (n - 1 - x) + 4 * y;
up = min(up, X - 5);
right = max(right, Y + 6);
for (int a = 0; a < 6; a++)
for (int b = 0; b < 7; b++)
if (box[a][b] != '.')
g[X - 5 + a][Y + b] = box[a][b];
}
for (int i = up; i < N; i++)
{
for (int j = 0; j <= right; j++) printf("%c", g[i][j]);
puts("");
}
return 0;
}
1945:【09NOIP普及组】多项式输出
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
bool is_first = true;
for (int i = n; i >= 0; i -- )
{
int a;
cin >> a;
if (!a) continue;
if (!is_first && a > 0) printf("+");
else if (a < 0) printf("-");
if (abs(a) != 1 || !i) printf("%d", abs(a));
if (i) printf("x");
if (i > 1) printf("^%d", i);
is_first = false;
}
return 0;
}
1953:【11NOIP普及组】数字反转
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string num;
cin >> num;
if (num[0] == '-') cout << '-', num = num.substr(1);
reverse(num.begin(), num.end());
while (num.size() > 1 && num[0] == '0') num = num.substr(1);
cout << num << endl;
return 0;
}
int方法:
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
if(n<0){ //判断负数
cout<<'-';
n=-n;
}
int res=0;
for(;n;n/=10) res=res*10+n%10;//数字反转直接忽略前导0,方便
cout<<res;
return 0;
}
1954:【11NOIP普及组】统计单词数
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string p, s;
void to_lower(string& s)
{
for (auto& c: s)
c = tolower(c);
}
int main()
{
getline(cin, p);
getline(cin, s);
to_lower(p), to_lower(s);
int cnt = 0, k = -1;
for (int i = 0; i < s.size(); i ++ )
{
if (s[i] == ' ') continue;
int j = i + 1;
while (j < s.size() && s[j] != ' ') j ++ ;
if (p == s.substr(i, j - i))
{
cnt ++ ;
if (k == -1) k = i;
}
i = j;
}
if (!cnt) puts("-1");
else cout << cnt << ' ' << k << endl;
return 0;
}
1415:【17NOIP普及组】图书管理员
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010, INF = 1e8;
int n, m;
int id[N];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> id[i];
while (m -- )
{
int l;
string num;
cin >> l >> num;
int res = INF;
for (int i = 0; i < n; i ++ )
{
string s = to_string(id[i]);
if (s.size() >= l && s.substr(s.size() - l) == num)
res = min(res, id[i]);
}
if (res == INF) res = -1;
cout << res << endl;
}
return 0;
}
1978:【18NOIP普及组】标题统计
#include <iostream>
using namespace std;
int main()
{
char c;
int s = 0;
while (cin >> c) s ++ ;
cout << s << endl;
return 0;
}
0 条评论
目前还没有评论...