运算符/printf/scanf/if

本文深入探讨了C语言的关键概念,包括运算符、printf、scanf、if语句、内存地址、关系运算符、sizeof函数、取模运算、循环控制结构等。通过实例分析,为读者提供了全面的理解和应用指南。

运算符/printf/scanf/if

  • 定义标示符,采用”驼峰规范”:例如
  void sendMessageWithNumber()
  • 实型:

    • 10.1//C语言默认是双精度double型数据;
    • 10.1f//末尾加f表示单精float型数据;
    • float类型有效位数是7位(不包括小数点);
    • double类型有效位数是15位(不包括小数点);
  • 字符常量的特殊情况:’\n’ ……… 在C语言中,系统给某些字符的组合定义了特殊的含义,这些字符的组合可以当做单个字符来使用;

  • 变量的作用域 局部变量和全局变量
    • 变量的作用域从定义的那一行开始,直到大括号结束或者遇到return或}为止.例如:
 int number; // 变量的作用域从定义开始
 number = 10;
 printf("return之前");
 return 0;
 }
  • 内存地址从大到小, 取地址:&变量名;
  • a++与++a,无论++在前还是在后, 最终都会自增一次:
    • 如果++写在变量的前面,那么会先将变量自增再用自增之后的结果参与运算
    • 如果++写在变量的后面,那么会先将变量的值参与运算再将变量自增
    • 总结一句话:++在前,先自增再运算,++在后,先运算再自增;
  • 关系运算符
    注意: > < >= <= 优先级大于 == !=
  • sizeof()
    • sizeof的计算格式:
      sizeof(变量/常量/数据类型);
    • sizeof可以用来计算一个变量或一个常量、一种数据类型所占的内存字节数;
    • 注意: sizeof是一个运算符, 不是一个函数;
    • 注意: 如果利用sizeof计算数据类型, ()不能省略;例如:
  int number = sizeof double;//错误
  int number = sizeof (double);//正确
  • 采用占位符%p打印详细地址,例如:
   printf("%p\n",&value);
  • printf指定位宽输出:
  printf("%mi", number);//其中m就是位宽,而i用于输出整型数据
  printf("%2i", number);
  • 指定保留多少位小数输出: %.nf, 其中n就是需要保留多少位小数, f用于输出实型;
  float floatValue = 3.1415926;
  printf("%.2f", floatValue);
  // printf("%.*f", 5,floatValue);
  return 0;
  • scanf存放数据到详细地址;例如:
   scanf("%i",&number);
  • 在scanf中,尽量不要使用 \n;如果在scanf末尾中使用了\n, 需要用户在输入末尾原样输入”\n”来破;
  • 取模运算的正负性取决于左边的操作数
  • 在多层循环中,一个break语句只向外跳一层;break不是跳出if语句而是跳出整个循环;而continue语句的作用是跳过循环体中剩余的语句而继续下一次.
  • if:如果需要判断某一个变量是否等于某一个值,建议把常量写前面, 变量写后面;例如:
 if (8 == number) {
    printf("********\n");
 }
  • 分析:
int main(){
   int a=1,b=2,c,d,e;
   c=(-a++)+(++b);
   d=(b--)+(++a)-a;
   e=(a/(++b))-(a/(--a));
   return 0;
 }
 //结果:a = 2, b = 3, c = 2, d = 3, e = 0;
#include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <limits.h> #define eps 1e-9 //unsigned int F[14000010]; //unsigned long long arr[7500005]; //double arr1[1000000]; //double arr2[1000000]; //double Arr[1000000]; //int arr[1000010] = { 0 }; //int arrn[10010] = { 0 }; //int arr[100][100]; //E3H! //int is_ry(int y) //{ // int result; // result = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); // return result; //} //int D(int y, int m) //{ // int D[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // int ds = D[m]; // if (m == 2 && is_ry(y)) // { // D[2] = 29; // return 29; // } // return ds; //} //E3M? int Max(int a, int b) { int result; if (a >= b) { result = a; } else { result = b; } return result; } int Min(int a, int b) { int result; if (a <= b) { result = a; } else { result = b; } return result; } int main() { //C3A //long long T; //scanf("%lld", &T); //unsigned int a, b; //for (int i = 0; i < T; i++) //{ // scanf("%u %u", &a, &b); // unsigned int n1 = ~a; // unsigned int n2 = a & b; // unsigned int n3 = a | b; // unsigned int n4 = a ^ b; // unsigned int n5 = a << b; // unsigned int n6 = a >> b; // printf("%u %u %u %u %u %u\n", n1, n2, n3, n4, n5, n6); //} //return 0; //C3B //long long n; //scanf_s("%lld", &n); //printf("0 "); //for (int k = 1; k <= n; k++) //{ // int sum = 0; // int arr[101]; // int f = floor(log2(k)); // int K = k; // for (int i = 0; i <= f; i++) // { // int N = K & 1; // arr[i] = N; // K = K >> 1; // } // for (int j = 0; j <= f; j++) // { // if (arr[j] == 1) // { // sum++; // } // } // printf("%d ", sum); //} //return 0; //C3C //int nn; //scanf_s("%d", &nn); //int n; //for (int i = 0; i < nn; i++) //{ // int arr[101] = {0}; // unsigned long long n; // scanf_s("%llu", &n); // unsigned long long f = floor(log2(n)); // unsigned long long N = n; // for (int i = 0; i <= f; i++) // { // unsigned long long N = n & 1; // arr[i] = N; // n = n >> 1; // } // for (int j = 63; j >= 0; j=j-4) // { // printf("%d%d%d%d ", arr[j],arr[j-1],arr[j-2],arr[j-3]); // } // printf("\n"); //} //return 0; //C3D //int n; //unsigned int sum=0; //scanf_s("%d", &n); //for (int i = 0; i < n; i++) //{ // sum = 0; // int arr[101] = {0}; // unsigned int n; // scanf_s("%u", &n); // int f = floor(log2(n)); // unsigned int N = n; // for (int i = 0; i <= f; i++) // { // unsigned int N = n & 1; // arr[i] = N; // n = n >> 1; // } // for (int j = 0; j <= 32; j++) // { // sum = sum + pow(2, j) * arr[31-j]; // } // printf("%u\n", sum); //} //C3E //int nn; //scanf_s("%d", &nn); //for (int i = 0; i < nn; i++) //{ // int arr[101]; // int n; // scanf_s("%d", &n); // int f = floor(log2(n)); // int N = n; // for (int i = 0; i <= f; i++) // { // int N = n & 1; // arr[i] = N; // n = n >> 1; // } // int k = 0; // int Arr[101]; // for (int j = f; j >= 0; j--) // { // if (arr[j] == 1) // { // Arr[k] = j; // k++; // } // } // for (int t = 0; t < k; t++) // { // printf("%.0lf ", pow(2, Arr[t])); // } // printf("\n"); //} //return 0; //C3F //int n, q; //scanf_s("%d%u%d", &n, &F[0], &q); //int k; //for (int i = 1; i <= n; i++) //{ // unsigned int x = F[i - 1]; // x ^= x << 13; // x ^= x >> 17; // x ^= x << 5; // F[i] = x; //} //while (q--) //{ // scanf_s("%d", &k); // printf("%u\n", F[k]); //} //return 0; //C3G //int n; //int a; //int b=0; //scanf_s("%d", &n); //for (int i = 0; i < n; i++) //{ // scanf_s("%d", &a); // b = b ^ a; //} //printf("%d\n", b); //return 0; //E3A //int a, b, c, x; //scanf_s("%d%d%d%d", &a, &b, &c, &x); //int A = a * x * x + b * x + c; //printf("%d", A); //return 0; //E3B //int n; //scanf_s("%d", &n); //int i = n; //for (int i = n; i > 1; i--) //{ // printf("%d bottles of beer on the wall,\n", i); // printf("%d bottles of beer.\n", i); // printf("Take one down and pass it around,\n"); // if (i == 2) // { // printf("now there's %d more bottle of beer on the wall!\n", i-1); // } // else // printf("now there's %d more bottles of beer on the wall!\n", i - 1); //} // printf("1 bottle of beer on the wall,\n"); // printf("1 bottle of beer.\n"); // printf("Take one down and pass it around,\n"); // printf("there's no more bottles of beer on the wall!\n"); //return 0; //E3C //unsigned long long a, b; //while (scanf_s("%llu%llu", &a, &b) != EOF) //{ // unsigned long long i = 0; // unsigned long long t = 0; // for (i = 0; i < 64; i++) // { // unsigned long long ai = a >> i; // unsigned long long bi = b >> i; // unsigned long long Ai = ai & 1; // unsigned long long Bi = bi & 1; // if (Ai - Bi == 1) // { // t = t | (1ULL << i); // } // } // printf("%llu\n", t); //} //return 0; //E3D //int T, n; //scanf_s("%d", &T); //arr[1] = 1; //unsigned long long temp=1; //for (int j = 2; j <= 7.5e6; j++) //{ // temp = (temp * j) % 998244353; // arr[j] = temp; //} //for (int i = 1; i <= T; i++) //{ // scanf_s("%d", &n); // printf("%llu\n", arr[n]); //} //return 0; //E3E //int T; //scanf_s("%d", &T); //double sum1 = 0.000000; //double sum2 = 0.000000; //double sign = 1.0; //for (int i = 1; i <= 100000; i++) //{ // sum1 = sum1 + sign *1.000000/ (2 * i - 1); // sign = -sign; // arr1[i] = sum1; //} //for (int i = 1; i <= 100000; i++) //{ // sum2 = sum2 + 1.000000 / (long long)((long long)(2 * i - 1) * (long long)(2 * i - 1)); // arr2[i] = sum2; //} //for (int i = 1; i <= 100000; i++) //{ // Arr[i] = arr1[i] * 4 - sqrt(8 * arr2[i]); //} //int arrn[1000]; //int arrN[1000]; //for (int j = 1; j <= T; j++) //{ // scanf_s("%d", &arrn[j]); // arrN[j] = arrn[j]; //} //for (int k = 1; k <= T; k++) //{ // printf("%.6lf\n", fabs(Arr[arrN[k]])); //} //return 0; //E3F? //int n; //int a; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d", &a); // arrn[i] = a; //} //for (int j = 1; j <= 10000; j++) //{ // for (int k = 1; k <= 1000000; k++) // { // if (j == arrn[k]) // { // arr[j]++; // } // } // if (arr[j] != 0) // { // printf("%d : %d\n", j, arr[j]); // } //} //return 0; //E3F //int n; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d",&arr[i]); // arrn[arr[i]]++; //} //for (int i = 1; i <= 10000; i++) //{ // if (arrn[i] > 0) // { // printf("%d : %d\n", i, arrn[i]); // } //} //E3G? //int m, n; //int x0, y0; //int x, y; //scanf_s("%d%d", &n, &m); //scanf_s("%d%d", &x0, &y0); //scanf_s("%d%d", &x, &y); //for (int xi = 1; xi <= n; xi++) //{ // for (int yi = 1; yi <= m; yi++) // { // printf("0"); // } // printf("0\n"); //} //E3G! //long long n, m; //long long x0, y0; //long long x, y; //scanf_s("%lld%lld", &n,& m); //scanf_s("%lld%lld", &x0, &y0); //scanf_s("%lld%lld", &x, &y); //for (long long i = x - 2; i <= x + 2; i++) //{ // for (long long j = y - 2; j <= y + 2; j++) // { // if (i == x0 && j == y0) // { // printf("+"); // } // else if (i == x0 && j != y0) // { // printf("-"); // } // else if (i != x0 && j == y0) // { // printf("|"); // } // else if (i - x0 == j - y0) // { // printf("\\"); // } // else if (i - x0 == y0 - j) // { // printf("/"); // } // else if (i - x0 < 0 && j - y0>0 && x0 - i > j - y0) // { // printf("A"); // } // else if (i < x0 && j > y0 && x0 - i < j - y0) // { // printf("B"); // } // else if (i > x0 && j > y0 && i - x0 < j - y0) // { // printf("C"); // } // else if (i > x0 && j > y0 && i - x0 > j - y0) // { // printf("D"); // } // else if (i > x0 && j < y0 && i - x0 > y0 - j) // { // printf("E"); // } // else if (i > x0 && j < y0 && i - x0 < y0 - j) // { // printf("F"); // } // else if (i < x0 && j < y0 && i - x0 < j - y0) // { // printf("H"); // } // else // { // printf("G"); // } // } // printf("\n"); //} //return 0; //E3H? //int n; //scanf_s("%d", &n); //int y, m, d, h, mi; //for (int i = 1; i <= n; i++) //{ // scanf_s("%d.%d.%d %d:%d", &y, &m, &d, &h, &mi); //} //E3H! //int n; //scanf_s("%d", &n); //while (n--) //{ // int y, m, d, h, mi; // scanf_s("%d.%d.%d %d:%d", &y, &m, &d, &h, &mi); // while (mi >= 60) // { // mi = mi - 60; // h++; // } // while (h >= 24) // { // h = h - 24; // d++; // } // while (1) // { // while (m > 12) // { // m = m - 12; // y++; // } // int DD = D(y, m); // if (d <= DD) // { // break; // } // d = d - DD; // m++; // } // if (y >= 10000) // { // printf("%05d.%02d.%02d %02d:%02d\n", y, m, d, h, mi); // } // else // { // printf("%04d.%02d.%02d %02d:%02d\n", y, m, d, h, mi); // } //} //return 0; //E3I? //long long a, b, n; //scanf_s("%lld%lld%lld", &a, &b, &n); //long long ZHENG = a / b; //long long XIAO = a % b; //long long arr[110] = {0}; //for (int i = 1; i <= n + 1; i++) //{ // XIAO = XIAO * 10; // long long A = XIAO / b; // XIAO = XIAO % b; // arr[i] = A; //} //if (arr[n + 1] >= 5) //{ // arr[n]++; // for (int i = n; i >= 2; i--) // { // if (arr[i] >= 10) // { // arr[i] = arr[i] - 10; // arr[i - 1]++; // } // } // if (arr[1] >= 10) // { // arr[1] = arr[1] - 10; // ZHENG++; // } //} //printf("%lld.", ZHENG); //for (int i = 1; i <= n; i++) //{ // printf("%lld", arr[i]); //} //printf("\n"); //return 0; //E3K //int a, b; //scanf_s("%d%d", &a, &b); //double down= floor(a * 1.000000 / b); //double up = ceil(a * 1.000000 / b); //if (down == 0.0) //{ // down = 0.0; //} //if (up == 0.0) //{ // up = 0.0; //} //printf("%.lf %.lf", down, up); //return 0; //E3L? //int T; //int a; //scanf_s("%d", &T); //for (int i = 1; i <= T; i++) //{ // scanf_s("%d", &a); // if (a % 6 == 0) // { // printf("Tom wins\n"); // } // else // { // printf("Jerry wins\n"); // } //} //return 0; //E3M? int T; scanf_s("%d", &T); int a1, b1, c1, d1, a2, b2, c2, d2; for (int i = 1; i <= T; i++) { scanf_s("%d%d%d%d%d%d%d%d", &a1, &b1, &c1, &d1, &a2, &b2, &c2, &d2); float X1 = (a1 + c1) * 1.0f / 2; float X2 = (a2 + c2) * 1.0f / 2; float Y1 = (d1 + b1) * 1.0f / 2; float Y2 = (d2 + b2) * 1.0f / 2; float rx1 = fabs(a1 - c1) * 1.0f / 2; float rx2 = fabs(a2 - c2) * 1.0f / 2; float ry1 = fabs(d1 - b1) * 1.0f / 2; float ry2 = fabs(d2 - b2) * 1.0f / 2; float dX = fabs(X1 - X2)*1.0f; float dY = fabs(Y1 - Y2)*1.0f; float S1 = 4 * rx1 * ry1*1.0f; float S2 = 4 * rx2 * ry2*1.0f; float SS = S1 + S2; float TT = fabs(Max(a1, a2) - Min(c1, c2)) * fabs(Min(d1, d2) - Max(b1, b2)); if (dX < rx1 + rx2 && dY < ry1 + ry2) { printf("%.f %.f\n", TT, SS - TT); } else { printf("%.f %.f\n", 0, SS); } } return 0; //E3N? //int T; //int a, b, c; //int x1, x2, y1, y2; //double k, m; //double delta; //double X1, X2, Y1, Y2; //scanf_s("%d", &T); //for (int i = 1; i <= T; i++) //{ // scanf_s("%d%d%d", &a, &b, &c); // scanf_s("%d%d", &x1, &y1); // scanf_s("%d%d", &x2, &y2); // if (x1 != x2) // { // k = (y1 - y2) * 1.0 / (x1 - x2); // m = -(y1 - y2) * 1.0 / (x1 - x2) * x1 + y1; // } // if (a != 0 && x1 != x2) // { // delta = (b - k) * (b - k) - 4 * a * (c - m); // if (delta > eps) // { // X1 = ((k - b) + sqrt(delta)) * 1.0 / (2 * a); // X2 = ((k - b) - sqrt(delta)) * 1.0 / (2 * a); // Y1 = a * X1 * X1 + b * X1 + c; // Y2 = a * X2 * X2 + b * X2 + c; // printf("2 "); // if (X1 < X2) // { // printf("(%.2lf,%.2lf) (%.2lf,%.2lf)\n", X1, Y1, X2, Y2); // } // else // { // printf("(%.2lf,%.2lf) (%.2lf,%.2lf)\n", X2, Y2, X1, Y1); // } // } // if (fabs(delta) <= eps) // { // X1 = ((k - b) + sqrt(delta)) * 1.0 / (2 * a); // Y1 = a * X1 * X1 + b * X1 + c; // printf("1 "); // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // if (delta < -eps) // { // printf("0\n"); // } // } // else if (a != 0 && x1 == x2) // { // printf("1 "); // X1 = x1; // Y1 = a * x1 * x1 + b * x1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b != 0 && x1 == x2) // { // printf("1 "); // X1 = x1; // Y1 = b * x1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b != 0 && x1 != x2) // { // if (k == b && c == m) // { // printf("-1\n"); // } // else if (k == b && c != m) // { // printf("0\n"); // } // else // { // printf("1 "); // X1 = (c - m) * 1.0 / (k - b); // Y1 = b * X1 + c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // } // else if (a == 0 && b == 0 && x1 == x2) // { // printf("1 "); // printf("(%.2lf,%.2lf)\n", x1, c); // } // else if (a == 0 && b == 0 && y1 != y2 && x1 != x2) // { // printf("1 "); // X1 = (c - m) * 1.0 / (k); // Y1 = c; // printf("(%.2lf,%.2lf)\n", X1, Y1); // } // else if (a == 0 && b == 0 && y1 == y2 && y1 == c) // { // printf("-1\n"); // } // else if (a == 0 && b == 0 && y1 == y2 && y1 != c) // { // printf("0\n"); // } //} //return 0; //E3Y //int n; //int arr[22]; //scanf_s("%d", &n); //for (int i = 1; i <= n; i++) //{ // scanf_s("%d", &arr[i]); //} //int max = 0; //int Max = 0; //int min = 100; //int Min = 0; //for (int i = 1; i <= n; i++) //{ // if (arr[i] > max) // { // max = arr[i]; // Max = i; // } // if (arr[i] < min) // { // min = arr[i]; // Min = i; // } //} //arr[Min] = 0; //arr[Max] = 0; //double sum = 0.0; //double ave = 0.0; //for (int i = 1; i <= n; i++) //{ // sum = sum + arr[i]; //} //ave= sum / (n - 2); //int cut = ave * 100; //double decut = cut * 1.00 / 100; //printf("%.2lf", decut); //return 0; }帮我从WA改到AC,不要改变我的变量名称,不要有逻辑变动
10-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值