1、编写程序,循环输入字符,当输入为回车时输入结束。统计其中英文字母、空格、数字和其他字符的个数并输出。
C语言源码——文艺版:
#include <stdio.h>
#define Que int main(){char input;int character=0, space=0, digit=0, other=0;
#define Ren while ((input = getchar()) != '\n') {
#define Guo if ('a' <= input && input <= 'z' || 'A' <= input && input <= 'Z') {
#define Yan character += 1;} else if ('0' <= input && input <= '9') {
#define Shen digit += 1;} else if (input == ' ') {
#define Yu space += 1;} else {
#define Shang other += 1;}}
#define Dui printf("英文字母:%d\n", character);
#define De printf("数字:%d\n", digit);printf("空格:%d\n", space);
#define ren printf("其他字符:%d\n", other);return 0;}
Que Ren Guo Yan Shen //确认过眼神
Yu Shang Dui De ren //遇上对的人
C语言源码——正常版:
#include <stdio.h>
int main() {
char input;
int character=0, space=0, digit=0, other=0;
while ((input = getchar()) != '\n') {
if ('a' <= input && input <= 'z' || 'A' <= input && input <= 'Z') {
character += 1;
} else if ('0' <= input && input <= '9') {
digit += 1;
} else if (input == ' ') {
space += 1;
} else {
other += 1;
}
}
printf("英文字母:%d\n", character);
printf("数字:%d\n", digit);
printf("空格:%d\n", space);
printf("其他字符:%d\n", other);
return 0;
}
Python源码:
sentence = input('input:')
character = space = digit = other = 0
for i in sentence:
if i.isdigit():
digit += 1
elif i == ' ':
space += 1
elif i.isalpha():
character += 1
else:
other += 1
print("英文字母:%d" % character)
print("数字:%d" % digit)
print("空格:%d" % space)
print("其他字符:%d" % other)
2、输入一段只有字母和空格的文字,统计其中有多少个单词,单词之间以空格隔开。(如输入this is a boy,则输出单词数为4)
C语言源码:
#include <stdio.h>
int main () {
char input;
int sum=1;
while ((input = getchar()) != '\n') {
if (input == ' ') {
sum++;
}
}
printf("%d", sum);
return 0;
}
Python源码:
sentence = input('input:')
print(len(sentence.split(' ')))
3、利用 * 编程在屏幕上显示一个直角三角形
C语言源码:
#include <stdio.h>
int main() {
int i, j;
for (i=1;i<=10;i++) {
for (j=1;j<=i;j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Python源码:
for i in range(1,10):
print('*' * i)
4、求出a\b\c在100以内满足a2 + b2 = c2(a!=b)的自然数。
C语言源码:
#include <stdio.h>
#include <math.h>
int main() {
int i,j,k;
float c;
for (i=1;i<=100;i++) {
for (j=i+1;j<=100;j++) {
k = i*i + j*j;
c = sqrt(k);
if (c == (int)c && c <= 100) {
printf("%d^2 + %d^2 = %d^2\n", i, j, (int)c);
}
}
}
return 0;
}
Python源码:
import math as m
for a in range(1, 100):
for b in range(a+1, 100):
c = m.sqrt(a**2 + b**2)
if c == int(c) and c <= 100:
print("%d^2 + %d^2 = %d^2" % (a, b, c))