笔记:
1.字符数组char s[]的长度用sizeof(), strlen()
sizeof总结:
没给数组大小的就会数本来有的字符数,给了数组大小就会按给的大小
拆分写字符的就不会自带0,直接写一个字符串的就会自动带0
char arr[] = { 'a','b','c','\0' };//sizeof(arr); 的值为4,包括结束符'\0';
char arr2[] = { 'a','b','c' };//sizeof(arr2); 的值为3;这种写法不好,要记得加上字符串结束符'\0'。
char arr3[20] = { 'a','b','c','d','e','\0' };//sizeof(arr3); 的值为20, 这里要注意,并不是6.
char arr4[] = "ab";//这里会自动在字符串尾部加上字符串结束符'\0',故数组arr4的长度为3.
//sizeof(arr4); 的值为3, 这里要注意,并不是2,因为用字符串字面值初始化字符数组,会自动在字符串后面添加结束符'\0',所以数组的长度会包括'\0'.
char* p = arr4;//sizeof(p)=4,因为指针就是占4个字节的
strlen:
自带0
字符串string s长度s.size()和s.length()都是求的实际的占位,不算0的
2.switch:
switch ()
{
case 1:cout << ;
break;
case
}
用一个显化记录的字符串不就行了。
3.把数字sum粘贴到字符串数组s 上
sprintf(s,"%d",sum);
改:用动态字符串存入。
4.既然用的char s[],想把字符串里的数字加和,当然要用-‘0’啦!!!
5.
for循环i < strlen(str)不能用的话可以用str[i]!='\0'
6.
答案:
我的这个只通过了部分文章来源:https://www.toymoban.com/news/detail-601074.html
#include<iostream>
#include<cstring>
using namespace std;
int main ()
{
char str[110]={0},s[50]={0};//
cin>>str;
long long cnt=0;
for(int i=0;str[i]!='\0';i++)
{
cnt+=str[i]-'0';
}
sprintf(s,"%lld",cnt);
if(cnt>=0&&cnt<=9)
{
switch(cnt)
{
case 1:
cout<<"yin";
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;
case 0:
cout<<"ling";
break;
}
}
else
{
int j;
for(j=0;j<strlen(s)-1;j++)
{
int a=s[j]-'0';
switch(a)
{
case 1:
cout<<"yin ";
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;
case 0:
cout<<"ling ";
break;
}
}
int b=s[j]-'0';
switch(b)
{
case 1:
cout<<"yin"<<endl;
break;
case 2:
cout<<"er"<<endl;
break;
case 3:
cout<<"san"<<endl;
break;
case 4:
cout<<"si"<<endl;
break;
case 5:
cout<<"wu"<<endl;
break;
case 6:
cout<<"liu"<<endl;
break;
case 7:
cout<<"qi"<<endl;
break;
case 8:
cout<<"ba"<<endl;
break;
case 9:
cout<<"jiu"<<endl;
break;
case 0:
cout<<"ling"<<endl;
break;
}
}
return 0;
}
重写了一个正确了文章来源地址https://www.toymoban.com/news/detail-601074.html
#include <iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
string a[15] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
int sum;
vector<string> ve;
int main()
{
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) sum += s[i] - '0';
while (sum)
{
ve.push_back(a[sum % 10]);
sum /= 10;
}
for (int i = ve.size() - 1; i >= 0; i--)
{
cout << ve[i];
if (i)cout << " ";
}
return 0;
}
到了这里,关于pat乙题1002的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!