zoukankan      html  css  js  c++  java
  • 找位置

    题目截图:

    思路:

      令 asc[i][j] 表示第 j 个 ascii 码为 i 的字符在字符串中的位置,其中 asc[i][0] 存储 ascii 码为 i 的字符在字符串中的个数。另外,由于要根据输入前后决定输出字符位置,需要设置 c[] 来存储字符第一次出现时的位置前后。最后,将个数大于 1 的字符按要求输出即可。

    代码如下:

     1 /*
     2     找位置 
     3 */
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <stdlib.h>
     9 #include <time.h>
    10 
    11 char str[101];            // 存储输入字符 
    12 char c[101];            // 存储字符第一次出现前后 
    13 int asc[256][101]; // asc[i][j] 表示第 j 个 ascii 码为 i 的字符在字符串中的位置
    14 
    15 int main() {
    16     while(scanf("%s", str) != EOF) {
    17         memset(asc, 0, sizeof(asc));        // 初始化 asc 数组 
    18         int i, j, k=0;
    19         for(i=0; i<strlen(str); ++i) {        // 遍历字符串的每个字符 
    20             if(asc[str[i]-''][0]==0) {    // 字符第一次出现 
    21                 c[k++] = str[i];
    22             }
    23             asc[str[i]-''][0]++;            // 字符出现次数 +1 
    24             // 记录字符位置 
    25             asc[str[i]-''][asc[str[i]-''][0]]=i;
    26         } 
    27         for(i=0; i<k; ++i) {                // 遍历每个不同字符 
    28             int ascii = c[i]-'';            // 求字符 ASCII 码 
    29             if(asc[ascii][0] > 1) {            // 若有重复
    30                 // 按格式输出 
    31                 for(j=1; j<=asc[ascii][0]; ++j) {
    32                     printf("%c:%d", c[i], asc[ascii][j]);
    33                     if(j < asc[ascii][0]) {
    34                         printf(","); 
    35                     }
    36                 }
    37                 printf("
    "); 
    38             }
    39         }
    40     } 
    41 
    42     return 0;
    43 }
  • 相关阅读:
    最近队伍训练记录20170926
    HDU5942 Just a Math Problem
    AC自动机+高斯消元 hdu 5955 Guessing the Dice Roll 16沈阳icpc
    [软件工程]软件工程的历史进程
    2017 Multi-University Training Contest
    A*B 原根+FFT优化
    莫比乌斯函数+莫比乌斯反演
    NTT板子 -- from ACdreamer -- test by HDU6061
    2017 Multi-University Training Contest
    将表单序列化成json对象
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest25.html
Copyright © 2011-2022 走看看