The length of the last word of the HJ1 string
The length of the last word of the HJ1 string
describe
Calculate the length of the last word of the string, the words are separated by spaces, and the length of the string is less than 5000. (Note: the end of the string does not end with a space)
Enter description:
Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.
Output description:
Output an integer representing the length of the last word of the input string.
Example 1
输入:hello nowcoder
输出:8
说明:最后一个单词为nowcoder,长度为8
code
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
getline(cin, str);
int loc = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == ' ') {
loc = i + 1;
}
}
cout << str.size() - loc << endl;
return 0;
}
HJ2 count the number of occurrences of a character
describe
Write a program that takes a string of letters, numbers, and spaces, and a character, and prints the number of occurrences of that character in the input string. (case insensitive)
Data range: 1 ≤ n ≤ 1000 1\le n\le 10001≤n≤1000
Enter description:
Enter a string of letters and numbers and spaces on the first line, and a character on the second line.
Output description:
Output the number of characters in the input string. (case insensitive)
code
Note: The string in the title contains numbers, and ASCII
it is not feasible to judge the uppercase and lowercase letters directly with the code difference of 32, because there is a code difference of 32 between the number character and the letter ASCII
.
For example: the code value of character 0 ASCII
is 48, P
the ASCII
code value of character 80
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
char ch;
int count = 0;
getline(cin, str);
cin >> ch;
for (int i = 0; i < str.size(); i++) {
if (str[i] == toupper(ch) || str[i] == tolower(ch)) {
count++;
}
}
cout << count << endl;
return 0;
}
HJ3 obvious random number
describe
Obviously N
a random integer between 1 and 500 is generated. Please delete the repeated numbers, that is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the sorted order.
Data range: 1 ≤ n ≤ 1000 1 \le n \le 10001≤n≤1000 , the size of the entered number satisfies1 ≤ val ≤ 500 1 \le val \le 5001≤val≤500
Enter description:
The first line first enters the number of random integers N
. The N
following lines enter an integer per line, representing a random number that is clearly generated. The specific format can refer to the "Example" below.
Output description:
Output multiple lines, indicating the result of processing the input data
code
#include <iostream>
using namespace std;
int main() {
int N, n;
while (cin >> N) {
int a[1001] = {
0 };
while (N--) {
cin >> n;
a[n] = 1;
}
for (int i = 0; i < 1001; i++)
if (a[i])
cout << i << endl;
}
return 0;
}
HJ4 string delimiter
describe
-
Input a string, please split each input string by length 8 and output;
-
For strings whose length is not an integer multiple of 8, please add 0 at the back, and empty strings will not be processed.
Enter description:
Continuous input strings (each string length is less than or equal to 100)
Output description:
Output all new strings of length 8 after splitting in sequence
code
Note: substr
Usage that needs to be understood
#include <iostream>
using namespace std;
int main(){
string str;
while(getline(cin,str)){
while(str.size() > 8){
cout << str.substr(0, 8) << endl;
str = str.substr(8);
}
cout << str.append(8 - str.size(),'0') << endl; //不够8位的补0
}
}
HJ5 base conversion
describe
Write a program that accepts a hexadecimal number and outputs the decimal representation of that number.
Data range: Guaranteed result in 1 ≤ n ≤ 2 31 − 1 1 \le n \le 2^{31}-11≤n≤231−1
Enter Description:
Enter a hexadecimal numeric string.
Output description:
Output a decimal string of this value. Different groups of test cases are \n
separated by .
code
#include <iostream>
using namespace std;
int main() {
int a;
while (cin >> hex >> a)
cout << a << endl;
return 0;
}
HJ6 prime factor
Description
Function: Input a positive integer and output all its prime factors in ascending order (repeated ones are also listed) (for example, the prime factor of 180 is 2 2 3 3 5 )
Data range: 1 ≤ n ≤ 2 × 1 0 9 + 14 1 \le n \le 2 \times{10^{9} + 14}1≤n≤2×1 09+14
Enter description:
enter an integer
Output description:
Print the factors of all its prime numbers in ascending order, separated by spaces.
Example:
输入:180
输出:2 2 3 3 5
code
Ideas: 1. First from 2 to n 2 to \sqrt{n}2 tondivide nnn , record the divisor, and then use the quotient to continue the above operation until the quotient is 1.
2. If the division fails, add one to the divisor. If it keeps adding one, the divisor is greater thann \sqrt{n}n, then the prime factor of x is only itself.
#include<iostream>
#include <math.h>
using namespace std;
int main(){
int n;
while (cin >> n) {
for (int i = 2; i <= sqrt(n); i++) {
while (n%i == 0) {
cout << i << ' ';
n /= i;
}
}
if (n > 1) {
cout << n << endl;
}
}
return 0;
}
HJ7 Approximate value
describe
Write a program that accepts a positive floating-point number and outputs an approximate integer value of that number. If the value after the decimal point is greater than or equal to 0.5, round up; if it is less than 0.5, round down.
Data range: ensure that the input number is within the range of 32-bit floating-point numbers
Enter description:
Enter a positive floating point number
Output description:
output the approximate integer value of this number
Example 1:
输入:5.5
输出:6
说明:0.5>=0.5,所以5.5需要向上取整为6
Example 2:
输入:2.499
输出:2
说明:0.499<0.5,2.499向下取整为2
code
#include <iostream>
using namespace std;
int main(){
double n;
while (cin >> n) {
cout << int(n+0.5) << endl;
}
return 0;
}
HJ8 merge table records
describe
Data table records contain table indexes index
and values value
( int
positive integers in the range). Please merge records with the same table index, that is, sum the values of the same index, and output the index
values in ascending order.
hint:
- 0 < = i n d e x < = 11111111 0 <= index <= 11111111 0<=index<=11111111
- 1 < = v a l u e < = 100000 1 <= value <= 100000 1<=value<=100000
Enter description:
First enter the number of key-value pairs nnn( 1 < = n < = 500 1 <= n <= 500 1<=n<=500 )
Enter the paired sum values n
on each line on the next line , separated by spacesindex
value
Output description:
Output merged key-value pairs (multiple lines)
code
In the question, the results are required to be output in ascending order, so I think of the map
container
#include<iostream>
#include<map>
using namespace std;
int main(){
map<int, int> result;
int n;
int a, b;
while (cin >> n) {
// 将数据读入map容器
while (n--) {
cin >> a >> b;
if (result.find(a) == result.end()) {
result.insert(pair<int, int>(a, b));
}
else {
result[a] += b;
}
}
// 输出
for (map<int, int>::iterator it = result.begin(); it != result.end(); it++) {
cout << it->first << " " << it->second << endl;
}
}
return 0;
}
HJ9 extracts non-repeating integers
HJ9 extracts non-repeating integers
describe
Input an integer of int
type and return a new integer with no repeating digits in right-to-left reading order.
Make sure that the last digit of the input integer is not 0.
Data range: 1 ≤ n ≤ 1 0 8 1 \le n \le 10^{8}1≤n≤1 08
Enter description:
Enter an int
integer of type
Output description:
Returns a new integer with no duplicate digits, in right-to-left reading order
Example:
输入:9876673
输出:37689
code
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main(){
unordered_set<int> result;
int n;
while (cin >> n) {
//分解整数
while (n >= 10) {
if (result.find(n%10) == result.end()) {
//在哈希表中未找到
result.insert(n%10); //取余,并添加到哈希表
cout << n % 10; //打印
n /= 10;
}
else {
n /= 10;
}
}
// 当n为个位数时,若在哈希表中未找到,则输出
if (result.find(n) == result.end()) {
cout << n;
}
else {
cout << endl;
}
}
return 0;
}
HJ10 character count statistics
HJ10 character count statistics
describe
Write a function that counts the number of distinct characters contained in a string. Characters are in the ASCII
code range ( 0~127 , including 0 and 127 ), and the newline indicates the end character, which is not included in the character. Those not within the scope will not be counted. Multiple identical characters are counted only once
For example, for the abaca
string , there are three different characters, , , so output a
3 b
.c
Data range: 1 ≤ n ≤ 500 1 \le n \le 5001≤n≤500
Enter description:
Enter a single line of string with no spaces.
Output description:
Output the number of characters in the range (0~127, including 0 and 127) in the input string.
Example 1
输入:abc
输出:3
Example 2
输入:aaa
输出:1
code
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main(){
set<char> st;
string str;
while (cin >> str) {
for (int i = 0; i < str.size(); i++) {
st.insert(str[i]); //往set容器中插入元素
}
cout << st.size() << endl;
}
return 0;
}
HJ11 Number Reversed
describe
Input an integer and output the integer in reverse order as a string
The program does not consider the case of negative numbers. If the number contains 0, the reversed form also contains 0. If the input is 100, the output is 001
Data range: 0 ≤ n ≤ 2 30 − 1 0 \le n \le 2^{30}-10≤n≤230−1
Enter description:
enter an int
integer
Output description:
output the integer in reverse order as a string
Example 1
输入:1516000
输出:0006151
Example 2
输入:0
输出:0
code
The idea: master the to_string
method, and then reverse it
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int n;
string str;
while (cin >> n) {
str = to_string(n);
reverse(str.begin(), str.end());
cout << str;
}
return 0;
}
HJ12 String Reversal
describe
Takes a string containing only lowercase letters, and outputs the reversed string of that string. (string length does not exceed 1000)
Enter description:
Enter a line, a string containing only lowercase letters.
Output description:
Output the reversed string of this string.
Example 1
输入:abcd
输出:dcba
code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string str;
while (cin >> str) {
reverse(str.begin(), str.end());
cout << str;
}
}
HJ13 Sentence Reverse Order
describe
Arrange an English sentence in reverse order by word. For example, "I am a boy", after the reverse order is "boy a am I"
All words are separated by a space, and the sentence contains no other characters except English letters
Data range: The length of the input string satisfies 1 ≤ n ≤ 1000 1 \le n \le 10001≤n≤1000
Note that this question has multiple sets of inputs
Enter description:
Enter an English sentence, with each word separated by a space. Make sure the input contains only spaces and letters.
Output description:
get sentences in reverse order
Example 1
输入:I am a boy
输出:boy a am I
Example 2
输入:nowcoder
输出:nowcoder
code
Ideas: 1. Reverse the entire sentence first; 2. Reverse the local words. This question is similar to 151. Flip the words in a string .
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void reverse(string& s, int start, int end) {
//翻转,区间写法:左闭又闭 []
for (int i = start, j = end; i < j; i++, j--) {
swap(s[i], s[j]);
}
}
int main() {
string str;
int left = 0;
int right = 0;
while (getline(cin, str)) {
reverse(str, 0, str.size() - 1); //整个反转
//局部单词反转
for (int i = 0; i <= str.size(); i++) {
if (i == str.size() || str[i] == ' ') {
//空格或字符串末尾就得反转
right = i - 1;
reverse(str, left, right);
left = i + 1;
}
}
cout << str;
}
return 0;
}
HJ14 string sorting
Description
Given nnn strings, pleasecorrect nnThe n strings are arranged in lexicographical order.
Data range: 1 ≤ n ≤ 1000 1 \le n \le 10001≤n≤1000 , the string length satisfies1 ≤ len ≤ 100 1 \le len \le 1001≤l e n≤100
Enter description:
The first line of input is a positive integer n ( 1 ≤ n ≤ 1000 1≤n≤10001≤n≤1000 ), the following n lines are n strings (string length≤ 100 ≤ 100≤100 ), the string contains only uppercase and lowercase letters.
Output description:
Data output n
line, the output result is a string arranged in lexicographical order.
code
Idea 1: Use sort
Algorithmic Quicksort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<string> vc;
string str;
int n;
while (cin >> n) {
while (n--) {
//将n个字符串读入数组中
cin >> str;
vc.push_back(str);
}
sort(vc.begin(), vc.end());
for (int i = 0; i < vc.size(); i++) {
cout << vc[i] << endl;
}
}
return 0;
}
Idea 2: multiset
The container has its own sorting
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main(){
multiset<string> st;
string str;
int n;
while (cin >> n) {
while (n--) {
//将字符串输入set容器
cin >> str;
st.insert(str);
}
for (auto it = st.begin(); it != st.end(); it++) {
cout << (*it) << endl;
}
}
return 0;
}
HJ15 Find the number of 1's when int-type positive integers are stored in memory
HJ15 Find the number of 1's when int-type positive integers are stored in memory
describe
Input a positive integer of int
type and calculate int
the number of 1's when the data of type is stored in memory.
Data Range: Guaranteed to be within the 32-bit integer number range
Enter description:
Enter an integer (type int)
Output description:
After converting this number to binary, output the number of 1s
Example 1
输入:5
输出:2
Example 2
输入:0
输出:0
code
#include <iostream>
using namespace std;
int main()
{
int i, count = 0;
cin >> i;
while (i > 0) {
if (i & 1) count++;
i >>= 1;
}
cout << count;
return 0;
}
HJ17 coordinate movement
describe
Develop a coordinate calculation tool, A means move to the left, D means move to the right, W means move up, and S means move down. Move from the (0,0) point, read some coordinates from the input string, and output the final input result to the output file.
enter:
The legal coordinate is A (or D or W or S) + number (within two digits)
The coordinates are separated by ;.
Illegal coordinate points need to be discarded. Such as AA10; A1A; %, YAD; ?
Here is a simple example such as:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
Process:
starting point (0,0)
-
A10 = (-10.0)
-
S20 = (-10,-20)
-
W10 = (-10,-10)
-
D30 = (20,-10)
-
x = invalid
-
A1A = invalid
-
B10A11 = Invalid
-
a null does not affect
-
A10 = (10,-10)
result(10, -10)
Data range: The length of each input string satisfies 1 ≤ n ≤ 10000 1\le n \le 100001≤n≤10000 , the coordinates are guaranteed to satisfy− 2 31 ≤ x , y ≤ 2 31 − 1 -2^{31} \le x,y \le 2^{31}-1− 231≤x,Y≤231−1 , and the numeric part contains only positive numbers
Enter description:
one line string
Output description:
final coordinates, separated by commas
Example 1
输入:A10;S20;W10;D30;X;A1A;B10A11;;A10;
输出:10,-10
Example 2
输入:ABC;AKL;DA1;
输出:0,0
code
Idea: first split the string by semicolon, and then select the target string for calculation,Note the conversion of characters to numbers
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;
int main() {
string s;
string str; //每个字符串
getline(cin, s); //总字符串
vector<string> vc; //存放分割后的字符串
int x = 0, y = 0;
for (int i = 0; i <= s.size(); i++) {
if (s[i] == ';' || i == s.size()) {
//找到;或者字符串末尾,捕获前面一个字符串
vc.push_back(str);
str.clear();
}
else {
str += s[i];
}
}
// 遍历搜寻目标字符串
for (int j = 0; j < vc.size(); j++) {
int num = 0; //每次初始化为0
if (vc[j].size() == 3 && vc[j][1] >= '0' && vc[j][1] <= '9'
&& vc[j][2] >= '0' && vc[j][2] <= '9') {
num = (vc[j][1] - '0') * 10 + (vc[j][2] - '0');
}
if (vc[j].size() == 2 && vc[j][1] >= '0' && vc[j][1] <= '9') {
num = (vc[j][1] - '0');
}
switch (vc[j][0]) {
case 'A': x -= num;
break;
case 'D': x += num;
break;
case 'S': y -= num;
break;
case 'W': y += num;
break;
default:
break;
}
}
cout << x << "," << y << endl;
return 0;
}