Error Notes Blue Bridge Cup (C++)

wrong ticket

Knowledge points: ungetc() function, getchar() function

Title description: Wrong bill

A secret-related unit has issued a certain bill, and it will all 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.

For example:
user input:
2
5 6 8 11 9 
10 12 9
, the program output:
7 9

Another example:
user input:
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
则程序输出:
105 120

资源约定:
峰值内存消耗 < 64M
CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型

该中方法的思路是:

所有用的票据编号都是1~100000,所以我们建立一个具有100001个元素的一维数组并初始化为0(表示这些数都没有出现),然后对输入的数据进行分析,如果输入了正整数i,则将a[i]++;

对所有的输入的数据操作完之后,能得到一个票据的最大值max和最小值min,本来编号在min和max之间应该是连续的(出现且仅出现了一次),也就是说a[min]~a[max]的值都是1,如果有a[m]=0,则说明m是断号(即没有出现过);

如果有a[n]=2,则说明n是重号(即出现了两次)。

知识点补充:

1.ungetc函数是将输出流中的废弃数据退入流中去。比如说

#include<stdio.h>
#include<stdlib.h>
void main()
{
int i;
int sum = 0;
char ch;
printf("Please input :\n");

while(scanf("%d",&i)==1)
{
sum += i;
while((ch=getchar())==' ');
if(ch == '\n')
{
break;
}
ungetc(ch, stdin);
}
printf("result: %d",sum);
printf("\n");
system("pause");
}

ungetc(ch, stdin);将你读到的字符回退到输入流中
比如上面的程序你先输入数字25,scanf读到i中,加到sum中,然后你再输入,调用ch=getchar(),发现ch不是' '也不是'\n',而是一个数,比如’7‘,显然这是你要继续加到sum中的数,要用scanf读到i中,但是你已经将这个'7'用getchar读出来了,只好用ungetc再把这个'7'给退回去
2.getchar函数:每次只能读一个字符
 
 
#include<iostream>  
using namespace std;  
int main()  
{  
    int a[100001]={0};//建立表示标号出现次数的数组,并初始化为0   
    int min = 100001;//最小值设为100001是为了让第一个与之比较的数可以取代其成为最小值   
    int max = 0;     //最大值设为0是为了让第一个与之比较的数可以取代其成为最大值    
    int row;    //输入的行数  
    int biaohao;//用于获取标号  
    char c;     //用于获取标号的首字符   
    cin>>row;  
    getchar();  //用于消去输入row时的回车,方式在for中被识别 //getchar()这个一定不能少,就是消去回车
  
    for(int i=0;i<row;)   
    {  
        c = getchar();  //获取输入的数据,具体用法:http://blog.csdn.net/qsyzb/article/details/19070949   
        if(c >= '0' && c<= '9')  
        {  
            ungetc(c,stdin);//将字符t送回到缓冲区中,保持数据的完整性   
            cin>>biaohao; //因为biaohao为int型,所以cin时遇到空格才停止   
            a[biaohao]++;   //改变对应标号出现的次数   
            if(biaohao < min)min = biaohao;//找到最小值   
            if(biaohao > max)max = biaohao;//找到最大值   
        }   
        else if(c == '\n')  //输入数据是回车   
        {  
            i++;   
        }  
    }//从min到max进行遍历,然后找到出现0次(断码),出现2次(重码)的数字    for( i=min;i<=max;i++)  
    {  
        if(a[i]==0)  
        {  
            cout<<i<<" ";             
        }  
        if(a[i]==2)  
        {  
            cout<<i<<endl;  
        }  
    }  
    return 0;  
}  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325773352&siteId=291194637