竖式问题:
#include<stdio.h>
#include<string.h>
int main(void)
{
int count = 0;
char s[20],buf[99];
scanf("%s", s);
int abc, de;
for(abc=111;abc<=999;abc++)
for(de=11;de<=99;de++){
int x;
x = abc*(de%10);
int y;
y=abc*(de/10);
int z;
z=abc*de;
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
int ok = 1;
int i;
for(i=0;i<strlen(buf);i++)
if(strchr(s, buf[i]) == NULL)
ok=0;
if(ok){
printf("<%d>\n",++count);
printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);
}
}
printf("the number of solutions = %d\n",count);
return 0;
}
TeX
#include<stdio.h>
int main(void)
{
int c,q=1;
while((c = getchar()) != EOF){
if(c == ‘"‘){
printf("%s",q ? "“" : "”");
q = !q;
}
else
printf("%c", c);
}
return 0;
}
//这个程序是将英文的引号变成中文的引号。
WERTYU:
#include<stdio.h>
char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;‘ZXCVBNM,./";
int main(void)
{
int i, c;
while((c=getchar()) != EOF){
for(i=1;s[i] && s[i] != c; i++)
;
if(s[i])
putchar(s[i-1]);
else
putchar(c);
}
return 0;
}
回文数:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"};
char r(char ch){
if(isalpha(ch))
return rev[ch - ‘A‘];
return rev[ch - ‘0‘ + 25];
}
int main(void)
{
char s[30];
while(scanf("%s", s) == 1){
int len = strlen(s);
int p=1, m = 1;
int i;
for(i=0; i<(len+1)/2;i++){
if(s[i] != s[len-1-i])
p = 0;
if(r(s[i]) != s[len-1-i])
m = 0;
}
printf("%s -- is %s.\n\n", s, msg[m*2+p]);
}
return 0;
}
猜数字游戏:
#include<stdio.h>
#define maxn 1010
int main(void)
{
int n, a[maxn], b[maxn];
int kase = 0;
while(scanf("%d", &n) == 1 && n)
{
printf("Game %d:\n", ++kase);
int i;
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(; ;){
int A = 0, B = 0;
int i;
for(i=0;i<n;i++){
scanf("%d", &b[i]);
if(a[i] == b[i])
A++;
}
if(b[0] == 0)
break;
int d;
for(d=1; d<=9; d++){
int c1=0,c2=0;
int i;
for(i=0; i<= n; i++){
if(a[i] == d)
c1++;
if(b[i] == d)
c2++;
}
if(c1 < c2)
B += c1;
else
B += c2;
}
printf(" (%d,%d)\n", A, B-A);
}
}
return 0;
}
生成元:
#include<stdio.h>
#include<string.h>
#define maxn 100005
int ans[maxn];
int main(void)
{
int T, n;
memset(ans, 0, sizeof(ans));
int m;
for(m=1;m<maxn;m++){
int x = m, y = m;
while(x>0){
y += x%10;
x /= 10;
}
if(ans[y] == 0 || m < ans[y])
ans[y] = m;
}
scanf("%d", &T);
while(T--){
scanf("%d", &n);
printf("%d\n", ans[n]);
}
return 0;
}
环状序列:
#include<stdio.h>
#include<string.h>
#define maxn 105
int less(const char *s, int p, int q){
int n = strlen(s);
int i;
for(i=0;i<n;i++)
if(s[(p+i)%n] != s[(q+i)%n])
return s[(p+i)%n] < s[(q+i)%n];
return 0;
}
int main(void)
{
int T;
char s[maxn];
scanf("%d", &T);
while(T--){
scanf("%s", s);
int ans = 0;
int n = strlen(s);
int i;
for(i=1;i<n;i++)
if(less(s, i, ans))
ans = i;
for(i=0;i<n;i++)
putchar(s[(i+ans)%n]);
putchar(‘\n‘);
}
return 0;
}
这是依照算法竞赛入门经典中第三章的问题,主要涉及数组和字符串,个人觉得还是有一定难度,记录在博客,供日后参考。