自己记录用!方法很笨,底子不好!
输入数据很大,用字符串输入转成数字。用strlen计算字符串长,把字符串读到char类型数组A中。用它得加一个<string.h>,<string>不行。A[i]-'0'后就变成了每一位数字(ASCII码)。其中本来用gets(A)读取字符串,自己电脑用DEVC++可以,但是PTA上不识别,改成了cin。
switch不加break会满足一个case及之后的都会输出,所以要记得加break。case后可以是‘1’则是字符1,只写1才是数字。不是switch格式中的。
第一次提交部分正确。应该是因为测试数据太大,我sum默认三位数了。下面的是错误代码
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int i,n,sum=0,j,k;
char A[1000];
//gets (A);
cin>>A;
j=strlen(A);//.h
for(i=0;i<j;i++){//遍历字符串判断是否是数字
if(A[i]>='0'&&A[i]<='9')//如果是数字就累加
sum+=A[i]-'0';
}
i=sum/100;
if(i!=0)
sum=sum-i*100;
switch(i){
case 1:cout<<"yi ";break;
case 2:cout<<"er ";break;
case 3:cout<<"san ";break;
case 4:cout<<"si ";break;
case 5:cout<<"wu ";break;
case 6:cout<<"liu ";break;
case 7:cout<<"qi ";break;
case 8:cout<<"ba ";break;
case 9:cout<<"jiu ";break;
}
i=sum/10;
if(i!=0)
sum=sum-i*10;
switch(i){
case 1:cout<<"yi ";break;
case 2:cout<<"er ";break;
case 3:cout<<"san ";break;
case 4:cout<<"si ";break;
case 5:cout<<"wu ";break;
case 6:cout<<"liu ";break;
case 7:cout<<"qi ";break;
case 8:cout<<"ba ";break;
case 9:cout<<"jiu ";break;
}
switch(sum){
case 1:cout<<"yi";break;
case 2:cout<<"er";break;
case 3:cout<<"san";break;
case 4:cout<<"si";break;
case 5:cout<<"wu";break;
case 6:cout<<"liu";break;
case 7:cout<<"qi";break;
case 8:cout<<"ba";break;
case 9:cout<<"jiu";break;
}
}
因为太大,sum位数也不知道。只能想用另一个数组暂存sum的每一位。B中位是反的顺序。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int i,n,sum=0,j,k=0;
char A[1000];
int B[100];
//gets (A);
cin>>A;
j=strlen(A);//.h
for(i=0;i<j;i++){//遍历字符串判断是否是数字
if(A[i]>='0'&&A[i]<='9')//如果是数字就累加
sum+=A[i]-'0';
}
for(k=0;k<100;k++){
B[k]=sum%10;
sum=sum/10;
}
for(i=k;i>0;i--){
switch(B[i]){
case 1:cout<<"yi ";break;
case 2:cout<<"er ";break;
case 3:cout<<"san ";break;
case 4:cout<<"si ";break;
case 5:cout<<"wu ";break;
case 6:cout<<"liu ";break;
case 7:cout<<"qi ";break;
case 8:cout<<"ba ";break;
case 9:cout<<"jiu ";break;
}
}
switch(B[i]){
case 1:cout<<"yi";break;
case 2:cout<<"er";break;
case 3:cout<<"san";break;
case 4:cout<<"si";break;
case 5:cout<<"wu";break;
case 6:cout<<"liu";break;
case 7:cout<<"qi";break;
case 8:cout<<"ba";break;
case 9:cout<<"jiu";break;
}
}
。。。。怎么还是部分正确。讨厌,哼!!!!!!!
哦哦哦差在0上。然后之前那个k其实没用上。。。记得存B的时候判断一下除数是不是0了,是的话就不再存了,跳出循环,k是不为0的个数,记得下面用的时候k-1。
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int i,n,sum=0,j,k=0;
char A[1000];
int B[100];
//gets (A);
cin>>A;
j=strlen(A);//.h
for(i=0;i<j;i++){//遍历字符串判断是否是数字
if(A[i]>='0'&&A[i]<='9')//如果是数字就累加
sum+=A[i]-'0';
}
for(k=0;k<100;k++){
if(sum!=0){
B[k]=sum%10;
sum=sum/10;
}
else break;
}
for(i=k-1;i>0;i--){
switch(B[i]){
case 0:cout<<"ling ";break;
case 1:cout<<"yi ";break;
case 2:cout<<"er ";break;
case 3:cout<<"san ";break;
case 4:cout<<"si ";break;
case 5:cout<<"wu ";break;
case 6:cout<<"liu ";break;
case 7:cout<<"qi ";break;
case 8:cout<<"ba ";break;
case 9:cout<<"jiu ";break;
}
}
switch(B[i]){
case 0:cout<<"ling";break;
case 1:cout<<"yi";break;
case 2:cout<<"er";break;
case 3:cout<<"san";break;
case 4:cout<<"si";break;
case 5:cout<<"wu";break;
case 6:cout<<"liu";break;
case 7:cout<<"qi";break;
case 8:cout<<"ba";break;
case 9:cout<<"jiu";break;
}
}
PTA 1001
唉只有这个题是顺利的。呜呜呜,不想学了。
#include<iostream>
using namespace std;
int main(){
int n,i=0;
cin>>n;
while(n!=1){
if (n%2==0) {n=n/2;i=i+1;}
else {n=(3*n+1)/2;i=i+1;}
}
cout<<i;
}