(Processing of Strings) Blue Bridge Cup 2013 C/C++ Group A Provincial Competition Zhenti 7 Error Notes

topic

A secret-related unit has issued some kind of bills, which are to be recovered at the end of the year.

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

Due to the negligence of the staff, an error occurred when entering the ID number, resulting in a broken ID for one ID and a duplicate ID for another ID.

Your task is to programmatically find out the ID of the break number and the ID of the double number.

It is assumed that break numbers cannot occur at the largest and smallest numbers.

The program is required to first input an integer N (N<100) to represent the number of data lines that follow.
  Then read in N lines of data.
  The length of each line of data varies, and is a number of (not more than 100) positive integers (not more than 100,000) separated by spaces.
  Each integer represents an ID number.

The program is required to output 1 line, containing two integers mn, separated by spaces.
  Among them, m represents the break ID, n represents the repeat ID

Example:
  User input:
  2
  5 6 8 11 9
  10 12 9

Then the program output:
  7 9

Another example:
  User Enter:
  6
  164 178 108 109 180 155 141 159 104 182 171 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 151 143 175 120 161 134 162 190
  149 138 142 146 1993 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

Then the program output:
  105 120

Resource convention:
  peak memory consumption < 64M
  CPU consumption < 1000ms

Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

All code is placed in the same source file, after debugging, copy and submit the source code.

Note: The main function needs to return 0
  Note: Only use the ANSI C/ANSI C++ standard, do not call special functions that depend on the compilation environment or operating system.
  Note: All dependent functions must be explicitly #included in the source file, and common header files cannot be omitted through project settings.

When submitting, take care to select the desired compiler type.

analyze

Interpret the meaning of the question, given an n, given n rows of data, each row of data does not necessarily consist of how many numbers, in the total data, there is a discontinuity in one place, and a number is repeated, find out these two What is the number.

At first glance, this question is relatively simple. After sorting it directly, it can be traversed in a loop. However, how do we input the data given by him? Since it does not tell us how much data is in each row, the difficulty lies in dealing with it. enter

code section

1. Initialize, enter

First an n, then an array of strings enter n lines by line

	int n;
	cin>>n;
	vector<string> str(n);

	getchar();

	for(int i=0;i<n;i++)
		getline(cin,str[i])
2. Process the input and convert the string to a number

The outer loop is n times, converting a line of data each time, setting the left and right endpoints respectively, the left endpoint is at the first character of the current number, and the right endpoint is at the space after the current number. Use the substr function to pass this string to the string Convert the function of digital processing, and then update the left and right endpoints. Note that there is no space after the last number. To make a special judgment, you only need to add the length of the current entire string as the limit.

	int cnt=0;			//记录数字的个数 
	for(int i=0;i<n;i++)
	{
    
     
		int l=0,r=0;	//左端点右端点
		while(r<str[i].size())		//把每行的各个数字转换 
		{
    
    
			while(str[i][r]!=' '&&r<str[i].size())
				r++;
			s_to_i(cnt,str[i].substr(l,r));
			
			++r;
			l=r;
			++cnt;
		}
	}
3. A function to convert a string to a number

Convert strings to numbers using stringstream

void s_to_i(int i,string str)
{
    
    
	stringstream ss;
	ss<<str;
	ss>>a[i];
}
4. Loop through the array to find the final answer

Here, it will be much easier to process after sorting the array. Repeated elements will be next to each other, so you only need to judge the numbers next to each other. If the two values ​​are equal, the answer is the answer. The missing data is that the difference between the two numbers before and after is 2, and then the front The number plus one is the answer, because the notes are normally consecutive

	sort(a,a+cnt);
	
	int ans1;		//少的id
	int ans2;		//重复id
	
	for(int i=0;i<cnt-1;i++)
	{
    
    
		if(a[i]==a[i+1])
			ans2=a[i];
		if(a[i+1]-a[i]==2)
			ans1=a[i]+1;
	}

full code

#include <bits/stdc++.h>
using namespace std;

int a[10000];		//存放所有数字 

void s_to_i(int i,string str)
{
    
    
	stringstream ss;
	ss<<str;
	ss>>a[i];
}

int main (void)
{
    
    
	int n;
	cin>>n;
	vector<string> str(n);

	getchar();

	for(int i=0;i<n;i++)
		getline(cin,str[i]);
	
	int cnt=0;			//记录数字的个数 
	for(int i=0;i<n;i++)
	{
    
     
		int l=0,r=0;	//左端点右端点
		while(r<str[i].size())		//把每行的各个数字转换 
		{
    
    
			while(str[i][r]!=' '&&r<str[i].size())
				r++;
			s_to_i(cnt,str[i].substr(l,r));
			
			++r;
			l=r;
			++cnt;
		}
	}
	
	sort(a,a+cnt);
	
	int ans1;		//少的id
	int ans2;		//重复id
	
	for(int i=0;i<cnt-1;i++)
	{
    
    
		if(a[i]==a[i+1])
			ans2=a[i];
		if(a[i+1]-a[i]==2)
			ans1=a[i]+1;
	} 
		
	cout<<ans1<<" "<<ans2;
	
	return 0;
	
} 

Summarize

The difficulty of this problem lies in the processing of input, the processing of strings, and the splitting into numbers. If
you are familiar with various functions of strings and the functions of string streams, this problem becomes much easier.

Guess you like

Origin blog.csdn.net/weixin_46035615/article/details/123821768