C语言_SDUST_OJ(3)

这篇博客主要介绍了C语言中字符统计的问题,包括如何统计英文字母、数字、空格和标点符号的出现次数。同时,讲解了二进制整数转化为十进制的方法,以及模拟登录密码验证的逻辑,最后讨论了如何将一天中的秒数转换为24小时制的时间格式。博客中强调了使用数组简化编程的重要性,并给出了若干提示和示例输入输出。
摘要由CSDN通过智能技术生成

字符统计

Description
给出一篇英文文章,含大小写字母、数字、标点符号和空白符等,统计其中各个英文字母出现的次数和各类字符出现的次数。
统计各类字符时,应使用头文件<ctype.h>中的字符分类函数。这里用到以下5个函数:
islower(c)c是否小写字母:‘a’~’z’;
isupper(c) c是否大写字母:‘A’~’Z’;
isdigit(c) c是否数字:‘0’~’9’;
isspace(c) c是否空白字符:包括空格(’ ‘)、换页符(’\f’)、换行符(’\n’)、回车符(’\r’)、水平制表符(’\t’)和垂直制表符(’\v’);
ispunct(c) c是否标点符号:标点符号包括除字母、数字和空白符之外的所有可打印字符,也就是说输入的所有字符减去以上4类字符数正好是标点符号数。(仅限C99)
注意,在C89中(如VC6.0),ispunct()函数的定义与C99(如本地的codeblocks和OJ后台的gcc)不同。在C89中,ispunct©在c为除空格符和字母、数字之外的所有可打印字符时都返回真。所以,当你使用VC6.0编译器时,使用该函数本地计算的结果可能不对,但是不影响提交后的结果正确,如果你的程序没有其他错误。

Input
输入为一篇英文文章,最少有一个字符,至EOF结束。

Output
输出各类字符的出现次数和每个字母的出现次数。按顺序,每行输出依次为:所有字符数、小写字母数、大写字母数、数字字符数、空白符数、标点符号数,然后是从A~Z所有字母出现的次数。每个字母的出现次数是大小写合并统计的。

输出格式见sample。

Sample Input

SDUSTOJ Online Judge FAQ
Q:What is the compiler the judge is using and what are the compiler options?
A:The online judge system is running on Debian Linux. We are using GNU GCC/G++ for C/C++ compile, Free Pascal for pascal compile and sun-java-jdk1.6 for Java. The compile options are:
C:		gcc Main.c -o Main -O2 -Wall -lm --static -std=c99 -DONLINE_JUDGE
C++:	g++ Main.cc -o Main -O2 -Wall -lm --static -DONLINE_JUDGE
Pascal:	fpc Main.pas -oMain -O1 -Co -Cr -Ct -Ci
Java:	javac -J-Xms32m -J-Xmx256m Main.java 
*Java has 2 more seconds and 512M more memory when running and judging.
Our compiler software version:
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
glibc 2.3.6
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
java version "1.6.0_22"

Q:Where is the input and the output?
A:Your program shall read input from stdin('Standard Input') and write output to stdout('Standard Output').For example,you can use 'scanf' in C or 'cin' in C++ to read from stdin,and use 'printf' in C or 'cout' in C++ to write to stdout.
User programs are not allowed to open and read from/write to files, you will get a "Runtime Error" if you try to do so.

Here is a sample solution for problem 1000 using C++:
#include 
using namespace std;
int main(){
    int a,b;
    while(cin >> a >> b)
        cout << a+b << endl;
	return 0;
}

Here is a sample solution for problem 1000 using C:
#include 
int main(){
    int a,b;
    while(scanf("%d %d",&a, &b) != EOF)
        printf("%d\n",a+b);
	return 0;
}

Here is a sample solution for problem 1000 using PASCAL:
program p1001(Input,Output); 
var 
  a,b:Integer; 
begin 
   while not eof(Input) do 
     begin 
       Readln(a,b); 
       Writeln(a+b); 
     end; 
end.

Here is a sample solution for problem 1000 using Java:
import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int a, b;
		while (cin.hasNext()){
			a = cin.nextInt(); b = cin.nextInt();
			System.out.println(a + b);
		}
	}
}

Sample Output

All Characters : 2020
Lowers : 1173
Uppers : 135
Digits : 70
Spaces : 405
Puncts : 237
A : 113
B : 24
C : 62
D : 47
E : 108
F : 25
G : 32
H : 23
I : 111
J : 19
K : 1
L : 59
M : 51
N : 121
O : 97
P : 46
Q : 3
R : 83
S : 78
T : 92
U : 63
V : 14
W : 19
X : 8
Y : 9
Z : 0

HINT
静下心来想想,你能用数组做些什么?来简化程序的编写。否则,这么多种类需要统计,只是定义单字母的变量还不够用呢。那就太麻烦了!
Sample的结果跟你的程序本地运行结果不一致,是因为数据中有的行字符太多没显示全,并且文中有很多对齐的地方使用的是制表符’\t’,而不是空格,所以,你只能依赖自己构造测试数据的能力了。

代码

用数组简化编程,注意ASCII码与字符的转换

#include<stdio.h>
#include<ctype.h>
int main()
{
    char c;
    int s[33]={0};
    int n[30]={0};
    while(scanf("%c",&c)!=EOF)
    {
        s[0]++;
        int flag=0;
        if(islower(c))
        {
            s[1]++;
            flag=1;
        }
        else if(isupper(c))
        {
            s[2]++;
            flag=2;
        }
        else if(isdigit(c))
            s[3]++;
        else if(isspace(c))
            s[4]++;
        else if(ispunct(c))
            s[5]++;
        if(flag==1)
        {
            int i=(int)(c)-97;
            n[i]++;
        }
        else if(flag==2)
        {
            int i=(int)(c)-65;
            n[i]++;
        }

    }
    printf("All Characters : %d\nLowers : %d\nUppers : %d\nDigits : %d\nSpaces : %d\nPuncts : %d\n",s[0],s[1],s[2],s[3],s[4],s[5]);
        int j=0;
        for(j=0;j<=25;j++)
        {
            printf("%c : %d\n",j+'A',n[j]);
        }

}

二进制整数转十进制

Description
给出一个二进制的非负整数x,x<232,把它转换成十进制数输出。

Input
输入为多行,每行一个二进制非负整数x。
Output
每行输出x对应的十进制数值。

Sample Input

0
1
01
10
11
100001
1111111111111111

Sample Output

0
1
1
2
3
33
65535

HINT
注意数据范围!!!

1.虽然让注意数据范围了,可还是没想到ll
2.还有一点,pow函数的使用
3.这个数组里边是字符型的,不能直接×,要想清楚

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    char s[33];
    while(scanf("%s",&s)!=EOF)
    {
        long long int sum=0;
        int i;
        int j=0;
        for(i=strlen(s)-1;i>=0;i--)
        {
            if(s[i]=='1')
                sum+=1*(pow(2,j));//!!!!!!!!!!!
            j++;
        }
        printf("%lld\n",sum);//!!!!!!!!!!
    }
}

登录密码验证

Description
编写一个程序,模拟用户登录系统的密码验证过程。系统提供给用户的密码长度最长为20个字符,若密码输入错误可以再次输入。但为了保证用户密码安全,若连续输入密码错误超过5次就会锁定账号一段时间。

Input
输入为若干个串,至EOF结束。输入的第一个串是用户的正确密码,后面的串为模拟用户登录时的输入的密码。

Output
每次输入错误的密码,输出一个“Wrong!”,若输入的密码为正确的,输出一个“Welcome!”,并结束密码测试。若前5次输入的密码都是错误的,则后面的输入中不管是否有正确的密码都输出“Out of limited!”。

Sample Input

abcdefg
123456 kkkkkkkk abcdefg

Sample Output

Wrong!
Wrong!
Welcome!

HINT
输入可以用scanf("%s")处理,密码比较用字符串的比较可以完成。

1.没理解题意,错五次不是直接结束了,而是还继续允许输入,但是一直输出Out of limited!

#include<stdio.h>
#include<string.h>
int main()
{
    char s[100],a[100];
    scanf("%s",&s);
    int c=0;
    int flag=0;
    while(scanf("%s",&a)!=EOF)
    {
        c++;
        if(flag==1)
        {
            printf("Out of limited!\n");
            continue;
        }
        if(c==6)
        {
            printf("Out of limited!\n");
            flag=1;
            continue;
        }
        if(strcmp(s,a)==0)
        {
            printf("Welcome!\n");
            return;
        }
        else if(strcmp(s,a)!=0)
        {
            printf("Wrong!\n");
        }
    }

}

几点几分几秒

Description
一天24小时,每小时60分钟,每分钟60秒。一天共有86400秒。

0点0分0秒是每天的第1秒;
0点0分1秒是每天的第2秒;
0点1分0秒是每天的第61秒;
1点0分0秒是每天的第3601秒;
23点59分59秒是每天的第86400秒。
你的任务是编写一个程序,把每天的第n秒转换成具体的24小时制时间(从00:00:00到23:59:59)。

Input
输入为若干整数n,表示每天的第n秒,1<=n<=86400,当输入n为0时表示输入结束。
Output
每行输出一个第n秒对应的具体时间,格式为“hh:mm:ss”。时、分、秒各占2位,不足两位要补0,如0点0分0秒为“00:00:00”。
Sample Input

1
2
61
3600
9999
86400
0

Sample Output

00:00:00
00:00:01
00:01:00
00:59:59
02:46:38
23:59:59

1.这里的输出格式。。。又忘了

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)&&n!=0)
    {
        n-=1;
        int s=n%60;
        int m=(n/60)%60;
        int h=(n/60/60)%60;
        printf("%02d:%02d:%02d\n",h,m,s);//!!!
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值