这里有18道精选题目!
题目难度由浅入深,请先阅读代码,思考其中的逻辑,再点击按钮核对答案。加油!🎈
#include <iostream>
#include <string>
using namespace std;
int main()
{
string st;
int i, len;
getline(cin, st);
len = st.size();
for (i = 0; i < len; i++)
if (st[i] >= 'a' && st[i] <= 'z')
st[i] = st[i] - 'a' + 'A';
cout << st << endl;
return 0;
}
输入:Hello, my name is Lostmonkey.
st[i] >= 'a' && st[i] <= 'z':判断是否为小写字母。st[i] - 'a' + 'A':将小写字母转换为对应的大写字母。
#include <iostream>
using namespace std;
int main(){
int i, length1, length2;
string s1, s2;
s1 = "I have a dream.";
s2 = "I Have A Dream.";
length1 = s1.size();
length2 = s2.size();
for (i = 0; i < length1; i++)
if (s1[i] >= 'a' && s1[i] <= 'z')
s1[i] -= 'a' - 'A';
for (i = 0; i < length2; i++)
if (s2[i] >= 'a' && s2[i] <= 'z')
s2[i] -= 'a' - 'A';
if (s1 == s2)
cout << "=" << endl;
else if (s1 > s2)
cout << ">" << endl;
else
cout << "<" << endl;
return 0;
}
#include <stdio.h>
char st[100];
int main() {
scanf("%s", st);
for (int i = 0; st[i]; ++i) {
if ('A' <= st[i] && st[i] <= 'Z')
st[i] += 1;
}
printf("%s\n", st);
return 0;
}
输入:QuanGuoLianSai
设字符串 S="Olympic",S 的非空子串的数目是( )。
以下关于字符串的判定语句中正确的是()。
若串 S="copyright",其子串的个数是( )。
#include<iostream>
#include<string>
using namespace std;
int main(){
string map = "2223334445556667778889999";
string tel;
int i;
cin>>tel;
for(i=0;i<tel.length();i++)
if((tel[i]>='0') && (tel[i]<='9') )
cout<<tel[i];
else if( (tel[i]>='A') && (tel[i]<='Z'))
cout<<map[tel[i]-'A'];
cout<<endl;
return 0;
}
输入:CCF-NOIP-2011
map 字符串,ABC->2, DEF->3, ..., NO->6, PQRS->7, TUV->8, WXYZ->9。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
char m1, m2;
int i;
getline(cin, s);
m1 = ' ';
m2 = ' ';
for (i = 0; i < s.length(); i++)
if (s[i] > m1) {
m2 = m1;
m1 = s[i];
}
else if (s[i] > m2)
m2 = s[i];
cout << int(m1) << ' ' << int(m2) << endl;
return 0;
}
输入:Expo 2010 Shanghai China
提示:'a'=97, 'A'=65, '0'=48, 空格=32
#include<iostream>
using namespace std;
int main()
{
int t[256];
string s;
int i;
cin >> s;
for (i = 0; i < 256; i++)
t[i] = 0;
for (i = 0; i < s.length(); i++)
t[s[i]]++; // 统计次数
for (i = 0; i < s.length(); i++)
if (t[s[i]] == 1) // 找第一个次数为1的
{
cout << s[i] << endl;
return 0;
}
cout << "no" << endl;
return 0;
}
输入:xyzxyw
字符 AAABBBCCC 共有( )个不同的非空子串。
字符串 abcab 有( )个内容互不相同的子串。
循环按键序列:CapsLock, A, S, D, F。屏幕上输出的第 81 个字符是?(初始小写)
#include <iostream>
#include <string.h>
char change[26], str[5000];
using namespace std;
void CheckChangeRule() {
int i;
for (i = 0; i < 26; i++) {
if (【①】)
change[i] -= 'A' - 'a';
}
}
void ChangeString() {
int i;
for (i = 0; i < strlen(str); i++) {
if (【②】)
str[i] = change[str[i] - 'A'] - 'a' + 'A';
else
【③】;
}
}
int main() {
cin >> str >> change;
CheckChangeRule();
【④】;
cout << str << endl;
return 0;
}
change 统一转成小写。
#include <iostream>
using namespace std;
int delnum( char *s )
{
int i, j;
j = 0;
for ( i = 0; s[i] != '\0'; i++ )
if ( s[i] < '0' 【①】 s[i] > '9' )
{
s[j] = s[i];
【②】;
}
return(【③】);
}
// main 中: cout << 【④】;
||。j 就要后移,j++。j。下面的程序的功能是输入若干行字符串,每输入一行,就按逆序输出该行,最后键入 -1 终止程序。请将程序补充完整。
#include <iostream.h>
#include <string.h>
int maxline = 200, kz;
int reverse( char s[] )
{
int i, j, t;
// i从前往后,j从后往前
for ( i = 0, j = strlen( s ) - 1; i < j; 【①】, 【②】 )
{
t = s[i]; s[i] = s[j]; s[j] = t;
}
return(0);
}
int main()
{
char line[100];
cout << "continue? -1 for end." << endl;
cin >> kz;
while(【③】)
{
cin >> line;
【④】;
cout << line << endl;
cout << "continue ? -1 for end." << endl;
cin >> kz;
}
}
i++,②填 j--。kz != -1。reverse(line)。找出字符串循环位移中字典序最小的一个。
char get(int i) {
if (i < n) return s[i];
else return s[i - n];
}
// main 部分逻辑
// 遍历所有可能的起始点 i,找到字典序最小的串
// if (get(i + j) < get(ans + j)) ans = i;
输入:CBBADADA
#include "ctype.h"
#include "stdio.h"
void expand( char s1[], char s2[] )
{
int i, j, a, b, c;
j = 0;
for ( i = 0; (c = s1[i]) != '\0'; i++ )
if ( c == '-' )
{
a = s1[i - 1]; b = s1[i + 1];
if ( isalpha( a ) && isalpha( b ) || isdigit( a ) && isdigit( b ) )
{
j--; // 回退,覆盖掉减号前面的字符
do
s2[j++] = a++;
while ( tolower( a ) < tolower( s1[i + 1] ) );
}
else s2[j++] = c;
}else s2[j++] = c;
s2[j] = '\0';
}
// 假设 main 输入: wer2345d-h454-82qqq
输入:wer2345d-h454-82qqq
char-char 或 num-num 展开。d-h 展开为 defgh。4-8 展开为 45678。j--,它把减号前面已经输出的那个字符重写了一遍,作为展开的起点。
int f(string x, string y) {
// 动态规划求最长公共子序列(LCS)长度
// ... v[i][j] = max(...) ...
return v[m][n];
}
bool g(string x, string y) {
if (x.size() != y.size()) return false;
return f(x + x, y) == y.size();
}
// main: cin >> x >> y; cout << g(x,y);
判断:
单选:
v(m+1, n+1),访问 v[n][m] 可能会越界导致崩溃。