kimikaZakuro @ 2024-05-14 20:48:32
#include <stdio.h>
#include <string.h>
char s[80];//存储输入串
char a[15];//存储每个单词
int b[9];//存储所有数字
int cnt = 0;//记录一共多少个数
char num1[30][20] = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty","a","both","another","first","second","third" };
int num2[30]={0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 00, 21, 44, 69, 96, 25, 56, 89, 24, 61, 0, 1, 4, 1, 1, 4, 9};
void judge()
{
for (int i = 0; i < 27; i++)
{
if (strcmp(&*num1[i],a)==0)
//犯错:strcmp比较字符串,不能用==
//犯错:strcmp返回负数、0、正数
//犯错:二维数组指针有两层
b[cnt++] = num2[i];
}
}
void sort()//冒泡排序
{
int flag = 0;
for (int i = 0; i < cnt; i++)//i为已排好的个数
{
for (int j =0;j<cnt-i-1;j++)
//犯错:j+1越界
//for (int j =0;j<cnt-i;j++)
{
if (b[j] > b[j + 1])
{
int temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
flag = 1;
}
}
if (!flag)//效率优化
break;
else
flag = 0;
}
}
int main()
{
fgets(s, sizeof(s), stdin);
s[strlen(s) - 2] = '\0';//覆盖'.'
for (int i=0;i<strlen(s);i++)
{
if (s[i] != ' ')
{
int j = i;
while (s[j] != ' ')
j++;
strncat(a, &s[i], j - i);
judge();
memset(a, 0, sizeof(a));
i = j;
}
else
;
}
sort();
//处理b中的数据,排列输出
int sign = 1;
//控制第一个输出<10的数字,消去0
for (int i = 0; i < cnt; i++)
{
if (b[i] == 0)
continue;
else if (b[i] < 10)
{
if (sign)
{
printf("%d", b[i]);
sign = 0;
}
else
printf("%02d", b[i]);
}
else
printf("%d", b[i]);
}
return 0;
}
by kimikaZakuro @ 2024-05-14 20:48:59
球球大佬了
by PRew_ @ 2024-05-14 20:57:07
关掉吸氧
by PRew_ @ 2024-05-14 20:57:43
@kimikaZakuro
by kimikaZakuro @ 2024-05-14 21:10:52
@nothing_exe_studio 谢谢回复,不过还是超时了哈哈哈
by PRew_ @ 2024-05-14 21:14:01
不要用冒泡排序,用二路并归排序降到O(n log n)
by PRew_ @ 2024-05-14 21:14:13
@kimikaZakuro
by kimikaZakuro @ 2024-05-14 21:50:33
@nothing_exe_studio 确实可以了,感谢!
by PRew_ @ 2024-05-15 18:59:51
@kimikaZakuro 给个关注呗OvO