Blue Bridge Cup (2013 c / c ++ Group B title 7) wrong notes

Copyright: For reprint contact me, illegal copying peril https://blog.csdn.net/qq_41106517/article/details/88372468

Problem Description:


Title: Error bill

    A secret unit sent down some notes, and to be fully recovered at the end of the year.

    Each ticket has a unique ID number. All annual bill ID number is continuous, but the beginning of a digital ID is randomly selected.

    Because of staff negligence, an error occurred when entering the ID number, resulting in a broken ID number, the other a heavy ID number.

    Your task is programmed to find the ID ID number and weight of broken numbers.

    Suppose off number unlikely to occur in the minimum and maximum number.

The program first inputs required an integer N (N <100) represents the number of rows of data that follows.
Then reads the data of N lines.
Each row of unequal length, with a plurality of separated spaces (less than 100) positive integer (less than 100,000)
each represents an integer ID number.

Program output line in claim 1, comprising two integers Mn, separated by spaces.
Wherein, m represents the number of broken ID, n represents the ID number of weight

For example:
a user input:
2
. 5. 6. 8. 11. 9 
10. 9 12 is

The program output:
79


Another example:
a user input:
. 6
164 is 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 119 160,167,147,198,111

The program output:
105120
   

Resources for:
peak memory consumption <64M
the CPU consumption <1000ms


Please strictly follow the requirements of output, not superfluous to print something like: "Please enter ..." unwanted content.

All source code in a single file, through debugging, submit the copy source.

Note: main function needs to return 0
Note: Use only ANSI C / ANSI C ++ standard, do not call a special function depends on the build environment or operating system.
Note: all dependent functions must explicitly #include <xxx>, can not be provided by the project source file header files commonly omitted.

When you submit, pay attention to select the desired type of compiler.

 



#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int N,i,j,k=0;
	scanf("%d",&N);
	int sum=0,N1=N,ID1,ID2;
	int a[100][100];	
    int b[100]={0};
    int c[100000];
	while(N--){	
	    char s=' ';	
	    for (i=0;i<100&&s!='\n';i++)    //最大输入100个数,遇到回车就终止循环	
	    {		
	        scanf("%d",&a[k][i]);		
	        s=getchar();           //s用来接收是否是回车
	        b[k]++;
	    } 	
	    k++;
	}
	int d=0;	
	for(int m=0;m<N1;m++){
		for(int n=0;n<b[m];n++){
			c[d++]=a[m][n];
			sum++;
//			printf("%d ",a[m][n]);
		}
//		printf("\n");
	}
	sort(c,c+sum);              //排序 
//	for(int i=0;i<sum;i++){
//		printf("%d ",c[i]);
//	}
    for(int k=1;k<sum;k++){
    	if((c[k]-c[k-1])==2){
    		ID1=c[k-1]+1;
		}
		if(c[k] == c[k-1]){
			ID2=c[k];
		}
	}
	printf("%d %d",ID1,ID2);
	
	return 0;
} 

Official Solution:

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;
const int MaxN = 10000;
int line;
int data[MaxN];

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

int main(int argc, const 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++]);
        }
    }
//    cout << index << endl;
    sort(data, data + index);
    int a, b;
    for (int i = 1; i < index; ++i) {
        if (data[i] == data[i - 1] + 2)a = data[i] - 1;//printf("%d ", data[i] - 1);
        if (data[i] == data[i - 1]) b = data[i];//printf("%d", data[i]);
    }
    printf("%d %d", a, b);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/88372468