问题 BV: 找新朋友

题目描述

新年快到了,天勤准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新朋友,现在会长想知道究竟有几个新朋友?请你编程序帮会长计算出来。

输入

第一行是测试数据的组数CN(Case number,1<CN<10000),接着有CN行正整数N(1<n<32768),表示会员人数。

输出

对于每一个N,输出一行新朋友的人数,这样共有CN行输出。

样例输入 Copy

2
25608
24027

样例输出 Copy

7680
16016

代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
 
int main()
{
	int N,n;
	int M;
	cin>>M;
	while(M--)
	{
		scanf("%d",&N);
	    n=N;
		for(int i=2;i<=sqrt(N);i++)//欧拉公式;与互质的那一题很是相似;
			if(N%i==0)
		{
			n=n/i*(i-1);
			while(N%i==0)
				N/=i;
		}
		if(N>1)
			n=n/N*(N-1);
		printf("%d\n",n);
	}
	return 0;
}
### 学生成绩链表的实现 #### 描述 为了有效地管理学生成绩,可以采用链表这种数据结构来存储成绩信息。通过定义一个学生节点类或结构体,每个节点不保存学生的成绩还包含指向下一个节点的指针。 对于 C/C++ 和 Java 这两种不同的编程语言环境下的具体实现如下: #### 使用 C 实现学生成绩链表操作 ##### 结构体定义 ```c #include <stdio.h> #include <stdlib.h> // 定义单向链表结点 typedef struct Student { int id; float score; struct Student *next; } StudentNode; ``` ##### 创建新节点函 ```c StudentNode* create_node(int student_id, float grade) { StudentNode *new_student = (StudentNode *)malloc(sizeof(StudentNode)); new_student->id = student_id; new_student->score = grade; new_student->next = NULL; return new_student; } ``` ##### 尾部插入功能 ```c void insert_at_tail(StudentNode **head_ref, int student_id, float grade) { // 新建节点 StudentNode *new_student = create_node(student_id, grade); if (*head_ref == NULL) { *head_ref = new_student; return; } // 到尾部并连接新的节点 StudentNode *last = *head_ref; while (last->next != NULL){ last = last->next; } last->next = new_student; } ``` ##### 删除指定ID的学生记录 ```c void delete_by_id(StudentNode **head_ref, int key) { StudentNode *temp = *head_ref, *prev; // 如果头节点就是要删除的目标 if (temp != NULL && temp->id == key) { *head_ref = temp->next; free(temp); return; } // 查要删除的节点及其前驱节点 while (temp != NULL && temp->id != key) { prev = temp; temp = temp->next; } // 若未到目标则返回 if (temp == NULL) return; // 断开链接并释放内存 prev->next = temp->next; free(temp); } ``` ##### 遍历打印所有学生的信息 ```c void traverse_list(const StudentNode *current_head) { const StudentNode *traverse_ptr = current_head; while(traverse_ptr != NULL){ printf("ID:%d Score:%f\n", traverse_ptr->id , traverse_ptr->score ); traverse_ptr = traverse_ptr->next; } } ``` 以上实现了基本的创建、插入、删除以及遍历显示的功能[^1]。 #### 使用 Java 实现学生成绩链表操作 ##### 类定义 ```java public class StudentRecord { public static class Node { private final int id; private final double score; private Node next; public Node(int id, double score) { this.id = id; this.score = score; this.next = null; } @Override public String toString() { return "ID:" + id + ",Score:" + score; } } private Node head; public void addAtTail(int id, double score) { Node newNode = new Node(id, score); if(head==null){ head=newNode; return ; } Node tail=head; while(tail.next!=null){ tail=tail.next; } tail.next=newNode; } public boolean removeById(int targetId) { if (head == null || (head.id == targetId && head.next == null)) { head=null; return true; } Node previous = null; Node current = head; while(current!=null&&current.id!=targetId){ previous=current; current=current.next; } if(current==null)return false; if(previous==null){ head=current.next; }else{ previous.next=current.next; } return true; } public void printAllRecords(){ StringBuilder sb = new StringBuilder(); for(Node n = head;n!=null;n=n.next){ sb.append(n.toString()).append("\n"); } System.out.println(sb.toString()); } } ``` 上述代码展示了如何利用面向对象特性构建更易于维护和扩展的应用程序[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值