1.7.9 字母后移
读入一行的代码:
while((k=getchar())!=EOF)
ASCII码
小写\(a\) 97
小写\(z\) 122
大写\(A\) 65
大写\(Z\) 90
数字0 \(48\)
数字9 \(57\)
//1.7.9 #include<bits/stdc++.h> using namespace std; int main() { char p[85]; char k; int i=0; while((k=getchar())!=EOF){ if(k>=65&&k<=89) k++; else if(k==90) k=65; else if(k>=97&&k<=121) k++; else if(k==122) k=97; //else if(k==32) putchar('32'); p[i]=k; i++; putchar(k); } //puts(p); return 0; }
1.7.11 潜伏者
//1.7.11? #include <algorithm> #include <iostream> #include <cmath> #include <cstdio> #include <cstring> using namespace std; #define N 1020 char s1[N],s2[N],s3[N]; int a[26]; bool fail = 0; int main(){ scanf("%s%s%s",s1,s2,s3); for(int i=0;i<26;i++){ a[i]=-1; } int len=strlen(s1); if(len<26) fail=1; else{ for(int i=0;i<len;i++){ if(a[s1[i]-'A'] == -1){ a[s1[i]-'A']=s2[i]- 'A'; }else{ if(a[s1[i]-'A']!= s2[i]- 'A'){ fail = 1; break; } } } for(int i = 0;i<26;i++){ if(a[i]==-1){ fail = 1; break; } } for(int i = 0;i<26;i++){ for(int j=i+1;j<26;j++){ if(a[i]==a[j]){ fail = 1; break; } } } } if(fail == 1){ cout<<"Failed"<<endl; } else{ len = strlen(s3); for(int i = 0; i <len;++i) s3[i]=a[s3[i]-'A']+'A'; printf("%s\n",s3); } return 0; }
1.7.18 验证子串
#include <cstirng> strstr(a,b);
返回值是1或0,用于判断是否是子串
//1.7.18 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; char a[205],b[205]; int main( ){ gets(a); gets(b); int len1,len2; len1=strlen(a);len2=strlen(b); bool op=0; if(len2>len1){ swap(a,b); swap(len1,len2); } if(strstr(a,b)) printf("%s is substring of %s",b,a); else printf("No substring"); return 0; }
1.7.24 单词的长度
把下列语句修改一下就好了【这告诉我们\(<iostream>\)不可靠】
```
cout>>s>>" ,"; //原来
printf("%d,",s);//现在
有可能""(双引号)打成‘’(单引号)了一开始没发现 所以还是$printf$吧
//1.7.24
include
include
include
include
using namespace std;
char a[2001];
int s=0;
int main(){
gets(a);
int l=strlen(a);
for(int i=0;i<l;i++)
{
if(a[i]!=' ') s++;
else if(a[i]==' '&&a[i-1]!=' ')
{
printf("%d,",s);
s=0;
}
}
cout<<s;
return 0;
}
# 1.7.29 $ISBN$号码 忘了$k==10$的特判‘$X$’
//1.7.29
include
include
include
include
using namespace std;
char a[15];
int b[15];
int k;
int main( ){
scanf("%s",a+1);
int j=1;
for(int i=1;i<=11;i++){
if(i==2||i==6) continue;
else {
k+=(a[i]-'0')*j;
j++;
}
}
//printf("%d\n",k);
k%=11;
if(k==10) k='X'-'0';
if(k==a[13]-'0') printf("Right");
else{
a[13]=k+'0';
printf("%s",a+1);
}
return 0;
}
重点!!!
k%=11;
if(k==10) k='X'-'0';//关于字符之间的运算
if(k==a[13]-'0') printf("Right");
else{
a[13]=k+'0';
printf("%s",a+1);
}
# 1.7.32 行程缩写 在程序内开数组,要memset要不然输出的数很怪
//1.7.32
include
include
include
include
include
using namespace std;
int main( ){
char s[1006];
scanf("%s",s+1);
int a[1010];
memset(a,0,sizeof(a));//!!
int len1;
len1=strlen(s+1);
for(int i=1;i<=len1;i++){
if(s[i]>='a'&&s[i]<='z') s[i]=s[i]-'a'+'A';
}
//printf("%s",s+1);
for(int i=1;i<=len1;i++){
if(s[i]==s[i+1]) a[s[i]-'A']++;
else {
printf("(%c,%d)",s[i],a[s[i]-'A']+1);
a[s[i]-'A']=0;
}
}
return 0;
}
# 1.7.35 字符串的展开
//1.7.35
include
include
include
include
include
using namespace std;
bool iszimu(char a){
if(a>='a'&&a<='z') return true;
return 0;
}
void solve(string &s,int p1,int p2,int p3){
if(p1==1)
{
for(int i=0;i<s.length();i++)
{
if(s[i]=='-')
{
if(iszimu(s[i-1])&&iszimu(s[i+1])&&s[i+1]>s[i-1]+1)
{
if(p3==1)
{
for(char temp = s[i-1]+1;temp<s[i+1];temp++)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
else
{
for(char temp = s[i+1]-1;temp>=s[i-1]+1;temp--)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
continue;
}
else if(isdigit(s[i-1])&&isdigit(s[i+1])&&s[i+1]>s[i-1]+1)
{
if(p3==1)
{
for(char temp = s[i-1]+1;temp<s[i+1];temp++)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
else
{
for(char temp = s[i+1]-1;temp>=s[i-1]+1;temp--)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
continue;
}
else if(s[i-1]>=s[i+1] ) cout<<'-';
else if(s[i-1]+1==s[i+1]);
else cout<<'-';//刚开始没有这个,如果s-2这种就会出错(少一个-)
}
else
cout<<s[i];
}
cout<<endl;
}
else if(p1==2)
{
for(int i=0;i<s.length();i++)
{
if(s[i]=='-')
{
if(iszimu(s[i-1])&&iszimu(s[i+1])&&s[i+1]>s[i-1]+1)
{
if(p3==1)
{
for(char temp = s[i-1]+1-('a'-'A');temp<s[i+1]-('a'-'A');temp++)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
else
{
for(char temp = s[i+1]-1-('a'-'A');temp>=s[i-1]+1-('a'-'A');temp--)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
continue;
}
else if(isdigit(s[i-1])&&isdigit(s[i+1])&&s[i+1]>s[i-1]+1)
{
if(p3==1)
{
for(char temp = s[i-1]+1;temp<s[i+1];temp++)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
else
{
for(char temp = s[i+1]-1;temp>=s[i-1]+1;temp--)
{
for(int j=0;j<p2;j++) cout<<temp;
}
}
continue;
}
else if(s[i-1]>=s[i+1] ) cout<<'-';
else if(s[i-1]+1==s[i+1]);
else cout<<'-';
}
else
cout<<s[i];
}
printf("\n");
}
else
{
for(int i=0;i<s.length();i++)
{
if(s[i]=='-')
{
if(iszimu(s[i-1])&&iszimu(s[i+1])&&s[i+1]>s[i-1]+1)
{
for(char temp = s[i-1]+1;temp<s[i+1];temp++)
{
for(int j=0;j<p2;j++) cout<<'';
}
continue;
}
else if(isdigit(s[i-1])&&isdigit(s[i+1])&&s[i+1]>s[i-1]+1)
{
for(char temp = s[i-1]+1;temp<s[i+1];temp++)
{
for(int j=0;j<p2;j++) cout<<'';
}
continue;
}
else if(s[i-1]>=s[i+1] ) cout<<'-';
else if(s[i-1]+1==s[i+1]);
else cout<<'-';
}
else
cout<<s[i];
}
cout<<endl;
}
}
int main(){
int p1,p2,p3;
string s;
while(cin>>p1>>p2>>p3)
{
cin>>s;
solve(s,p1,p2,p3);
}
return 0;
}
# 最小公倍数·最大公因数 ### 辗转相除法 重要定理:如果$a,b$的最大公因数和最小公倍数是$m,n$,那么$ab=mn$
include
include
include
using namespace std;
int gcd(int x,int y){
int r=x%y;
while(r!=0){
x=y;
y=r;
r=x%y;
}
return y;
}
int lcm(int x,int y){
return x*y/gcd(x,y);
}
int main( ){
int a,b;
printf("Please input two numbers a and b");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
printf("gcd(%d,%d)=%d,lcm(%d,%d)=%d",a,b,gcd(a,b),a,b,lcm(a,b));
}↑辗转相除法求最大公因数和最小公倍数 ### 以1.7.30字符环为例
cpp
//1.7.30 字符环
include
include
include
include
using namespace std;
int gcd(int x,int y){
int r=x%y;
while(r!=0){
x=y;
y=r;
r=x%y;
}
return y;
}
int lcm(int x,int y){
return x*y/gcd(x,y);
}
int main( ){
char s1[260],s2[260];
scanf("%s%s",s1+1,s2+1);
int len1,len2;
len1=strlen(s1);len2=strlen(s2);
if(len2>len1){ swap(len1,len2); swap(s1,s2); } int lengcd; lengcd=gcd(len1,len2); for(int i=1;i<=len1;i++){ for(int j=1;j<=len1+len2,j++){ int k=j%len2; while(s1[k]) } }
}
```
# 统计单词个数
```cpp
include
include
using namespace std;
char s[105];
int nw=1,count=0;
//way1
int main( ){
gets(s+1);
s[0]=' ';
int le=strlen(s+1);
//printf("%d \n",le);
for(int i=0;i<=le;i++){
if(s[i]==' '){
nw=1;
}
else{
if(nw==1){
nw=0;
count++;
}
}
// printf("%d %d %c\n",i,count,s[i]);
}
printf("%d",count);
return 0;
}
void way2( ){
gets(s);
for(int i=0;i<=101;i++){
if(s[i]=='\0'){
break;
}
if(s[i]==' '){
nw=1;
}
else{
if(nw==1){
nw=0;
count++;
}
}
//printf("%d %d %c\n",i,count,s[i]);
}
printf("%d",count);
return ;
}
include
include
include
using namespace std;
char sm[105];
char str[105];
int cnt=0;
void way3(){
stack < int > sp;
gets(sm);
int le=strlen(sm);
sp.push(0);//事先压入空格
for(int i=0;i<le;i++){
if(sm[i]!=' ') sp.push(sm[i]-'0');
else sp.push(0);//处理字符串
}
while(sp.size()!=0){
char tmp;
tmp=sp.top();
if(tmp==0) cnt++;
sp.pop();//统计
}
printf("%d",cnt);
return ;
}
```