又是一道错题:
1. 班级活动https://www.lanqiao.cn/problems/17153/learning/?page=1&first_category_id=1&sort=difficulty&asc=1&second_category_id=3
问题描述
小明的老师准备组织一次班级活动。班上一共有 n 名 (n 为偶数) 同学,老师想把所有的同学进行分组,每两名同学一组。为了公平,老师给每名同学随机分配了一个 n 以内的正整数作为 id,第 i 名同学的 id 为 ai。
老师希望通过更改若干名同学的 id 使得对于任意一名同学 i,有且仅有另一名同学 j 的 id 与其相同 (ai=aj)。请问老师最少需要更改多少名同学的 id?
输入格式
输入共 2 行。
第一行为一个正整数 n。
第二行为 n 个由空格隔开的整数 a1,a2,...,an。
输出格式
输出共 1 行,一个整数。
评测用例规模与约定
对于 20%的数据,保证 n≤10^3。
对于 100%的数据,保证 n≤10^5。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a;
scanf("%d",&a);
int *b=(int*)calloc(100005,sizeof(int));
for(int i=0;i<a;i++){
int temp;
scanf("%d",&temp);
b[temp]++;
}
int count=0;
for(int i=0;i<100005;i++){
if(b[i]>2)count+=(b[i]-2);
if (b[i]==1)count++;
}
printf("%d\n",count/2);
free(b);
return 0;
}
对上次的题再次的错解28/54:
#define max(a,b) (((a)>(b))?(a):(b))
long long mostPoints(int questions[][2], int questionsSize, int* questionsColSize) {
long long record[questionsSize+1];
for(int i=0;i<questionsSize+1;i++) {
record[i] = 0;
}
for(int i=0;i<questionsSize;i++) {
const int temp=i+1+questions[i][1],prior=record[i];
record[i]=record[i]+questions[i][0];
if (temp<questionsSize) {
record[temp]=max(record[temp],record[i]);
}else {
record[questionsSize]=max(prior+questions[i][0],record[questionsSize]);
}
}
return max(record[questionsSize-1],record[questionsSize]);
}
网络编程(作业):
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class d1 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 10000);
System.out.println("用户3");
new Thread(()->{
try(InputStreamReader isr = new InputStreamReader(socket.getInputStream())){
char []data = new char[1024];
int len;
while((len=isr.read(data))!=-1){
System.out.print("【群消息】"+new String(data,0,len));
}
}catch(IOException e){
System.out.println("连接已断开!");
}
}).start();
try(OutputStream os = socket.getOutputStream()){
Scanner sc = new Scanner(System.in);
while(true){
String s = sc.nextLine();
if(s.equals("exit"))break;
os.write((s+"\n").getBytes());
}
}
socket.close();
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
public class d2 {
static CopyOnWriteArrayList<Socket> list=new CopyOnWriteArrayList<>();
static AtomicInteger count=new AtomicInteger(0);
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000);
System.out.println("服务器成功启动!");
while (true) {
Socket socket = serverSocket.accept();
list.add(socket);
new Thread(new d3(socket,count.incrementAndGet())).start();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class d3 implements Runnable {
Socket socket;
int num;
public d3(Socket socket,int num) {
this.socket = socket;
this.num = num;
}
@Override
public void run() {
InputStreamReader in;
try {
in = new InputStreamReader(socket.getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
int temp;
StringBuilder message = new StringBuilder();
System.out.println("用户"+num+"连接成功!当前在线人数为:"+d2.list.size());
while (true) {
try {
if ((temp = in.read()) == -1) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
if((char)temp!='\n')message.append((char)temp);
else {
broadcast(message.toString());
message.setLength(0);
}
}
d2.list.remove(socket);
String temp1="用户"+num+"断开连接!剩余在线:"+d2.list.size();
System.out.println(temp1);
broadcast(temp1);
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void broadcast(String message){
for(Socket list :d2.list){
try{
if (list != socket && !list.isClosed()) {
list.getOutputStream().write(("用户"+num+":"+message + "\n").getBytes());
}
}catch (Exception e){
d2.list.remove(list);
}
}
}
}