HDU-2573-Typing

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2573

这题把%s与gets()的输入法搞混了一直感觉没有错,就是找不出哪里错了,

题目思路不是很难。

直接看代码

#include<stdio.h>
#include<string.h>
int main(void)
{
char s[10];
int l,i,j,flag;
int t,n;
scanf("%d",&t);
while(t--)
{
flag=0;
j=0;
scanf("%d",&n);
getchar();
while(n--)
{
gets(s);
l=strlen(s);
if(l==1)
{
if(flag)
printf("%c",s[0]-32);
else
printf("%c",s[0]);
}
else if(l==4)
{
flag=!flag;
}
else
{
if(flag)
printf("%c",s[6]);
else
printf("%c",s[6]-32);
}
}
printf("\n");
}
return 0;
}

 

如果熟悉sscanf()的也可以这样做

#include<stdio.h>

#include<string.h>
int main()
{
    int t,n,i,k;
    char a[110][10];
    char b[10],c[10];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
        for(i=0;i<n;i++)
        {
             gets(a[i]);
        }
        k=0;
        for(i=0;i<n;i++)
        {
           sscanf(a[i],"%[^ ]",b);
           sscanf(a[i],"%*s%s",c);
           if(strcmp(b,"Caps")==0)
           {
               k++;
               continue;
           }
           else if(strcmp(b,"Shift")==0)
           {
               if(k%2==0)
               printf("%c",c[0]-32);
               else
               printf("%c",c[0]);
           }
           else if(k%2==0)
           printf("%s",b);
           else if(k%2==1)
           printf("%c",b[0]-32);
        }
        printf("\n");
    }
    return 0;
}
posted @ 2014-09-10 15:54  立刻行动  阅读(195)  评论(0编辑  收藏  举报