P - Reduced ID Numbers

T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 10 6-1. T. Chur finds this range of SINs too large for identification within her groups. For each group, she wants to find the smallest positive integer m, such that within the group all SINs reduced modulo m are unique.
Input
On the first line of the input is a single positive integer N, telling the number of test cases (groups) to follow. Each case starts with one line containing the integer G (1 ≤ G ≤ 300): the number of students in the group. The following G lines each contain one SIN. The SINs within a group are distinct, though not necessarily sorted.
Output
For each test case, output one line containing the smallest modulus m, such that all SINs reduced modulo m are distinct.
Sample Input
2
1
124866
3
124866
111111
987651
Sample Output
1
8
题意:输入n,表示测试的组数,g表示学生个数,输入g个数表示学号。输出各个学号模i后,余数不相同的最小i.
我直接用map标记,结果超时,后来百度了下,map会慢,只能用数组标记,这就需要用到memset函数初始化
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[301]; 
int b[1000001]={0};
int main()
{
 int n,g;
 int temp;
 int i,j;
 cin>>n;
 while(n--)
 {
  cin>>g;
 
  for(i=0;i<g;i++)
  {
   cin>>a[i];
  }
  for(i=g;i<1000001;i++)
  {
   int flag=1;
   memset(b,0,sizeof(int)*(i+1));
   for(j=0;j<g;j++)
   {
    temp=a[j]%i;
    if(b[temp]!=0)
    {
     flag=0;
     break;
    }
    else
    {
     b[temp]=1;
    }
   }
   if(flag==1)
   { 
    cout<<i<<endl;
    break;
   }
  }
 } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据 Evaluate Expression- reduced 算法,可以先将算数表达式转化为后缀表达式,然后再根据后缀表达式求值。将3*(7-2)转化为后缀表达式的过程如下: 1. 初始化一个空栈 S,创建一个空的后缀表达式列表 L。 2. 从左到右扫描算数表达式: a. 如果当前扫描到的是一个数字,就将它加入列表 L。 b. 如果当前扫描到的是一个运算符,就进行如下操作: i. 如果 S 为空,或者 S 栈顶的运算符为左括号,就将该运算符加入栈 S。 ii. 如果该运算符优先级比栈顶运算符的优先级高,就将该运算符加入栈 S。 iii.如果该运算符优先级比栈顶运算符的优先级低或者相等,就将栈顶运算符弹出并加入列表 L,然后重复步骤 b 直到满足条件 ii。 c. 如果当前扫描到的是一个左括号,就将它加入栈 S。 d. 如果当前扫描到的是一个右括号,就进行如下操作: i. 将栈顶运算符弹出并加入列表 L,直到遇到左括号。 ii. 将左括号弹出,但不加入列表 L。 3. 如果算数表达式已经扫描完毕,就将栈 S 中的所有运算符依次弹出并加入列表 L。 4. 列表 L 中的元素顺序即为转换后的后缀表达式。 将3*(7-2)转化为后缀表达式的结果为:3 7 2 - *。 接下来,可以利用栈来求值后缀表达式。具体的求值过程如下: 1. 初始化一个空栈 S。 2. 从左到右扫描后缀表达式: a. 如果当前扫描到的是一个数字,就将它压入栈 S。 b. 如果当前扫描到的是一个运算符,就从栈 S 中弹出两个数字,进行相应运算,并将运算结果压入栈 S。 3. 后缀表达式扫描完毕之后,栈 S 中仅有一个数字,即为表达式的值。 将后缀表达式3 7 2 - *求值的过程如下: 1. 初始化一个空栈 S。 2. 从左到右扫描后缀表达式: a. 遇到数字 3,将其压入栈 S。 b. 遇到数字 7,将其压入栈 S。 c. 遇到数字 2,将其压入栈 S。 d. 遇到运算符 -,从栈 S 中弹出两个数字,分别为 2 和 7,进行减法运算 7-2=5,将运算结果 5 压入栈 S。 e. 遇到运算符 *,从栈 S 中弹出两个数字,分别为 5 和 3,进行乘法运算 3*5=15,将运算结果 15 压入栈 S。 3. 后缀表达式扫描完毕之后,栈 S 中仅有一个数字 15,即为表达式的值。 因此,算数表达式3*(7-2)的值为15。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值