编程初学者
37 竞选舍长
题目40:假设你们社团要竞选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终得票多的人为社长.
//40、竞选舍长
//第一种方法:一个字符一个字符的输入
#include <stdio.h>
int main(){
//这里ch的类型为int 不为char的原因是:
//getchar返回类型为整形,返回的是字符的ASCII码值,
//所以这里可以为char 也可以为int
//但是int更合适一些,由于EOF相当于-1.
int ch = 0;
int count_a = 0;//统计A的个数
int count_b = 0;//统计B的个数
//输入
//getchar函数是获取一个字符,如果获取多个字符,用循环。
//结束标志是'0',所以不为'0'时,进入循环。 EOF是多组输入的结束标志。
while(((ch = getchar()) != '0') && (ch != EOF)){
if(ch == 'A'){
count_a++;
}
if(ch == 'B'){
count_b++;
}
}
//输出
if(count_a == count_b){
printf("%c\n", 'E');
}else if(count_a > count_b){
printf("%c\n", 'A');
}else{
printf("%c\n", 'B');
}
return 0;
}
//第二种方法:简写
#include <stdio.h>
int main(){
int ch = 0;
int count = 0;
//输入
//getchar函数是获取一个字符,如果获取多个字符,用循环。
//结束标志是'0',所以不为'0'时,进入循环。 EOF是多组输入的结束标志。
while(((ch = getchar()) != '0') && (ch != EOF)){
if(ch == 'A'){
count++;
}
if(ch == 'B'){
count--;
}
}
//输出
if(count == 0){
printf("E\n");
}else if(count > 0){
printf("A\n");
}else{
printf("B\n");
}
return 0;
}
//第三种方法:一行字符输出
#include <stdio.h>
int main(){
//定义一个字符串
char str[100] = {0};
//读取一行字符
gets(str);
int count = 0;
int i = 0;
while(str[i] != '0'){
//数组中的每个字符进行判断
if(str[i] == 'A'){
count++;
}
if(str[i] == 'B'){
count--;
}
//进行下一个字符
i++;
}
//输出
if(count == 0){
printf("E\n");
}else if(count > 0){
printf("A\n");
}else{
printf("B\n");
}
return 0;
}
38 你是天才
题目41:据说智商140以上者称为天才,KiKi想知道他自己是不是天才,请帮他编程判断。输入一个整数表示一个人的智商,如果大于等于140,则表明他是一个天才,输出“Genius”。
//41 你是天才吗?
//第一种多组输入的方法:scanf返回值
#include <stdio.h>
int main(){
int n = 0;//表示智商
//多组输入(scanf("%d", &n)) != EOF
//scanf返回值为输入的字符个数,结束标志为EOF --> -1
while((scanf("%d", &n)) != EOF){
if(n >= 140){
printf("Genius\n");
}
}
return 0;
}
//第二种多组输入的方法:~按位取反操作
#include <stdio.h>
int main(){
int n = 0;//表示智商
//多组输入 ~ (scanf("%d", &n))
//-1
//10000000 00000000 0000000 0000001 -1的原码
//11111111 11111111 1111111 1111110 -1的反码
//11111111 11111111 1111111 1111111 -1的补码
//~ -1
//00000000 00000000 0000000 0000000
//while循环若条件为假,不能进入循环。
while( ~ (scanf("%d", &n))){
if(n >= 140){
printf("Genius\n");
}
}
return 0;
}
39 完美成绩
题目42:KiKi想知道他的考试成绩是否完美,请帮他判断。从键盘输入一个整数表示的成绩,编程判断成绩是否在90~100之间,如果是则输出“Perfect”。
//42 完美成绩
#include <stdio.h>
int main(){
int score = 0;
while( ~ (scanf("%d", &score))){
//if(90 <= score <= 100)
// printf("Perfect\n");
//若输入120,本不该输出Perfect
//但如果这样写90 <= score <= 100
//第一步:120 满足 90 <= score 输出1
//第二步:if(1 <= 100) 所以会输出Perfect
if(score >= 90 && score <= 100){
printf("Perfect\n");
}
}
return 0;
}
40 及格分数
题目43:KiKi想知道他的考试分数是否通过,请帮他判断。从键盘任意输入一个整数表示的分数,编程判断该分数是否在及格范围内,如果及格,即:分数大于等于60分,是输出“Pass”,否则,输出“Fail”。
//43 及格分数
#include <stdio.h>
int main(){
int score = 0;
while(~ (scanf("%d", &score))){
if(score >= 60){
printf("Pass\n");
}else{
printf("Fail\n");
}
}
return 0;
}
41 判断整数奇偶性
题目44:KiKi想知道一个整数的奇偶性,请帮他判断。从键盘任意输入一个整数(范围-231~231-1),编程判断它的奇偶性。
//44 判断整数的奇偶性
#include <stdio.h>
int main(){
int num = 0;
while(~ (scanf("%d", &num))){
if(num%2 == 0){
printf("Even\n");
}else{
printf("Odd\n");
}
}
return 0;
}
42 最高分数
题目45:KiKi参加了语文、数学、外语的考试,请帮他判断三科中的最高分。从键盘任意输入三个整数表示的分数,编程判断其中的最高分。
//45 最高分数
//第一种方法;数组遍历
#include <stdio.h>
int main(){
int s[3] = {0};
while(~ (scanf("%d %d %d", &s[0], &s[1], &s[2]))){
//假设max中存放的是最大值
//max是局部变量,因为每输入一组数据,都要先将其max初始化为0,之后再比较,得到最大值,存放在max中。
int max = 0;
//遍历数组,确定最大值
int i = 0;
for(; i<3; i++){
if(s[i] > max){
max = s[i];
}
}
//三次比较完之后,输出最大值。
printf("%d\n", max);
}
return 0;
}
//第二种方法:常规思路
#include <stdio.h>
int main(){
int s1 = 0;
int s2 = 0;
int s3 = 0;
while(~ (scanf("%d %d %d", &s1, &s2, &s3))){
int max = 0;
if(s1>max){
max = s1;
}
if(s2>max){
max = s2;
}
if(s3>max){
max = s3;
}
printf("%d\n",max);
}
return 0;
}
43 判断是元音还是辅音
题目46:KiKi开始学习英文字母,BoBo老师告诉他,有五个字母A(a), E(e), I(i), O(o),U(u)称为元音,其他所有字母称为辅音,请帮他编写程序判断输入的字母是元音(Vowel)还是辅音(Consonant)。
//46 判断元音还是辅音
//getchar输入字符
#include <stdio.h>
int main(){
int ch = 0;
char s[] = "AaEeIiOoUu";//11个元素 0-9
while((ch = getchar()) != EOF){
//遍历数组,确定ch是否和数组中的元素相等
int i = 0;
for(; i<10; i++){
if(ch == s[i]){
printf("Vowel\n");
break;
}
}
//break跳出和i>=10两种情况
if(i == 10){
printf("Consonant\n");
}
//消除getchar获取字符后的'\n'
getchar();
}
return 0;
}
//scanf输入字符
#include <stdio.h>
int main(){
int ch = 0;
char s[] = "AaEeIiOoUu";//11个元素 0-9
while((scanf("%c", &ch)) != EOF){
//遍历数组,确定ch是否和数组中的元素相等
int i = 0;
for(; i<10; i++){
if(ch == s[i]){
printf("Vowel\n");
break;
}
}
//break跳出和i>=10两种情况
if(i == 10){
printf("Consonant\n");
}
//消除getchar获取字符后的'\n'
getchar();
}
return 0;
}
//scanf输入字符,加空格,或者%c后面加\n,消除'\n' --- (scanf(" %c", &ch)) != EOF
//%c前面写一个空格,表示在读取字符的时候,忽略所有的空白字符
//或者%c后面加\n
#include <stdio.h>
int main(){
int ch = 0;
char s[] = "AaEeIiOoUu";//11个元素 0-9
while((scanf(" %c", &ch)) != EOF){
//遍历数组,确定ch是否和数组中的元素相等
int i = 0;
for(; i<10; i++){
if(ch == s[i]){
printf("Vowel\n");
break;
}
}
//break跳出和i>=10两种情况
if(i == 10){
printf("Consonant\n");
}
}
return 0;
}
44 判断是不是字母
题目47:KiKi想判断输入的字符是不是字母,请帮他编程实现。
//判断是不是字母
//方法1:常规方法
#include <stdio.h>
int main(){
int ch = 0;
while((ch = getchar()) != EOF ){
if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
printf("%c is an alphabet.\n",ch);
}
else
{
printf("%c is not an alphabet.\n",ch);
}
//清空缓冲区
getchar();
}
return 0;
}
//方法2:库函数方法
#include <stdio.h>
#include <ctype.h>
int main(){
int ch = 0;
while((ch = getchar()) != EOF ){
if(isalpha(ch))
{
printf("%c is an alphabet.\n");
}
else
{
printf("%c is not an alphabet.\n");
}
//清空缓冲区
getchar();
}
return 0;
}
45 判断大小写转换
题目:KiKi想完成字母大小写转换,有一个字符,判断它是否为大写字母,如果是,将它转换成小写字母;反之则转换为大写字母。
//大小写转换
//第一种方法:常规方法
#include <stdio.h>
int main(){
int ch = 0;
while((ch = getchar()) != EOF ){
if(ch >= 'A' && ch <= 'Z'){
printf("%c\n",ch+32);//大写转小写+32
}else if(ch >= 'a' && ch <= 'z'){
printf("%c\n",ch-32);//小写转大写-32
}
//清空缓冲区
getchar();
}
return 0;
}
//第二种方法:库函数方法
#include <stdio.h>
#include <ctype.h>
int main(){
int ch = 0;
while((ch = getchar()) != EOF ){
//注释:isupper --- 判断是否为大写字母
// islower --- 判断是否为小写字母
// tolower --- 大写转小写
// toupper --- 小写转大写
if(isupper(ch)){
printf("%c\n",tolower(ch));//大写转小写+32
}else if(islower(ch)){
printf("%c\n",toupper(ch));//小写转大写-32
}
//清空缓冲区
getchar();
}
return 0;
}
46 计算单位阶跃函数
题目50: KiKi最近学习了信号与系统课程,这门课里有一个非常有趣的函数,单位阶跃函数。
//计算单位阶跃函数
#include <stdio.h>
int main(){
int t = 0;
while(~scanf("%d",&t)){
if(t>0){
printf("%d\n",1);
}else if(t == 0){
printf("%.1f\n",0.5);
}else{
printf("%d\n",0);
}
}
return 0;
}
47 三角形判断
题目51:KiKi想知道已经给出的三条边a,b,c能否构成三角形,如果能构成三角形,判断三角形的类型(等边三角形、等腰三角形或普通三角形)。
//判断三角形
#include <stdio.h>
int main(){
int a = 0;
int b = 0;
int c = 0;
while(~scanf("%d %d %d", &a, &b, &c)){
//三角形
if((a+b>c) && (a+c>b) && (b+c>a)){
//等边三角形
if((a==b) && (a == c)){
printf("Equilateral triangle!\n");
}
//等腰三角形
else if(((a==b) && (b!=c)) || ((a==c) && (c!=b)) || ((b==c) && (c!=a))){
printf("Isosceles triangle!\n");
}
//其余三角形
else{
printf("Ordinary triangle!\n");
}
//不是三角形
}else{
printf("Not a triangle!\n");
}
}
return 0;
}