题目介绍
题目名称:[蓝桥杯][入门题]——Hello, world!
题目描述:
This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numbers and output corresponding messages.
输入:
The input will contain a list of positive integers separated by whitespaces(spaces, newlines, TABs). Please process to the end of file (EOF). The integers will be no less than 32.
输出:
Output the corresponding message. Note there is NOT a newline character in the end of output.
样例输入:
72 101 108 108 111 44
32 119 111 114 108 100 33
样例输出:
Hello, world!
题目解答
搞清楚这个题你需要知道:
- 可以输入空格、新行、TAB的情况下,如何用EOF来结束存储这些普通数字?
顺便附上一张ASCII表镇一镇文章
方法1:注意输入的时候Alt+10可以换行 就可以避免用到回车,scanf当只有一个赋值对象的时候并且赋值成功的话,返回值为1,否则为0;当有n个赋值对象都成功赋值的话返回n;结束输入的时候用Ctrl+Z回使得scanf的返回值为-1。
#include<stdio.h>
int main(){
int n;
while(scanf("%d",&n)==1) //或者while(~scanf("%d",&n)) 或者while(scanf("%d",&n)=='EOF')但是这个会输入超限
printf("%c",n);
return 0;
}
方法2:这个方法不用避免回车,直接敲,自己测试的时候想输EOF到程序里必须要先回车然后Ctrl+Z再回车。
#include<stdio.h>
int main(){
char c;
int a[100],i=0;
while((scanf("%d",&a[i]))!=EOF){
i++;
}
for(int j=0;j<i;j++){
printf("%c",a[j]);
}
return 0;
}
不知道有没有朋友跟我一样的疑问,这里敲了回车符保存到了数组里,那么输出%c应该会输出回车产生一个换行的效果啊,为什么没有呢。我调试了一下发现,因为%d只读整数,%c读所有东西,所以数组里面是不会存在除整数以外的东西
My_Error
因为看到输入是带TAB,空格,换行的,所以最初的时候想到使用gets,开了一个大数组,存入了gets得到的字符数组,然后想办法利用atoi或者sprintf转成整型数组,再转换成ASCII对应的图形,导致程序异常的麻烦和复杂而且最终没有实现。
本人水平有限,不足之处在所难免,恳请各位师傅指正!