zoukankan      html  css  js  c++  java
  • 入门模拟

    • 简单模拟

      • PAT B1001 害死人不偿命的(3n+1)猜想 
        // time: 2018.01.03
        // title: PAT B1001 害死人不偿命的(3n+1)猜想  P85
        // idea: 利用循环直接模拟即可 
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            int m=0, n;                    // m 步数 
            scanf("%d", &n);
            while(n != 1) {
                if(n%2 == 0) {
                    n /= 2;                // 偶数 
                } else {
                    n = (3*n+1) / 2;    // 奇数 
                }
                m++;
            } 
            
            printf("%d", m);
            
            
            return 0;
        }
      • PAT B1032 挖掘机技术哪家强
        // time: 2018.1.3
        // title: PAT B1032 挖掘机技术哪家强  P86
        // idea: 用数组储存,依次比较,记录最大值以及相应学校编码 
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            int score[100000] = {0};
            int n, k, s;                // 记录数, 学校编号,分数 
            int max = 0, maxk = -1;            // 记录最大值和相应学校编码
            scanf("%d", &n);
            
            for(int i=0; i<n; ++i) {
                scanf("%d %d", &k, &s);
                score[k] += s;
                if(score[k] > max) {        // 记录最大值和相应编号 
                    max = score[k];
                    maxk = k;
                }
            } 
            
            printf("%d %d
        ", maxk, max);
        
            return 0;
        }
    • 查找元素

      • codeup 1934 找x
        // time: 2018.1.3
        // title: codeup 1934 找x 
        // idea: 用一个数组存储n个数,遍历查找x 
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        const int maxn = 201;
        int num[maxn] = {0}; 
        
        int main() {
            int n, x, k = -1;        // n 数值个数  x 查询键值  k 下标 
            scanf("%d", &n);
            for(int i=0; i<n; ++i) {    // 将输入数据储存在数组中 
                scanf("%d", num+i);
            } 
            scanf("%d", &x);
            for(int i=0; i<n; ++i) {
                if(num[i] == x) {
                    k = i;
                    break;
                }
            }  
            printf("%d
        ", k);
            
            return 0;
        }
    • 图形输出

      • PAT B1036 跟奥巴马一起编程  
        // time: 2018.1.3
        // title: PAT B1036 跟奥巴马一起编程 
        // idea: 当列数col为奇数时,行数row是col/2+1  当列数为偶数时,行数为col/2 
        //         第1行和第row行输出row个字符C
        //         其他行输出首尾字符C加col-2个空格 
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            int col, row;                // 行 列 
            char c;                        // 字符C 
            scanf("%d %c", &col, &c);
            
            if(col%2) {                    // 计算行数row 
                row = col/2+1;            //
            }  else {
                row = col/2;            //
            }
            for(int i=1; i<=row; ++i) {
                if(i==1 || i==row) {                // 输出第一行和最后一行 
                    for(int j=1; j<=col; ++j) {
                        printf("%c", c);
                    }
                } else {                            // 输出中间row-2行 
                    printf("%c", c);    
                    for(int j=1; j<=col-2;++j) {
                        printf(" ");
                    }
                    printf("%c", c);
                }
                printf("
        ");
            } 
            
        
            return 0;
        }
    • 日期处理

      • codeup 1928 日期差值 *
        /*
            codeup  1928 日期差值 书上代码 
            思路:简单模拟,在第一个日期没到第二个日期时循环 
        */
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int month[13][2] = {    // 平年 闰年 每月天数 
            {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, 
            {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}
        }; 
        
        bool isLeap(int year) {        // 判断是否是闰年 
            return ((year%4==0 && year%100!=0) || year%400==0); 
        } 
        
        int main() {
            int time1, y1, m1, d1;
            int time2, y2, m2, d2;
            
            while(scanf("%d%d", &time1, &time2) != EOF) {
                if(time1 > time2) {    // 若time1在time2之后,交换 
                    int temp = time1;
                    time1 = time2;
                    time2 = temp;
                }
                
                y1 = time1/10000, m1=time1%10000/100, d1=time1%100;
                y2 = time2/10000, m2=time2%10000/100, d2=time2%100; 
                int ans = 1;            // 记录结果 
                // 第一个日期没到第二个日期时进行循环
                while(y1<y2 || m1<m2 || d1<d2) {            
                     d1++;
                     if(d1 == month[m1][isLeap(y1)+1]) {
                         m1++;            // 日期变为下个月的1号 
                         d1 = 1;
                     } 
                     if(m1 == 13) {
                         y1++;            // 日期变为下一年一月 
                         m1 = 1;
                     } 
                     ans++;                // 累计 
                }
                
                printf("%d
        ", ans); 
            }
        
            return 0;
        }
    • 进制转换

      • 对一个P进制的数,如果转换为Q进制,需要分为两步:
        • 将P进制数x转换为十进制数y
        • 将十进制数y转换为Q进制数z
      • PAT B1022 D进制的A+B 
        /*
            PAT B1022 D进制的A+B 
            先计算A+B,再将结果转换为D进制数(除基取余法) 
        */
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            int a, b, d;
            scanf("%d%d%d", &a, &b, &d);
            
            int sum = a+b;
            int z[40], num=0;    // z储存d进制数的每一位 
            do {                    // 除基取余法
                z[num++] = sum%d;
                sum/=d;
            } while(sum!=0);
            for(int i=num-1; i>=0; --i) {    // 从后往前输出 
                printf("%d", z[i]); 
            } 
                 
            return 0;
        }
    • 字符串处理 *

      • codeup 5901 回文串
        /*
            codeup 5901 回文串 
            思路:遍历字符数组前半部分,比较该字符与其对称字符,若不同,则不是回文串 
        */
        
        #include <cstdio>
        #include <cstring>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            char s[256];            // 储存输入字符串 
            scanf("%s", &s);
            
            int i;
            // strlen函数获取字符数组长度 
            for(i=0; i<strlen(s)/2; ++i) {        // 遍历字符数组前半部分 
                if(s[i] != s[strlen(s)-1-i]) {
                    break;
                }
            } 
            if(i<strlen(s)/2) {            // 有字符不对称情况跳出循环,不是回文串 
                printf("NO
        ");
            } else {                    // 是回文串 
                printf("YES
        ");
            }
        
            return 0;
        }
      • PAT B1009 说反话  

        /*
            PAT B1009 说反话 
            思路:先按顺序读入字符串到字符数组中,再按逆序输出 
                **也可以先用gets把整行数据输入到字符数组,然后按字符遍历,遇到空格则为下一个字符 
        */
        
        #include <cstdio>
        #include <string>
        #include <algorithm>
        #include <cmath>
        using namespace std;
        
        int main() {
            char str[81][81];        // 储存字符串 
            int num = 0;            // 计数 
            
            while(scanf("%s", str[num]) != EOF) {
                num++;
            }
            for(int i=num-1; i>=0; --i) {
                printf("%s", str[i]);
                if(i>0)    printf(" ");
            } 
            
            return 0;
        }
  • 相关阅读:
    CC2431 代码分析②-CC2431狂轰滥炸
    CC2431 代码分析①-CC2431 喊出第一声
    【开源GPS追踪】 之 手机端安卓版
    【开源GPS追踪】 之 服务器硬伤
    【开源GPS追踪】 之 为何费力不讨好
    【开源GPS追踪】 之 硬件开源
    【开源GPS追踪】 之 服务器端opengts安装
    Microsoft Office Excel cannot access the file, There are several possible reasons
    Dynamics CRM IFD部署后延长系统注销时间
    Dynamics CRM2013 Server2012R2下部署ADFS和IFD遇到的问题
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/Algorithmofnotes02.html
Copyright © 2011-2022 走看看