Word
Time Limit: 1000MS Memory limit: 65536K
题目描述
Tom is very upset that many people on the internet mix uppercase and lowercase letters in one word. That's why he decided to invent an extension
for his favorite browser that would change the letters' register in every word so that it either only consisted of lowercase letters or, vice versa, only of uppercase ones. At that as little as possible letters should be changed in the word. For example, the
word HoUse must be replaced with house, and the word ViP — with VIP. If a word contains an equal number of uppercase and lowercase letters, you should replace all the letters with lowercase ones. For example, maTRIx should be replaced by matrix. Your task
is to use the given method on one given word.
输入
The first line of input contains an integers T, indicating T test case will follow.Each of the next T lines contains a word s — it consists
of uppercase and lowercase Latin letters and possesses the length from 1 to 100.
输出
Print the corrected word s. If the given word s has strictly more uppercase letters, make the word written in the uppercase register, otherwise
- in the lowercase one.
示例输入
3 HoUse ViP maTRIx
示例输出
house VIP matrix
提示
来源
示例程序
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
void lscase(char str[],int n)
{
int i;
for (i=0;i<strlen(str);i++)
{
if(n)
{
if ((str[i]>='a')&&(str[i]<='z'))
str[i]-=32;
}
else
if ((str[i]>='A')&&(str[i]<='Z'))
str[i]+=32;
}
}
int main(int argc, char *argv[])
{
char c[1000];
int n,count;
scanf("%d",&n);
for (int i=0;i<n;i++)
{
count=0;
scanf("%s",c);
for (int j=0;j<strlen(c);j++)
{
count+=(isupper(c[j])&&1); //*
}
if (2*count<=strlen(c))lscase(c,0);
else lscase(c,1);
printf("%s\n",c);
}
return 0;
}
又是一道水得不能再水的题。字符串处理,大小写转换。
但是居然,这个题WA了15遍,其教训之惨痛、印象之深刻,令人难以忘怀。
话休絮烦,由于本题太水,只总结最重要的错因。
我在程序注释处使用了一个函数:isupper(char);它的作用是判断字符是否是大写字母。重点是它的返回值,我以前一直以为此类判断函数若值为真,则返回1,假时为0。其实不然,若待判断字符当真为大写字母,返回的应是一个大于0的数,而并不一定是1。
类似情况还有strcmp(char * c1,char * c2)这也是一个判断功能函数,它在c1,c2相等的情况下会返回一个大于0的值,而并不一定是1。
以后如碰到类似函数,定要先弄清楚它的方方面面,再来使用。

1352

被折叠的 条评论
为什么被折叠?



