yb-oi培训记要

本文介绍了编程的基础概念,包括变量声明、输入输出操作、三种基本程序结构等,并讲解了简单的算法如最大值、最小值、质数判断等,适合编程初学者阅读。

7.22 3hours

play movie: Obama programing for an hour a day, no teaching in school,Zuckerberg

noip's introduction

maze blockly, no try

dev c++, 5.11

first program: cout<<"hello world ";

IPO:

declare var: type name1,name2; //int double char bool string

input: 

cin>>var_name1>>var_name2; //cin>>a>>b;

scanf("%d",&a); 

process:

var_name=expression; //a=5; y=x*x+2*x+1;

output:

cout<<a<<"+"<<b<<'='<<sum;

printf("%.2lf",sum);

three basic structers of Programs

Sequence Structure:

cin>>a>>b;

t=a; a=b; b=t;

math expression:

% 7%3=1

/  7/3=2

((x-y)*x+5*x)*y+y*y

a=n%10;

b=n/10%10;

c=n/100;

 

branch structure:

make computer smart

relational expression: > < >= <= == !=

logical expression: && || !

if( condition expression){ statement;}

else { statement;}

if( a%2==0) cout<<"even";

else cout<<"odd";

if(year%4==0&&year%100!=0||year%400==)cout<<"leap year";

if(a<b)a=b;

if(a<c)a=c;

if(a<b){t=a;a=b;b=t;}

if(a<c){t=a;a=c;c=t;}

if(b<c){t=b;b=c;c=t;}

if(score>=75&&score<90) cout<<"better";

 

0723

nesting

if(month==4||month==6||moth==9||moth==11)

  d=30;

else if(month==2){

          if(year==leap_year) d=29;

         else d=28;

     }

    else d=31;

switch(expression){

  case value1: statement;break;

  case value2:statement;break;

  default:statement;

}

 

loop structure:

computer's fast speed

while(condition expression){

  statement

}

counter:

char c;

cin>>c;

while(c!='#'){

  if(c=='a'||c=='A') count++;

  cin>>c;

}

cout<<count;

 

cin>>n;

i=count=0;

while(i<n){

  cin>>a;

  if(a%2==0) count++;

  i++;

}

cout<<count;

 

accumulator:

cin>>n;

sum=0;

for(i=0;i<n;i++){

  cin>>ai>>bi;

  if(bi=='Y') sum+=ai;

}

cout<<sum;

 

table:

do{

  cin>>c;

  if(c=='W')w++;

  if(c=='L')l++;

  if(w>10&&w-l>1||l>10&&l-w>1){

  cout<<w<<':'<<l<<endl;

  w=l=0;

}

}while(c!='E')

 

0724

Small Algorithms and Skills

Maximum

Minimum

prime number(marking)

Exhaustion

Fibonacci

gcd

lcm

Recurrence(iteration)

 

write the results of programs

 

multiple loops

write the stars

a row

a row×column

Hundreds of dollars and hundreds of chickens

Optimization of algorithm

 

0725

sum, Simulation

count: exhaustive, divide number, counter

 

exercise:

golden coin

open the door

interesting prime

 

one dimensional array

type name[const expression];

int a[50];

a[0],a[1]...a[49]

 

IPO

cin>>n;

for(i=0;i<n;i++)cin>>a[i];

memset(a,0,sizeof(a));

int a[50]={0,1}

for(i=n-1;i>=0;i--)cout<<a[i]<<' ';

 

enter the  scores of n students , print out the number and score of student whose  score is lower than average

output the numbers of the same number: counter

Taotao pick appples: counter

calculate the money of books: use array as constant

age and ills: use array as counters

bucket sort: use array as counters and sort

 

fibonacci

see the results the program

#include <stdio.h>

int main(){

   int i,j,h,m,n,k;

   int b[11];

   scanf("%d",&n);

   for(i=1;i<=10;i++){

       m=n;

       j=11;

       while(m>0){

           j--;

           b[j]=m%10;

           m/=10;

       }

       for(h=j;h<=10;h++)

           n+=b[h];

   }

   printf("%d\n",n);

   return 0;

}

input:1234

output:

 

 

 

save plan:

 

stairs

Palindrome number

Binary Conversion

sort

 

Two-dimensional array

int student[50][7];

Saddle number

 

function

type name(parameters){

  return ?;

}

name(parameters);

Parametric function

void

return value

 

recursive

Reverse order

Factorial

 

0726

High Precision Computation

bubble sort

insert sort

Bucket sort

quick sort

merge sort

 

0727

search

eight queens

simple dynamic plan

 

0728

test

freopen

create input file

lemon

 

 

 

 

<think> 我们之前讨论了用户脚本中出现的语法错误(未闭合的单引号导致文件结束符错误),现在用户提供了新的引用,并希望了解grep命令在脚本中的使用情况、可能的问题及解决方案。结合用户之前遇到的语法错误,我们需要重点关注grep命令中引号使用不当可能导致的类似错误。 用户引用信息分析: 引用[1]:展示了split_tags函数与grep的管道使用,重点在引号内使用特殊字符(`<`)和变量传递。 引用[2]:说明grep的静默模式替代方案(重定向到/dev/null),并涉及输出重定向的语法(2>&1)。 引用[3]:列出Linux常用命令,与grep直接相关的是rpm查询命令,但可忽略。 用户需求:解决脚本中使用grep命令时出现的问题(尤其是引号错误和文件结尾错误)。 解决方案设计: 1. 分析grep命令中引号使用场景(单引号、双引号、无引号)及其与之前语法错误的关系。 2. 针对用户之前遇到的“未预期文件结束符”错误,重点检查grep模式字符串中的引号闭合问题。 3. 提供grep常见问题及解决方法,包括特殊字符处理、模式字符串跨行问题、引号嵌套等。 4. 结合错误案例给出修复示例。 具体回答结构: ### 解决Shell脚本中grep命令的引号错误 - 分析错误原因(引号不闭合如何影响整个脚本) - 常见问题场景及解决方案(含代码示例) - 调试技巧 ### 相关问题列表 生成3个相关问题 </think> ### 解决Shell脚本中grep命令的引号错误与文件结尾问题 在脚本中使用`grep`命令时(如`grep 'PT - SH - OC - OI083217' -Rn`),若引号使用不当会导致两类关键错误: 1. **未闭合引号错误**(寻找匹配的“'”时遇到未预期的文件结束符) 2. **文件结尾语法错误**(未预期的文件结尾) 这些问题通常由以下场景引发: #### 一、常见错误场景及解决方案 1. **模式字符串包含未转义的特殊字符** - **问题**:当搜索模式包含`$`、`!`或`'`时,未转义会破坏引号结构 ```bash # 错误示例($未转义) grep 'Version: $5.0' file.txt # $会被解释为变量 ``` - **修复方案**: - 使用单引号包裹整个模式并转义内部单引号 ```bash grep 'PT - SH - OC - OI083217'\''s log' -Rn # 用'\''转义单引号 ``` - 或改用双引号但转义特殊字符 ```bash grep "PT - SH - OC - OI083217\'s log" -Rn # 双引号内用\'转义 ``` 2. **跨行模式字符串未正确处理** - **问题**:长模式跨行书写时意外断开引号 ```bash # 错误示例(行尾\后多空格) grep 'PT - SH - OC \ - OI083217' -Rn # \后空格导致换行符未被转义 ``` - **修复方案**: - 使用`$'...'`格式支持转义换行符 ```bash grep $'PT - SH - OC\n- OI083217' -Rn # \n显式表示换行 ``` - 或用变量保存模式 ```bash pattern="PT - SH - OC - OI083217" grep "$pattern" -Rn ``` 3. **管道操作中grep输出未过滤** - **问题**:未处理空输出导致后续命令语法错误 ```bash # 错误示例(grep无匹配时rm收到空参数) grep "temp_file" *.log | xargs rm ``` - **修复方案**: - 通过重定向屏蔽错误(引用[2]方法) ```bash grep "louise" /etc/passwd > /dev/null 2>&1 && echo "Found" ``` - 或用`-q`抑制输出 ```bash if grep -q "critical_error" system.log; then alert_admin # 仅检查存在性[^1] fi ``` #### 二、调试技巧与预防措施 1. **语法验证工具** ```bash bash -n script.sh # 检查语法错误 shellcheck script.sh # 静态分析工具 ``` 2. **分阶段执行** ```bash # 先单独测试grep命令 pattern='PT - SH - OC - OI083217' grep "$pattern" -Rn /target_dir > result.tmp ``` 3. **引号使用规范** | 场景 | 推荐方案 | 示例 | |---------------------|-------------------|--------------------------| | 含变量 | 双引号 | `grep "$var" file` | | 含特殊字符 | 单引号+转义 | `grep '!\$#' file` | | 跨行长字符串 | 引号外拼接 | `'part1'"part2"` | > **关键原则**:当模式字符串包含连字符(`-`)、空格或斜杠时,必须用引号包裹整个模式,否则会被解析为命令选项或分隔符(如`PT`被误认为选项)[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值