[Blue Bridge Cup] Exam Training 2013 C++A Group Question 7 Wrong Notes

 Wrong ticket

Description of the problem A
secret-related unit issued a certain kind of bills, and they had to withdraw all of them at the end of the year.

Each ticket has a unique ID number. The ID numbers of all bills throughout the year are consecutive, but the starting number of the ID is randomly selected.

Due to the negligence of the staff, an error occurred when entering the ID number, which caused one ID to be out of number and another to be duplicated.

Your task is to find out the ID of the broken number and the ID of the repeated number through programming.

Assume that a broken number cannot occur in the largest and smallest numbers.

Input format The
program is required to input an integer N (N<100) to indicate the number of data rows.

Then read in N rows of data.

The length of each line of data varies. It is a number (not more than 100) positive integers (not more than 100000) separated by spaces. Please note that there may be extra spaces in and at the end of the line. Your program needs to be able to handle these spaces.

Each integer represents an ID number.

Output format The
program is required to output one line, containing two integers mn, separated by spaces.

Among them, m represents broken ID, n represents repeated ID
sample input 1
2
5 6 8 11 9
10 12 9
sample output 1
7 9
sample input 2
6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119
Sample output 2
105 120

 

Problem analysis

The main difficulty of this question in C++ lies in the processing of multiple strings, such as inputting multiple lines, processing newline characters (getchar() must not be less), and dividing the output string (c++ does not Write the split function, you need to implement it yourself), convert the string to an integer number (s2i() template)

Store the input divided number in an array. The size of the array is allocated according to the meaning of the question, and sorted after storage, so that an ordered sequence of numbers can be obtained. The breakpoint phenomenon occurs when the difference is 2 and the difference is 0. repeated. Note that when looping the array, i starts from 1, i-(i-1) to avoid the array out of bounds

The point that is easy to overlook is that the title requires an output format ! One line, m, space, n, that is, the number of breakpoints, the number of spaces, and the number of repetitions. At this time, it is best to save the number obtained in the previous search process with variables!

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

int line;
const int maxN = 10000;
int data[maxN];

void s2i(string &str, int &num){
	stringstream ss;
	ss << str;
	ss >> num;
}

int main(int argc, char** argv) {
	scanf("%d", &line);
	getchar();		//去掉换行符,一定不能少 
	int index = 0;
	for(int i = 0; i < line; i++){
		string s;
		getline(cin, s); 
		istringstream iss(s);
		string tmp;
		
		while(getline(iss, tmp, ' ')){
			s2i(tmp, data[index]);
			index++;		
		}
	} 
//	cout << index ;
	
	//排序
	sort(data, data+index);
	int a, b; 
	//应该用变量记录下来断点和重复的数,因为并不知道断点和重复哪一个在前 
	//而题目要求先输出断点,后输出重复的数 
	for(int i = 1; i < index; i++){
		if(data[i] - data[i-1] == 2){	//断序的 
//			cout << data[i]-1 << endl;	//扣分 
			a = data[i]-1;
		}
		if(data[i] == data[i-1]){		//重复的 
//			cout << data[i] << endl;	//扣分 
			b = data[i];
		}
	}
	
	cout << a << " " << b;
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115142967