一道简单的ACM选拔题,咋就总是不对呢?
要求实现这样一个简单的输入和输出,我相信应该很容易看明白吧,
但是我写这个代码在只输入一个的时候正常,如:输入LIUYan,输出的是LIUYAN
但是在有几个输入时,不管输入是什么,输出都全是小写了,如:输入LIUYan,输出也是liuyan,
请问下,这个是怎么回事啊?
我代码哪里出问题了么?
谢谢了!!
#include <stdio.h>ACM C
#include <ctype.h>
#define M 100
int main (void)
{
int n, i, j, ct = 0;
printf ("input up to the numbers of line (the lines are not more than 30): ");
scanf ("%d", &n);
getchar ();
char letter[n][M];
printf ("input up to the words per line:\n ");
printf ("To stop, press [Enter] at a line's start.\n");
while (ct < n && gets (letter[ct]) != NULL && letter[n][0] != '\0')
ct++;
for (ct = 0; ct < n; ct++)
puts (letter[ct]);
for (i = 0; i < n; i++)
{
int upp = 0, low = 0;
for (j = 0; j < M; j++)
{
if (letter[i][j] == '\0')
break;
if (isupper (letter[i][j]))
upp++;
if (islower (letter[i][j]))
low++;
}
if (upp > low)
for (i = 0; i < n; i++)
for (j = 0; j < M; j++)
{
letter[i][j] = toupper (letter[i][j]);
if (letter[i][j] == '\0')
break;
}
else
{
for (i = 0; i < n; i++)
for (j = 0; j < M; j++)
{
letter[i][j] = tolower(letter[i][j]);
if (letter[i][j] == '\0')
break;
}
}
}
for (ct = 0; ct < n; ct++)
puts (letter[ct]);
system ("pause");
return 0;
}
[解决办法]
1. ACM选拔题肯定不要前面那么多废话printf。
2. 样例看不懂。原题呢。
3. 你if (upp > low)下面的把所有数据全都改了大小写。你是只想改当前字符串的大小写吧?
[解决办法]
代码还算工整的,不错
不过确实如楼上所说,不用那么多废话。。
Sample Output:跟你说怎么输出,你就怎么输出,不用一大堆hint
另外,这么简单的题目,为什么你需要那么复杂的写法呢?我手敲试试:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int main()
{
int total;
cin>>total;
string temp
while(total--)
{
cin>>temp;
transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
cout<<temp<<endl;
}
}
[解决办法]
你写的废话太多了。。
我重写了一下,你看一下吧。。
[解决办法]
1.估计是选拔ACM的队员的校内题目,要求比较低,所以才有那么多的格式。
2.你程序里面的for循环和if else太多了,一个for循环遍历完生成两个字符串(一个大写一个小写),统计字母的大小写个数,大写多就输出大写字符串...,时间复杂度也就O(n).
[解决办法]
恩,都是共同学习吗,不懂的话可以互相交流