zoukankan      html  css  js  c++  java
  • 成长轨迹48 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2818:密码】

    题目http://poj.grids.cn/practice/2818

    注意:
    1、每一块之后有一个空行
    2、分析经过多少轮后循环进行一次
    注意每个位开始产生循环的轮数不一样
    生成和原来整串一模一样可能不行,但是每个位基本都经历了好几个不同循环
    所以分析循环要从各个位分析,而非整串分析

    //注意第几个位置和下标的区别,,,

    【 runtime error代码】

    View Code
     1 #include <stdio.h>
    2 #include <string.h>
    3 #include <ctype.h>
    4
    5 char buf[100],result[100];
    6 int key[250];
    7 int main()
    8 {
    9
    10 while(1)
    11 {
    12 int n;
    13 scanf("%d",&n);
    14 if(n==0)
    15 break;
    16
    17 for(int i=0;i<n;i++)
    18 {
    19 scanf("%d",&key[i]);
    20 }
    21
    22 while(1)
    23 {
    24 int k;
    25 scanf("%d",&k);
    26 if(k==0)
    27 break;
    28
    29 fgets(buf,sizeof(buf),stdin);
    30 int l = strlen(buf)-1;
    31 for(int i=0;i<k;i++)
    32 {
    33 for(int j=0;j<n+1;j++)
    34 result[j]=' ';
    35 for(int j=0;j<n+1;j++)
    36 {
    37 result[key[j]]=buf[j];
    38 }
    39 for(int j=0;j<n+1;j++)
    40 {
    41 buf[j]=result[j];
    42 }
    43 }
    44 printf("%s\n",buf);
    45 }
    46 }
    47 }

    //注意重复加密是会变成循环的

    【Time Limit Exceeded 代码】

    View Code
     1 #include <stdio.h>
    2 #include <string.h>
    3 #include <ctype.h>
    4
    5 int key[250];
    6 int main()
    7 {
    8 while(1)
    9 {
    10 int n;
    11 scanf("%d",&n);
    12 if(n==0)
    13 break;
    14
    15 for(int i=0;i<n;i++)
    16 {
    17 int t;
    18 scanf("%d",&t);
    19 key[i]=t-1;//【注意是第key[i]个位置】
    20 }
    21
    22 while(1)
    23 {
    24 int k;
    25 scanf("%d ",&k);
    26 if(k==0)
    27 break;
    28
    29 char buf[250],result[250];
    30 memset(buf,0,sizeof(buf));//【之前不知为何还是有残留】
    31 fgets(buf,sizeof(buf),stdin);
    32
    33 int l = strlen(buf);
    34 buf[l-1]='\0';//【\n在最后一位】
    35
    36 for(int i=0;i<k;i++)
    37 {
    38 for(int j=0;j<n;j++)
    39 result[j]=' ';
    40 for(int j=0;j<n;j++)
    41 result[key[j]]=buf[j];
    42 for(int j=0;j<n;j++)
    43 buf[j]=result[j];//【这样后面才可以重复进行加密】
    44 }
    45 for(int i=0;i<n;i++)
    46 printf("%c",result[i]);
    47 printf("\n");
    48 }
    49 }
    50 return 0;}

    //1、每一块之后有一个空行
    //2、分析经过多少轮后循环进行一次
    //注意每个位开始产生循环的轮数不一样
    //生成和原来整串一模一样可能不行,但是每个位基本都经历了好几个不同循环
    //所以分析循环要从各个位分析,而非整串分析
    //结果还是大改了一下代码,因为原来那种思路只能整串加密而不能逐位加密

    【ac代码】

    View Code
     1 #include <stdio.h>
    2 #include <string.h>
    3 #include <ctype.h>
    4
    5 int key[250],times[250];
    6 char str1[250],str2[250];
    7 int main()
    8 {
    9 int i,n,m,count;
    10 int tmp;
    11 char ch;
    12 while(scanf("%d",&n)!=EOF && n)
    13 {
    14 for(i=1;i<=n;i++) scanf("%d",&key[i]);
    15
    16 for(i=1;i<=n;i++)
    17 {
    18 tmp=i;
    19 count=0;
    20 do
    21 {
    22 tmp=key[tmp];
    23 count++;
    24 }while(tmp!=i);
    25 times[i]=count;
    26 }
    27 while(scanf("%d",&m)!=EOF && m)
    28 {
    29 getchar();
    30 i=1;
    31 while((ch=getchar()) != '\n')str1[i++] = ch;
    32 while(i<=n) str1[i++]=' ';
    33 for(i=1;i<=n;i++)
    34 {
    35 count = m % times[i];
    36 tmp = i;
    37 while(count--) tmp=key[tmp];
    38 str2[tmp] = str1[i];
    39 }
    40 str2[n+1]='\0';//【\n在最后一位】
    41
    42 printf("%s\n",str2+1);
    43 }
    44 printf("\n");
    45 }
    46 return 0;
    47 }
  • 相关阅读:
    Oracle(创建序列)
    BoostrapValidator使用方法
    SpringBoot(八)----SpringBoot配置日志文件
    SpringBoot(七)-----查看配置报告
    eclipse导入新项目后,运行时找不到主类解决办法(转载)
    严重性代码说明项目文件行 禁止显示状态错误 C4996 fopen('fscanf'、strcmp):This function or variable may be unsafe. 最全解决办法
    Spring Boot(六)----application.properties文件加载顺序
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    异常处理(一):Non-parseable POM C:Usersadmin.m2 epositoryorgspringframework问题解决方案
    Spring Boot(五)----profile文件路径设置
  • 原文地址:https://www.cnblogs.com/zeedmood/p/2352416.html
Copyright © 2011-2022 走看看