zoukankan      html  css  js  c++  java
  • C算法--入门 2.1

     1 #include <stdio.h>
     2 
     3 
     4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     5 /*输出数据1 换行2 输出数据2 ;计算机响8下*/
     6 int main() {
     7     int num1=1,num2=2;
     8     printf("%d
    
    %d",num1,num2); 
     9     printf("%c",7);
    10     printf("%c",7);
    11     printf("%c",7);
    12     printf("%c",7);    
    13     printf("%c",7);
    14     printf("%c",7);
    15     printf("%c",7);
    16     printf("%c",7);
    17     return 0;
    18 }
    转义字符
     1 #include <stdio.h>
     2 
     3 
     4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     5 /*输出数据1 换行2 输出数据2 ;计算机响8下*/
     6 int main(){
     7     char str1[25]="h e l l o";
     8     char str2[25]="happy birthday !";
     9     printf("%s,%s",str1,str2);
    10     return 0;
    11 } 
    字符数组
    #include <stdbool.h>
    /*若不写#include <stdbool.h> 需要用.cpp文件运行*/
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(){
        bool flag1=0,flag2=true;
        int a=1,b=1;
        printf("%d %d %d
    ",flag1,flag2,a==b);
        return 0;
    } 
    布尔型
     1 #include <stdio.h>
     2 
     3 
     4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     5 
     6 int main(){
     7     double r=12.56;
     8     int a =3, b=5;
     9     printf("%d
    ",(int)r);
    10     printf("%d
    ",a/b);
    11     printf("%.2f",(double)a/(double)b);
    12     /*以两位小数输出*/
    13     return 0;
    14 } 
    强制类型转换
     1 #include <stdio.h>
     2 #define pi 3.14
     3 
     4 /*define 标识符 常量*/ 
     5 int main(){
     6     double r=3;
     7     printf("%f
    ",pi*r*r);
     8     return 0;
     9     
    10 } 
    符号常量
     1 #include <stdio.h>
     2 
     3 const double  pi=3.14;
     4     
     5 /*const 标识符 常量*/ 
     6 int main(){
     7     double r=3;
     8     printf("%f
    ",2*pi*r);
     9     return 0;
    10 }
    const常量
     1 #include <stdio.h>
     2 #define ADD(a,b) ((a)+(b)) 
     3 
     4 /*define 标识符 任何语句或片段*/
     5 /*#define ADD(a,b) ((a)+(b)) */ 
     6 int main(){
     7     int num1=3,num2=5;
     8     printf("%d",ADD(num1,num2));
     9     return 0;
    10 }
    define定义语句片段

    注:宏定义直接将对应部分替换

    在scanf中除了 char数组整个输入的情况不加&之外,其他变量类型都需要加&(无需特地记忆,都加)

    #include <stdio.h>
    #define ADD(a,b) ((a)+(b)) 
    
    /*123不足五位 前面自动用两个空格填充,凑足五位;1234567大于五位 直接输出*/
    int main(){
        int a=123,b=1234567;
        printf("%5d
    ",a);
        printf("%5d
    ",b);
        return 0;
    }
    %md右对齐输出
    1 #include <stdio.h>
    2 
    3 /*123用空格补齐*/
    4 int main(){
    5     int a =123,b=1234567;
    6     printf("%05d
    ",a);
    7     printf("%05d
    ",b);
    8     return 0; 
    9 }
    用空格补齐
     1 #include <stdio.h>
     2 
     3 /*浮点数保留到小数点后几位*/
     4 int main() {
     5     double d1=12.3456;
     6     printf("%.0f
    ",d1);
     7     printf("%.1f
    ",d1);
     8     printf("%.2f
    ",d1);
     9     printf("%.3f
    ",d1);
    10     printf("%.4f
    ",d1);
    11     return 0;
    12 }
    保留到小数点后几位
     1 #include <stdio.h>
     2 
     3 /*输入的第二个字符被接受但未被存储*/
     4 int main() {
     5     char c1,c2,c3;
     6     c1=getchar();
     7     getchar();
     8     c2=getchar();
     9     c3=getchar();
    10     putchar(c1);
    11     putchar(c2);
    12     putchar(c3);
    13     return 0;
    14 }
    getchar/putchar输入输出字符
    1 #include <stdio.h>
    2 
    3 /*给long long 起个别名LL*/
    4 typedef long long LL;
    5 int main(){
    6     LL a =123456789012345, b=234567890123456;
    7     printf("%lld
    ",a+b);
    8     return 0;
    9 } 
    typedef定义别名
  • 相关阅读:
    Apache HTTP Server 与 Tomcat 的三种连接方式介绍
    Java使用Memcached
    缓存系统MemCached的Java客户端优化历程
    在Java中使用Memcached(转)
    memcached简介及java使用方法
    JS中冒泡排序,选择排序,快速排序
    DOM的查找,新增,删除操作
    JS中文档碎片的理解和使用
    JS中undefined和null的区别,以及出现原因
    JS中的数学方法
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/11131129.html
Copyright © 2011-2022 走看看