//int 整数/整型;short 短整型; long 长整型; long long 更长整型
//char 字母 %: a%b表示a/b取余数 \0: 在\0处停止运行 输入字符串逗号后不用&:scanf("%s",a);
//float 短的小数,单精度浮点数
//double 长的小数,双精度浮点数
// 整数:int printf("%d") scanf("%d"); 带小数点的数:float,double printf("%f") scanf("%lf") printf(".nf%")表示小数保留了n位
//由于某些问题scanf不能运行时应换成scanf_s
//a+=5——>a=a+5 复合赋值运算符:"+= -= *= /= %="(2个运算符中间无空格)
/*const意味不变的,放在int前,确定属性 */ // 循环中针对多组数据输入输出使用 !EOF :while(scanf("%d",&n) !=EOF)
// 字符串的比较不能使用==,,要是有strcmp() : char input[]={0}; ---> strcmp(input,"CHEN")
//表达式:a++ 运算:给a+1 表达式的值:a原来的值 表达式:a-- 运算:给a-1 表达式的值:a原来的值
//表达式:++a 运算:给a+1 表达式的值:a+1以后的值 表达式:--a 运算:给a-1 表达式的值:a-1以后的值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
// #include <math.h> // 给定一个浮点数,要求得到该浮点数的个位数。
int main() {
/* printf("%d\n", 1 + 2);
printf("1+1=%d", 1 + 1);
system("color 6"); //system("color+数字")可以改变运行结果颜色 */
/* int a = 9;//=表示将右边的值赋给左边; a=9表示将9赋值给a
int b = a + 7;
printf("%d",b); */
/* int a, b, j = 88;
printf("这件商品的价格是:%d¥\n",j);
printf("请输入交易金额(¥):");
scanf("%d¥", &a);
b = a - j;
printf("找您:%d¥\n", b);
system("color 6"); */
/* int a, b;
printf("请输入a和b的值:");
//scanf("%d,%d",&a,&b);
scanf("%d %d", &a, &b);//输入格式应与编码格式一致 如:有空格(逗号)时输入时要输入空格(逗号)
int j = a + b;
printf("j=%d",j); */
/* const int a = 6;
int a = 7; */ //const:不变的,确定的
/* int x = 123, y = 500;
printf("%d+%d=%d", x, y, x + y); */
/* double a = 10, b = 3;
scanf("%lf", &a, &b);
printf("%.2f", a / b); */
/* int a = 10;
printf("a++=%d\n", a++);
printf("a=%d\n", a); */
/* printf("++a=%d\n", ++a);
printf("a=%d", a); */
/* char a[] = {"CHEN"};
scanf("%s", a);
printf("%s\n", a); */
/* printf("%d\n", sizeof(float)); // 数据类型大小/字节,1字节byte=8比特bit
printf("%d\n", sizeof(short)); */
/* int a = 10, b = 20, c = 30;
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
printf("%d", a);
}
else {
if (b > c) {
printf("%d", b);
}
else {
printf("%d", c);
}
}
int a, b, c, d;
scanf("%d %d %d", &a, &b, &c);
if (a > b)
d = a;
else
d = b;
if (c > d)
d = c;
printf("%d", d); */
/* int a;
scanf("%d", &a);
if (a % 2==0) {
printf("n为偶数");
}
else {
printf("n为奇数");
} */
//printf("%c", '\x75');
/* int N = 1;
scanf("%d", &N);
if (N > 0) {
printf("positive");
}
else {
if (N == 0) {
printf("zero");
}
else {
printf("negative");
}
} */
/* char arr[] = {'#','#','#','\0'};
scanf("%c", arr);
printf("%s\n", arr);
printf("%s", arr); */
/* int a = 0;
while (a < 3) {
printf("%d ", a);
a++;
} */
//给定一个浮点数,要求得到该浮点数的个位数。给定一个浮点数,要求得到该浮点数的个位数。
/* float a;
scanf("%f", &a);
int b = abs(a);
b = b % 10;
printf("%d\n", b); */
// 三目操作符:有三个操作数
//条件操作符:(exp1?exp2:exp3)
// √ √ ×
// × × √
/* // 比较a,b↓
int a = 10, b = 20;
int r = (a > b ? a : b);
printf("%d\n", r); */
// 逗号表达式就是逗号隔开的一串表达式,特点是从左向右依次计算,整个表达式的结果是最后一个表达式的结果
/* int a = 10, b = 20, c = 0;
int d = (c = a - 2, a = b + c, c - 3);
// c=8 a=28 5
printf("%d\n", d); */
/* int Add(int x, int y) {
return x + y;
}
int main() {
int sum = Add(2, 3); // ()就是函数调用操作符,Add,2,3都是()的操作数
printf("%d\n", sum);
return 0;
} */
// 指针变量就是用来存放地址的
// int a = 10;
// int* p = &a; // *说明p是指针变量;int说明p指向的对象是int类型
/* //输出字符串的大小:
char arr[] = { "Hello,World!" };
printf("%d", sizeof(arr)); */
//输入1,2,3,4,5输出weekday;输入6,7输出weekend
/* int day = 0;
//while (day<=7) {
//day++;
scanf("%d", &day);
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
printf("%d:weekday\n", day);
break;
case 6:
case 7:
printf("%d:weekend\n", day);
break;
default:
printf("请输入正确的星期数(1--7)");
}
//} */
/* 题目:使用Switch语句编写一个模拟简单计算器的程序。依次输入两个整数和一个字符,并用空格隔开。
如果该字 符是一个“ + ”,则打印和;如果该字符是一个“ - ”,则打印差;
如果该字符是一个“ * ”, 则打印积;如果该字符是“ / ”,则打印商;如果该字符是一个 “ % ”,则打印余数。打印结果后输出一个空行。 */
/* int a, b;
char ch;
scanf("%d %d %c", &a, &b, &ch);
switch (ch) {
case '+':
printf("%d\n", a + b);
break;
case '-':
printf("%d\n", a - b);
break;
case'*':
printf("%d\n", a * b);
break;
case'/':
printf("%d\n", a / b);
break;
case'%':
printf("%d\n", a % b);
break;
} */
/* // 计算n的阶乘(n!)
int n = 1;
int f = 1;
scanf("%d", &n);
for (int i = 1; n >= i; i++) {
f *= i;
}
printf("%d", f); */
/* // 计算1!+2!+3!+...+n!
int n = 1;
int f = 1;
int sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
f *= i;
sum += f;
}
printf("%d", sum); */
/* // 计算2的n次方
int n = 0;
scanf("%d", &n);
int result = 1;
for (int i = 0; i < n; i++) {
result *= 2;
}
printf("%d", result); */
// 题目:在一个有序数组中查找具体的某个数字number
/* // 方法一:
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int number = 0,i=0;
int sz = sizeof(arr) / sizeof(arr[0]);
scanf("%d", &number);
for (int i = 0; i < sz; i++) {
if (arr[i] == number) {
printf("找到了,下标是:%d\n", i);
break;
}
}
if (i == sz) {
printf("找不到\n");
} */
/* //方法二:二分(折半)查找
int number = 0;
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int left = 0, right = sizeof(arr) / sizeof(arr[0])-1,mid;
scanf("%d", &number);
while (left <= right) {
mid = (left + right) / 2;
if (number == arr[mid]) {
break;
}
else if (number > arr[mid])
left = mid + 1;
else
right = mid - 1;
}
if (arr[mid] == number) {
printf("找到了,下标是:%d\n", mid);
}
else {
printf("Not Found!");
} */
/* // 输出小于N的偶数
int N = 0;
int i = 1;
scanf("%d", &N);
while (i < N) {
i++;
if (i % 2 == 0 && i != N) {
printf("%d\n", i);
}
}
int N = 0;
scanf("%d", &N);
for (int i = 1; i < N; i++) {
if (i % 2 == 0 && i != N) {
printf("%d\n", i);
}
} */
/* 题目:班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。
输入格式
第一行有一个整数n(1 <= n <= 100),表示学生的人数。其后n行每行有1个整数,表示每个学生的年龄,取值为15到25。
输出格式
输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。*/
/* int n, age;
float sum=0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &age);
sum += age;
}
printf("%.2f", sum / (float)n); // 因为题目要求输出浮点数,所以整型int n——>要强制转化为 (float)n */
// 将三个数从小到大输出
/* int a, b, c, t;
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
t = a;
a =b ;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
printf("%d %d %d", a, b, c); */
/* // 九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
printf("%d*%d=%d ", i, j, i * j);
}
printf("\n");
} */
/* // 打印直角三角形图案
int n;
while(scanf("%d",&n)!=EOF){
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
} */
/* // 打印翻转直角三角形图案
int n;
while (scanf("%d", &n) != EOF) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
} */
/* //冒泡排序
int arr[10] = { 3,4,2,7,0,1,8,5,9,6 };
printf("原数字:");
for (int i = 0; i <10; i++) {
printf("%d ", arr[i]);
}
for (int i = 0; i < 9; i++) {
for (int j =i+1; j <10; j++) {
if (arr[j] < arr[i]) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
printf("\n排序结果:");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
} */
/* // 进阶版冒泡排序
printf("你要排几个数字的顺序:");
int n;
scanf("%d", &n);
printf("请输入你要排序的这%d个数字:", n);
int arr[100];
for (int i = 0; i <n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n-1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
}
printf("\n这%d个数字冒泡排序的结果为: ", n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
} */
/* //选择排序法
int arr[10] = { 34, 12, 45, 7, 25, 98, 67, 43, 89, 2 };
int n = sizeof(arr) / sizeof(arr[0]); // 计算数组长度
int i, j, maxIndex, temp; // maxIndex用于记录每轮最大值的索引,temp用于交换元素
printf("原始数组: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 选择排序,降序
for (i = 0; i < n - 1; i++) {
maxIndex = i; // 假设当前索引i处的元素是最大值
for (j = i + 1; j < n; j++) {
// 找到剩余元素中的最大值的索引
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
// 将找到的最大值与当前位置的值交换
temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
printf("降序排序后的数组: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n"); */
/* //输出三角字符阵列(倒三角和正三角)
char ch = 'A';
for (int i = 4; i >=0; i--) {
for (int j =1; j <=i; j++) {
printf("%c ",ch);
ch++;
}
printf("\n");
}
char ch = 'j';
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", ch);
ch--;
}
printf("\n");
}
//打印前四十项斐波那契数列 {1,1,2,3,5,8……}
int f1 = 1, f2 = 1;
for (int i = 1; i <= 20; i++) {
printf("%10d %10d", f1, f2);
if (i % 2 == 0) {
printf("\n");
}
f1 += f2;
f2 += f1;
}*/
/* //爱心的简单打印
int i, j, k, n = 0, x = 0, y = 50;
//头部没有规律,所以直接打印
printf("\n\n\n\n\n");
printf(" lovelove lovelove\n");
printf(" lovelovelove lovelovelove\n");
printf(" lovelovelovelove lovelovelovelove\n");
printf(" lovelovelovelovelove lovelovelovelovelove\n");
printf(" lovelovelovelovelovelo lovelovelovelovelovelo\n");
printf(" lovelovelovelovelovelove lovelovelovelovelovelov\n");
for (i = 0; i < 2; i++)
{
printf("lovelovelovelovelovelovelovelovelovelovelovelovelove\n");
}
for (i = 0; i < 5; i++) //中间部分的上部分
{
y = 50;
y = y - i * 2;
n++;
for (k = 0; k < n; k++) //在每一行的起始位置先打印空格
{
printf(" ");
}
while (1) //空格后面打印love,但是要注意love即使没打印完,也要换行
{
if (x < y)
{
printf("l");
y--;
}
else
break;
if (x < y)
{
printf("o");
y--;
}
else
break; if (x < y)
{
printf("v");
y--;
}
else
break;
if (x < y)
{
printf("e");
y--;
}
else
break;
}
printf("\n");
}
//最下面的部分,具体内容同上,没和上一部分放一起是因为从这行开始多两个空格
for (i = 0, n = 3; i < 10; i++)
{
y = 37;
y = y - i * 4;
n++;
for (k = 0; k < n; k++)
{
printf(" ");
}
while (1)
{
if (x < y)
{
printf("l");
y--;
}
else
break;
if (x < y)
{
printf("o");
y--;
}
else
break;
if (x < y)
{
printf("v");
y--;
}
else
break;
if (x < y)
{
printf("e");
y--;
}
else
break;
}
printf("\n");
}
printf("\n\n\n"); */
/* //goto 语句
int number=0;
goto_add:
printf("goto语句测试\n");
printf("%d\n", number);
number++;
if (number < 2)
goto goto_add; */
/* //函数调用
int n;
scanf("%d", &n);
printf("add(%d) = %d\n",n, add(n));
return 0;
}
int add(int a) {
if (a == 1 || a == 0) {
return 1;
}
else {
return add(a - 1) * a; //递归调用
}
}*/
/* //输出100以内素数
int i, j;
for ( i = 2; i <=100; i++) {
for ( j = 2; j <=i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
printf("%d\n", i);
}
} */
/* //判断一个数是否为素数
int n, i;
scanf("%d", &n);
for ( i = 2; i <= n; i++) {
if (n % i == 0) {
break;
}
}
if (i < n) {
printf("no");
}
else{
printf("yes");
} */
/* //字符串的大小(strlen),比较(strcmp),合并(strcat)
int arr1[10];
int arr2[10];
gets(arr1);
gets(arr2);
char ret = strcmp(arr1, arr2);
strcat(arr1, arr2); //将arr2合并到arr1中,输出arr1即是arr1与arr2合并的字符串
printf("%d %d %s\n", strlen(arr1),strlen(arr2),arr1);
if (ret >= 0)
printf("arr1=arr2");*/
return 0;
}
初识C语言
最新推荐文章于 2024-09-28 22:05:59 发布