浙江大学计算机与软件学院2019年保研上机模拟练习 --- 凌宸1642

浙江大学计算机与软件学院2019年保研上机模拟练习 — 凌宸1642

7-1 Happy Numbers (20 分) — 简单数学

A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers and the number of iterations is called the degree of happiness, while those that do not end in 1 are unhappy numbers (or sad numbers). (Quoted from Wikipedia)

For example, 19 is happy since we obtain 82 after the first iteration, 68 after the second iteration, 100 after the third iteration, and finally 1. Hence the degree of happiness of 19 is 4.

On the other hand, 29 is sad since we obtain 85, 89, 145, 42, 20, 4, 16, 37, 58, and back to 89, then fall into an endless loop. In this case, 89 is the first loop number for 29.

Now your job is to tell if any given number is happy or not.

译:一个快乐的数字由以下过程定义:从任何正整数开始,用其以十为底的数字的平方和替换该数字,并重复该过程,直到该数字等于 1(它将停留在哪个位置),或者它在一个不包含 1 的循环中无限循环。这个过程以 1 结束的那些数字是快乐的数字,迭代的次数称为快乐度,而那些不以 1 结尾的则是不快乐的数字(或悲伤的数字)。 (引自维基百科)

例如,19 是快乐的,因为我们在第一次迭代后获得 82,第二次迭代后获得 68,第三次迭代后获得 100,最后为 1。因此 19 的快乐度为 4。 另一方面,29 是悲伤的,因为我们得到 85、89、145、42、20、4、16、37、58,然后回到 89,然后陷入无限循环。 在这种情况下,89 是 29 的第一个循环编号。

现在你的工作是判断任何给定的数字是否快乐

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N lines follow, each contains a positive integer (no more than104) to be tested.

译:每个输入文件包含一个测试用例,对于每种情况,第一行给定一个正整数 N (≤100) , 接下来 N 行每行包含一个用于测试的不超过 104 的整数。

Output Specification:

For each given number, output in a line its degree of happiness if it is happy, or the first loop number if it is sad.

译:对于每个给定的数字,如果他是一个开心数的话,在一行中打印它的幸福度,否则,打印第一个开始循环的数。

Sample Input:

3
19
29
1

Sample Output:

4
89
0

The Code:

#include<bits/stdc++.h>
using namespace std ;
int n , ans , t ;
int getSum(int x){
    // 得到 x 的数位和
	int res = 0 , t ;
	while(x){
   
		t = x % 10 ;
		res += t * t ;
		x /= 10 ;
	}
	return res ;
}
int main(){
   
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
   
		cin >> t ;
		ans = 0 ;
		if(t == 1) {
    // 对输入进行特判是否为 1
			cout << 0 << endl ;
			continue ;
		}
		unordered_map<int , bool> mp ; // 用 unordered_map ,标记出该数是否出现
		bool status = false ; // 循环的标识
		do{
   
			mp[t] = true ;
			t = getSum(t) ;
			ans ++ ;
			if(t == 1) break ; // 满足题意,直接退出循环
			if(mp[t]) status = true ; // t 之前出现过,所以此时t是第一个开始循环的数,修改循环标记
		}while(!status) ;
		if(t == 1) cout << ans << endl ; // 输出开心度
		else cout << t << endl ; // 输出第一个进入循环的数
	}
	return 0 ;
}

7-2 Zigzag Sequence (25 分) — 简单模拟

This time your job is to output a sequence of N positive integers in a zigzag format with width M in non-decreasing order. A zigzag format is to fill in the first row with M numbers from left to right, then the second row from right to left, and so on and so forth. For example, a zigzag format with width 5 for numbers 1 to 13 is the following:

1 2 3 4 5
10 9 8 7 6
11 12 13

译:这次你的工作是输出一个由 N 个正整数组成的锯齿形格式,宽度为 M 的非递减顺序。 锯齿形格式是第一行从左到右填M个数字,第二行从右到左,依此类推。 例如,数字 1 到 13 的宽度为 5 的锯齿形格式如下:

1 2 3 4 5
10 9 8 7 6
11 12 13

Input Specification:

Each input file contains one test case. For each case, the first line gives 2 positive integers N and M. Then the next line contains N positive integers as the original sequence. All the numbers are no more than 104. The numbers in a line are separated by spaces.

译ÿ

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值