算法竞赛入门经典第三章
程序员文章站
2024-03-18 23:21:46
...
例题3-4 猜数字的提示
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n,kase=0;
int a[100],b[100];
while(scanf("%d",&n)==1&&n){
printf("Game %d:\n",++kase);
int i,j;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(;;){
int A=0,B=0;
for(i=0;i<n;i++){
scanf("%d",&b[i]);
if(a[i]==b[i])
A++;
}
if(b[0]==0&&b[1]==0&&b[2]==0&&b[3]==0)
break;
/*for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(b[i]==a[j]&&b[j]!=a[j]&&b[i]!=a[i])
B++;break;
}
}
//B=B-A;*/
int num;
for(num=1;num<=9;num++){
int c1=0,c2=0;
for(i=0;i<n;i++){
if(a[i]==num) c1++;
if(b[i]==num) c2++;
}
if(c1<c2)
B+=c1;
else
B+=c2;
}
B=B-A;
printf(" (%d,%d)\n",A,B);
}
}
return 0;
}
例3-5 生成元
例3-6 环状序列
方法一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n,i,j=0;
char s0[220],s1[220];
scanf("%s",s0);
n=strlen(s0);
strcpy(s1,s0);
strcat(s0,s1);
strcpy(s1,s0);
for(i=0;i<n;i++){
if(strncmp(s0+j,s1+i,n)>0)
j=i;
}
strncpy(s1,s0+j,n);
s1[n]='\0';
printf("%s",s1);
return 0;
思考题
题目1 必要的存储量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void count(){
int n,count=0;
while(scanf("%d",&n)!=EOF)
count++;
return count;
}
void MaxMinAve(){
int max=-100000,min=100000,s=0,n,count=0,average;
while(scanf("%d",&n)!=EOF){
if(max<n) max=n;
if(min>n) min=n;
s+=n;
count++;
}
average=s/count;
printf("%d %d %d",max,min,average);
//return average;
}
void closest(){
int s[100],n=0;
int i,j,len;
int a,b,distence=100000;
//scanf("%s",s);
while(scanf("%d",&s[n])!=EOF)
n++;
len=strlen(s);
for(i=0;i<len;i++){
for(j=i+1;j<len;j++){
if(abs(s[i],s[j])<distence)
distence=abs(s[i],s[j]);
a=s[i];
b=s[j];
}
}
printf("%d %d最接近,距离是%d",a,b,distance);
}
void second(){
int max=-100000,second;
while(scanf("%d",&n)!=EOF){
if(max<n){
second=max;
max=n;
}
}
printf("%d",second);
}
void fangcha(){
int nums[100];
int count = 0, n, i;
double ave = 0.0, sum = 0.0, psum = 0.0;
while(1 == scanf("%d", &n))
{
nums[count++] = n;
sum += n;
}
ave = sum / count;
for(i = 0; i < count; ++i)
psum += (nums[i] - ave) * (nums[i] - ave);
printf("variance: %.3f\n", psum / count);
}
void SmallerThanAverage(){
int nums[100],n;
int count=0,n,i;
int ave,sum=0;
int ct=0;
while(1 == scanf("%d", &n))
{
nums[count++] = n;
sum += n;
}
ave = sum / count;
for(i = 0; i < count; ++i){
if(num[i]<ave)
ct++;
}
printf("%d",ct);
}
int main(int argc, char *argv[]) {
return 0;
}
题目2 统计字符1的个数
1.缺少头文件<string.h>;maxn太大
2.for循环不断求字符串长度
3.统计字符串中字符为‘1’,而非1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define maxn 10000+10
int main(int argc, char *argv[]) {
char s[maxn];
scanf("%s",s);
int tot=0,n,i;
n=strlen(s);
for(i=0;i<n;i++)
if(s[i]=='1')
tot++;
printf("%d\n",tot);
return 0;