LintCode: Happy Number

本文提供了一种使用C++实现的快乐数判断算法。通过不断将数字的每一位平方和相加直至结果为1或是循环,则该数字为快乐数;反之则不是。此算法简洁高效,适用于竞赛及实际应用。

C++

复制代码
 1 class Solution {
 2 public:
 3     /**
 4      * @param n an integer
 5      * @return true if this is a happy number or false
 6      */
 7     bool isHappy(int n) {
 8         // Write your code here
 9         int a, b;
10         if (n <= 0) return false;
11         if (n < 10) n = n*n;
12         while (n > 9) {
13             a = 0;
14             while (n) {
15                 b = n%10;
16                 a += b*b;
17                 n /= 10;
18             }
19             n = a;
20         }
21         return n == 1;
22     }
23 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5105353.html,如需转载请自行联系原作者

请帮我写一份c++代码,解决一下问题,要求符合输入输出样本There are n people (excluding myself) in my 30th birthday party. They sing the traditional “happy birthday” song: Happy birthday to you! Happy birthday to you! Happy birthday to Rujia! Happy birthday to you!!! Since I love music, I want to hear something more interesting, not that everyone sings together. Ah yes, I want one person to sing one word! For example, there are three people: Mom, Dad, Girlfriend, I’d like them to sing like this: Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you Very nice, right? What if there are more than 16 people? That’s easy: repeat the song until everyone has sung at least once :) Please, don’t stop in the middle of the song.Input There is only one test case. The first line contains a single integer n (1 ≤ n ≤ 100). Then each of the next n lines contains a capitalized name (i.e. one upper-case letter followed by zero or more lowercase letters). Each name contains at most 100 characters and do not have whitespace characters inside. Output Output the song, formatted as above. Sample Input 3 Mom Dad Girlfriend Sample Output Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you
12-06
以下是实现生日派对上每人依次唱“生日快乐”歌一个单词的C++代码: ```cpp #include <iostream> #include <vector> #include <string> int main() { int n; std::cin >> n; std::cin.ignore(); // 忽略换行符 std::vector<std::string> names(n); for (int i = 0; i < n; ++i) { std::getline(std::cin, names[i]); } std::vector<std::string> song = { "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "Rujia", "Happy", "birthday", "to", "you" }; int personIndex = 0; int songIndex = 0; while (personIndex < n || songIndex < song.size()) { if (personIndex >= n) { personIndex = 0; } if (songIndex >= song.size()) { songIndex = 0; } std::cout << names[personIndex] << ": " << song[songIndex] << std::endl; personIndex++; songIndex++; } return 0; } ``` ### 代码说明 1. **输入处理**:首先读取人数`n`,然后忽略换行符,接着读取`n`个人的名字并存储在`names`向量中。 2. **歌词存储**:将“生日快乐”歌的歌词按单词存储在`song`向量中。 3. **演唱循环**:使用两个索引`personIndex`和`songIndex`分别遍历人名和歌词。当一个遍历完后,将其索引重置为0,继续循环,直到所有人都至少唱了一次且不中断歌曲。 ### 输入输出示例 **输入**: ``` 3 Mom Dad Girlfriend ``` **输出**: ``` Mom: Happy Dad: birthday Girlfriend: to Mom: you Dad: Happy Girlfriend: birthday Mom: to Dad: you Girlfriend: Happy Mom: birthday Dad: to Girlfriend: Rujia Mom: Happy Dad: birthday Girlfriend: to Mom: you ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值