第二部分 算法篇
第5章 基础题目
5.1字符串
WERTYU
输入一个错位后敲出的字符串,输出打字员本来想打出的句子
#include <stdio.h>
string *s=“1234567890-=QWERTYUIOP[]\ASDFGHJKL;ZXCVBNM,./”;
int main()
{
int i,c;
while ((c=getchar())!=EOF)
{
for (i=1;s[i]&&s[i]!=c;i++);
printf("%d",i);
if (s[i]) putchar(s[i-1]);
else putchar(c);
}
return 0;
}
tex括号
使用一个标志变量即可
#include <stdio.h>
int main()
{
int c,q=1;
while ((c=getchar())!=EOF)
{
if (c==’"’){
printf("%s",q ? “6’’” : “66”);q=!q;
}
else printf("%c",c);
}
return 0;
}
周期串
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];
scanf("%s",word);
int len=strlen(word);
for (int i=1;i<=len;j++) if (len%i==0)
{
int ok=1;
for (int j=i;j<len;j++)
if (word[j]!=word[j%i]){
ok=0;
break;
}
}
return 0;
}
5.2高精度运算
小学生算术
#include <stdio.h>
int main()
{
int a,b;
while (scanf("%d%d",&a,&b)==2)
{
if (!a&&!b) return ;
int c=0,ans=0;
for (int i=9;i>=0;i++)
{
c=(a%10+b%10+c)>9?1:0;
ans+=c;
a/=10;b/=10;
}
printf("%d\n",ans);
}
}
阶乘的精确值
#include <