1.开灯问题

有n盏灯,编号为1~n,第一个人把所有灯打开,第二个按下所有编号为2的倍数的开关(这些灯都被关掉),第三个人按下所有编号为3的倍数的开关,依次类推,一共有k个人,问最后有哪些灯开着?输入n和k,输出开着的灯的编号?

  1. #include<stdio.h>
  2. #include<string.h>
  3. int a[1000]; //我们用数组来保存灯的状态
  4. int main(void)
  5. {
  6. int n,k,i,j;
  7. int first=1;//最后的格式输出
  8. memset(a,0,sizeof(a)); //把灯先设置为0,表示灯是关的
  9. //然后模拟所有的关灯和开灯的操作
  10. scanf("%d%d",&n,&k); //输入人数和灯数
  11. for(i=1;i<=k;i++)
  12. {
  13. for(j=1;j<=n;j++)
  14. {
  15. if(j%i==0) a[j]=!a[j]; //如果满足条件,按动开关
  16. }
  17. }
  18. for(j=1;j<=n;j++)
  19. {
  20. if(a[j]==1)
  21. {
  22. if(first) first=0;
  23. else printf(" ");
  24. printf("%d",j);
  25. }
  26. }
  27. printf("\n");
  28. return 0;
  29. }

2.蛇形填数

在n*n的方阵填入1,2,...,n*n,要求填成蛇形,例如n=4

10  11 12 1

9   16 13  2

8   15 14  3

7   6   5     4

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 10
  4. int a[MAX][MAX];
  5. int main(void)
  6. {
  7. int x,y,n,tot;
  8. scanf("%d",&n); `
  9. memset(a,0,sizeof(a));
  10. tot=a[x=0][y=n-1]=1; //确定起点
  11. while(tot<n*n) //判断有没有越界,注意下一个是不是0,还有如果x+1<n为假,则就不计算后面的,因为&&短路预算
  12. {
  13. while(x+1<n && !a[x+1][y]) a[++x][y]=++tot;
  14. while(y-1>=0 && !a[x][y-1]) a[x][--y]=++tot;
  15. while(x-1>=0 && !a[x-1][y]) a[--x][y]=++tot;
  16. while(y+1<n && !a[x][y+1]) a[x][++y]=++tot;
  17. }
  18. for(x=0;x<n;x++)
  19. {
  20. for(y=0;y<n;y++)
  21. printf("%3d",a[x][y]);
  22. printf("\n");
  23. }
  24. return 0;
  25. }

3.竖式问题

 找出所有形如abc*de的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合,输入数字集合,输出所有竖式.

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(void)
  4. {
  5. char s[20],buf[100];
  6. int abc,de,x,y,z,ok;
  7. int num=0;
  8. scanf("%s",s);
  9. for(abc=111;abc<=999;abc++)
  10. {
  11. for(de=11;de<=99;de++)
  12. {
  13. x=abc*(de%10);y=abc*(de/10);z=abc*de;
  14. sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z); //写入字符串buf中
  15. ok=1;
  16. for(int i=0;i<strlen(buf);i++) //比较buf和s中的字符是不是都有
  17. {
  18. if(strchr(s,buf[i])==NULL) ok=0;
  19. }
  20. if(ok) //成功的标志
  21. {
  22. printf("<%d>\n",++num); //计数
  23. printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);
  24. }
  25. }
  26. }
  27. printf("The number of solutions =%d\n",num);
  28. return 0;
  29. }

4.最长回文子串

输入一个字符串,求出其中最长的回文子串.

  1. //分析,首先不适用使用scanf来输入字符串,因为碰到空格或者TAB就会停下
  2. //于是我们可以使用fgets
  3. //先找最大回文子串的长度
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<ctype.h>
  7. #define MAX 5000
  8. char buf[MAX],s[MAX];
  9. int main(void)
  10. {
  11. int n,m=0,max=0;
  12. int i,j,k;
  13. fgets(buf,sizeof(buf),stdin); //输入字符串
  14. n=strlen(buf); //求字符串长度
  15. for(i=0;i<n;i++)
  16. if(isalpha(buf[i])) s[m++]=toupper(buf[i]); //全部变成大写,方便判别
  17. for(i=0;i<n;i++)
  18. {
  19. for(j=0;j<m;j++)
  20. {
  21. int ok=1;
  22. for(k=i;k<=j;k++) //s[k]的对称位置是s[i+j-k]
  23. {
  24. if(s[k]!=s[i+j-k]) ok=0;
  25. }
  26. if(ok&&j-i+1>max) max=j-i+1;
  27. }
  28. }
  29. printf("max=%d\n",max);
  30. return 0;
  31. }
  32.  
  33. #include<stdio.h>
  34. #include<string.h>
  35. #include<ctype.h>
  36. #define MAX 5000
  37. char buf[MAX],s[MAX];
  38. int p[MAX];
  39. int main(void)
  40. {
  41. int n,m=0,max=0;
  42. int x,y,i,j;
  43. fgets(buf,sizeof(buf),stdin);
  44. n=strlen(buf);
  45. for(i=0;i<n;i++)
  46. {
  47. if(isalpha(buf[i]))
  48. {
  49. p[m]=i;
  50. s[m++]=toupper(buf[i]);
  51. }
  52. }
  53. for(i=0;i<m;i++)
  54. {
  55. for(j=0;i-j>=0&&i+j<m;j++) //从内部向外分开,奇数情况,aba
  56. {
  57. if(s[i-j]!=s[i+j]) break;
  58. if(j*2+1>max) {
  59. max=j*2+1;
  60. x=p[i-j];
  61. y=p[i+j];
  62. }
  63. }
  64. for(j=0;i-j>=0&&i+j+1<m;i++) //从内部向外分开,偶数情况,考虑aabb
  65. {
  66. if(s[i-j]!=s[i+j+1]) break;
  67. if(j*2+2>max) {
  68. max=j*2+2;
  69. x=p[i-j];
  70. y=p[i+j+1];
  71. }
  72. }
  73. }
  74. for(i=x;i<=y;i++)
  75. printf("%c",buf[i]);
  76. printf("\n");
  77. return 0;
  78. }

习题

分数统计

任务1 分数为不吵为100的非负整数

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(void)
  4. {
  5. int num[110];
  6. int x;
  7. int ans;
  8. int max=0;
  9. memset(num,0,sizeof(num));
  10. while(scanf("%d",&x)==1)
  11. {
  12. num[x]++;
  13. }
  14. for(int i=0;i<=100;i++)
  15. {
  16. if(num[i]>=max)
  17. {
  18. max=num[i];
  19. ans=i;
  20. }
  21. }
  22. for(int i=0;i<101;i++)
  23. {
  24. if(num[i]==max)
  25. printf("%d ",i);
  26. }
  27. printf("\n");
  28. return 0;
  29. }

  任务2 输入为不超过100的非负实数

  1. //习题3.1,分数统计(stat)
  2. #define LOCAL
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #ifndef MAX
  7. #define MAX 10000+1
  8. #endif
  9. int a[MAX];
  10. int main()
    {
  11. //从本地读取文件(重定向),不用每次都进行数据输入
  12. #ifdef LOCAL
  13. freopen("data.txt","r",stdin);
  14. #endif
  15. memset(a,0,sizeof(a));
  16. double degree;
  17. while(scanf("%lf",&degree) == 1){
  18. //直接double强制转化为int会出现问题,如4.9999999999,应为5,但会是4.9
  19. //使用floor进行四舍五入可以解决这个问题
  20. double m = degree * 100;
  21. int n = floor(m+0.5);
  22. a[n] += 1;
  23. }
  24.  
  25. int i,max = a[0];
  26. int tmp[MAX];
  27. memset(tmp,0,sizeof(tmp));
  28. for(i=1; i <= MAX; i++){
  29. if(a[i] > max){
  30. max = a[i];
  31. }
  32. }
  33. int j = 0;
  34. for(i = 0; i < MAX; i++){
  35. if(a[i] == max){
  36. tmp[j] = i;
  37. j++;
  38. }
  39. }
  40. for (i = 0; i < j; ++i)
  41. {
  42. double temp = tmp[i]*0.01;
  43. printf("%.2f\n",temp);
  44. }
  45. return 0;
  46. }

单词的平均长度

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. char ch;
  5. int num=0,words=0;
  6. int inword=0;
  7. while((ch=getchar())!=EOF)
  8. {
  9. if(isalpha(ch)) num++;
  10. if(!isspace(ch)&&!inword)
  11. {
  12. inword=1;
  13. words++;
  14. }
  15. if(isspace(ch)&&inword)
  16. inword=0;
  17. }
  18. printf("The averge word is %.2f\n",(double)num/words);
  19. return 0;
  20. }

乘积的末3位

输入若干个整数(可以是正数、负数或者零),输出它们的乘积的末3位。这些整数中会混入一些由大写字母组成的字符串,你的程序应该忽略它们。提示:试试看,在执行scanf("%d")时输入一个字符串会怎样?

输入:AB123CC   BB123123321321          DDD22    888888888888888888888888888ZZ      -411B

输出:968

输入:AA-11BBB   D2CCC

输出:-22

假定末3位是指,不足3位就输出数字本身,如果是负数则包括负号,比如结果是-12,则输出-12;结果是11,则输出11,结果是0,则输出0;

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #define N 100000
  6. #define M 5000
  7. #define L 5
  8. char input[N];
  9. char str[M];
  10. char tmp[L];
  11. char rev[L];
  12.  
  13. int main(void)
  14. {
  15. int i, len, k;
  16. int flag = 1;
  17. int product = 1;
  18. char *p = NULL;
  19.  
  20. fgets(input, sizeof (input), stdin);
  21. p = input;
  22. while (sscanf(p, "%s", str) == 1) {
  23. len = k = 0;
  24. for (i = strlen(str)-1; i != -1; i--) {
  25. if (len != 3 && 1 == isdigit(str[i]))
  26. tmp[len++] = str[i];
  27. if (str[i] == '-') {
  28. flag = -flag;
  29. break;
  30. }
  31. }
  32. tmp[len] = '\0';
  33. while (len > 0)
  34. rev[k++] = tmp[--len];
  35. rev[k] = '\0';
  36. product *= atoi(rev);
  37. product %= 1000;
  38. p += strlen(str)+1;
  39. while (*p == ' ') p++;/* 滤空 */
  40. }
  41. printf("%d\n", product < 100 ? product*flag : product);
  42. return 0;
  43. }

计算器 

编程程序读入一行恰好包括一个+或-或*的表达式,输出它的值。运算符保证是二元运算符,且两个运算数均不超过100的非负整数。运算数和运算符可以紧挨也可以有一个或多个空格、TAB隔开。行首尾均可以有空格。提示:选择合适的输入方法可以将问题简化。

  1. #include <stdio.h>
  2. #define N 1000
  3. char str[N];
  4. int main(void)
  5. {
  6. char *p = NULL;
  7. int op1, op2;
  8.  
  9. fgets(str, sizeof(str), stdin);//it will contains '\n';
  10.  
  11. for (p = str; *p != '\n'; p++) {
  12. if (*p == '+' || *p == '-' || *p == '*')
  13. break;
  14. }
  15. if (*p != '\n'){
  16. switch(*p) {
  17. case '+':
  18. sscanf(str, "%d + %d", &op1, &op2);
  19. printf("%d\n", op1+op2);
  20. break;
  21. case '-':
  22. sscanf(str, "%d - %d", &op1, &op2);
  23. printf("%d\n", op1-op2);
  24. break;
  25. case '*':
  26. sscanf(str, "%d * %d", &op1, &op2);
  27. printf("%d\n", op1*op2);
  28. break;
  29. }
  30. }
  31. return 0;
  32. }

 输入一个n*n字符矩阵,把它左旋90度后输出

  1. #include <stdio.h>
  2. #define N 100
  3.  
  4. char a[N][N];
  5. int main(void)
  6. {
  7. int n;
  8. int i, j;
  9.  
  10. scanf("%d", &n);
  11.  
  12. for (i = 0; i != n; i++) {
  13. for (j = 0; j != n; j++)
  14. scanf("%s", &a[i][j]);
  15. }
  16.  
  17. for (j = n-1; j != -1; j--) {
  18. for (i = 0; i != n; i++)
  19. printf("%c ", a[i][j]);
  20. printf("\n");
  21. }
  22.  
  23. return 0;
  24. }

  进制转换

  1. #include <stdio.h>
  2. voidtrans(int n, int b)
  3. {
  4. if (n >= b)
  5. trans(n/b, b);
  6. printf("%d", n%b);
  7. }
  8. void trans(int n, int b);
  9. int main(void)
  10. {
  11. int b, n;
  12.  
  13. scanf("%d %d", &b, &n);
  14. trans(n, b);
  15. printf("\n");
  16. return 0;
  17. }
  18.  
  19. //非递归
  20. #include <stdio.h>
  21. #define N 100
  22. int a[N];
  23. int main(void)
  24. {
  25. int b, n;
  26. int k = 0;
  27. int i;
  28.  
  29. scanf("%d %d", &b, &n);
  30. while (n >= b) {
  31. a[k++] = n%b;
  32. n /= b;
  33. }
  34. a[k++] = n%b;
  35. for (i = k-1; i != -1; i--)
  36. printf("%d", a[i]);
  37. printf("\n");
  38. return 0;
  39. }
  40.  
  41. //输出基数b( 2 <= b <= 10)和正整数n(b进制),输出n的十进制表示
  42. #include <stdio.h>
  43. #include <string.h>
  44. #define N 100
  45. char str[N];
  46. int main(void)
  47. {
  48. int n, i, k, tmp;
  49. int res = 0;
  50.  
  51. scanf("%d %s", &n, str);
  52. for (i = strlen(str)-1; i != -1; i--) {
  53. tmp = str[i]-'0';
  54. k = strlen(str)-1-i;
  55. while(k--)
  56. tmp *= n;
  57. res += tmp;
  58. }
  59. printf("%d\n", res);
  60.  
  61. return 0;
  62. }

  

算法入门笔记------------Day2的更多相关文章

  1. 算法入门笔记------------Day4

    1.WERTYU 输入一个错位后敲出的字符串,输出打字员本来想打出的字 #include<stdio.h> char *s="`1234567890-=QWERTYUIOP[]\ ...

  2. 算法入门笔记------------Day3

    主要是复习前面的基本内容,以及函数的概念 组合数 #include<stdio.h> int f(int n) { int m=1; for(int i=1;i<=n;i++) m* ...

  3. 算法入门笔记------------Day1

    1.C语言使用%d显示float值,不会把float值转换为近似的int值,而是显示垃圾值,使用%f显示int值,也不会把该int值转换为浮点值 2.三位数反转:输入一个三位数,分离它的百位,十位和个 ...

  4. Android NDK JNI 入门笔记-day04-NDK实现Hash算法

    * Android NDK JNI 入门笔记目录 * 开头 前面的学习,我们已经掌握了 NDK 开发的必备知识. 下一步就要多实践,通过创造问题并解决问题,来增加熟练度,提升经验. 日常开发中,经常会 ...

  5. 「Android 开发」入门笔记

    「Android 开发」入门笔记(界面编程篇) ------每日摘要------ DAY-1: 学习笔记: Android应用结构分析 界面编程与视图(View)组件 布局管理器 问题整理: Andr ...

  6. React.js入门笔记

    # React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...

  7. redis入门笔记(1)

    redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...

  8. C++ 快速入门笔记:进阶编程

    C++入门笔记:高级编程 文件和流 打开文件 void open (const char *filename, ios::openmode mode); ios::app 追加模式.所有写入都追加到文 ...

  9. golang微服务框架go-micro 入门笔记2.2 micro工具之微应用利器micro web

    micro web micro 功能非常强大,本文将详细阐述micro web 命令行的功能 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go- ...

随机推荐

  1. html 报表导出excel防止数字变科学计数

    在html 标签加:  <html xmlns:x="urn:schemas-microsoft-com:office:excel">    在要导出的tr加:  &l ...

  2. c++常见面试题

    1.class和struct的区别? 在c++中,在class中声明的成员默认为private成员,而在struct中声明的成员默认为public成员,class的默认继承方式为private,str ...

  3. [题解]codevs1001 舒适的路线

    h3 { font-family: Consolas; color: #339966 } .math { font-family: Consolas; color: gray } 题目描述 Descr ...

  4. ギリギリ eye (优先队列)

    1.题目: [题目描述] A.D.1999,由坠落地球的"谜之战舰"带来的 Over Technology,揭示了人类历史和远 古文明之间的丝丝联系,促使人类终止彼此间的战争,一方 ...

  5. java中运算符的优先级

    所谓优先级,就是在表达式中的运算顺序.Java 中常用的运算符的优先级如下表所示: 级别为 1 的优先级最高,级别 11 的优先级最低.譬如,x = 7 + 3 * 2  得到的结果是 13 &quo ...

  6. Coursera Machine Learning 作业答案脚本 分享在github上

    Github地址:https://github.com/edward0130/Coursera-ML

  7. python学习之——splinter使用

    开始学习使用splinter工具了,目前是摸索中,先熟悉splinter工具的使用方法~~ 实现功能: 打开firefox浏览器->www.baidu.com->输入关键词 python, ...

  8. Microsoft Visual Stduio 2005 Ent安装报错解决方法

    错误:Microsoft Visual Studio 2015 Devenv : 安装时发生严重错误 安装过程第一次出现该错误时,查看了日志文件,错误提示如下: [0EEC:0EF0][2016-10 ...

  9. 浅淡HTML5移动Web开发

    说实话,我们这次开发移动端的项目,整个项目组的人都是第一次,最初立项的时候为是选择native app和web app还争论了一番,最后综合考虑,我们选择了web(我们选择了h5)开发.但从这两种开发 ...

  10. 深入理解Java虚拟机(三)、垃圾收集算法

    1.第一门真正使用内存动态分配和垃圾收集技术的语言:Lisp 2.程序计数器.虚拟机栈.本地方法栈这3个区域随线程而生灭,这几个区域的内存会随着方法结束或线程结束而回收,GC关注的是Java堆和方法区 ...