1001
卡拉兹(Callatz)猜想: 对任何一个正整数
n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一步得到 n=1。卡拉兹在1950 年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……
我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过 1000 的正整数 n,简单地数一下,需要多少步(砍几下)才能得到 n=1?
输入格式: 每个测试输入包含 1 个测试用例,即给出正整数 n 的值。 输出格式: 输出从 n 计算到 1 需要的步数。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
int count=0;
while(n!=1){
if(n%2==0){
n/=2;
}
else{
n=(3*n+1)/2;
}
count++;
}
cout<<count<<endl;
}
1002 写出这个数 (20分)
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10^100
输出格式:
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。
ATTENTION
1. 读取字符串中的字符数字变为整数是要注意ascii码变换(-48或-‘0’)
2. 字符串长度s.length()、幂函数pow(x,y)
3. 整数位数为(log10()+1)向下取整,但小于其的最大十的整数次幂是10的(整数位数-1)次幂(如1008为四位,小于其最大为10的三次幂)
4. 部分正确的几个原因:没有注意十以下的情况/没有注意如108、1008这种中间会出现零的情况
5. 最后输出无回车
我的基本思路是先求出各位数字之和,当和小于十直接输出,当其大于10时依次输出每位数字
个人解法
#include <bits/stdc++.h>
using namespace std;
int shuchu(int p){
if(p==0) cout<<"ling";
else if(p==1) cout<<"yi";
else if(p==2) cout<<"er";
else if(p==3) cout<<"san";
else if(p==4) cout<<"si";
else if(p==5) cout<<"wu";
else if(p==6) cout<<"liu";
else if(p==7) cout<<"qi";
else if(p==8) cout<<"ba";
else if(p==9) cout<<"jiu";
return 0;
}
int main(){
string s;
cin>>s;
// cout<<s<<endl;
int whole=0;
for(int i=0;i<(int)s.length();i++){
// cout<<s.length()<<endl;
whole+=(int)s[i]-48;
// cout<<whole<<endl;
// cout<<s[i]<<endl;
}
if(whole<10){
shuchu(whole);
// cout<<endl;
}else{
// cout<<whole<<endl;
int q;
int wei=(int)(log10(whole));
// cout<<wei<<endl;
for(int j=wei;j>0;j--){
q=whole/(pow(10,j));
//cout<<q<<endl;
shuchu(q);
cout<<" ";
whole-=q*pow(10,j);
if(whole<10){
if(j==1){
shuchu(whole);
// cout<<endl;
break;
}else{
for(int k=j;k>1;k--){
cout<<"ling"<<" ";
}
shuchu(whole);
break;
}
}
// cout<<whole<<endl;
}
}
return 0;
}
他人解法1(求和后使用栈,将个位、十位、百位依次推入栈中,然后从顶向下依次输出后弹栈)
string s[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
stack<int> x;
do{
x.push(whole % 10);
whole = whole / 10;
} while (whole != 0);
cout << s[x.top()];
x.pop();
while (x.size() != 0)
{
cout << " " << s[x.top()];
x.pop();
}
他人解法2(不使用栈,直接将和转化为字符串,然后从前至后依次输出)
string str[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string num = to_string(whole);//将和直接转化成字符串
for (int i = 0; i < num.length(); i++) {
if (i != 0) cout << " ";
cout << str[num[i] - '0'];
}
他人解法3(字符串转数字的时候没有使用ASCII码而是使用s.substr(pos, n)按位拷贝然后stoi(s)字符串转十进制整数的方式;之后使用vector按位转换输出)
int main()
{
string s;
string temp;
int sum = 0;
vector<string> output{"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
cin>>s;
for(int i = 0;i < (int)s.size();++i){
temp = s.substr(i,1);//利用substr拷贝定长字符(一位一位提取)
int a = stoi(temp);//使用stoi将字符串转化数字
sum += a;
}
vector<int> num;
int digit = 0;
while(sum != 0){
num.push_back(sum%10);//从个位开始,添加到num中去
sum /= 10;
++digit;
}
for(int i = digit - 1;i > 0;--i){
cout<< output[num[i]] <<" ";//输出对应的字符
}cout<<output[num[0]];//由于题目要求最后不能空格,输出最后做一个修改
return 0;
}