1.读入一些整数,逆序输出到一行中,已知整数个数不超过100个。
注意:while(scanf("%d",&x)==1)表示当输入数据个数为一个时。输入结束先按enter,再ctrl+z,再enter。
#include<stdio.h>
#define MAXN 110//数组大小定义稍大写更保险
int a[MAXN];//比较大的数组尽量声明在main函数外
int main()
{
int n=0,i,x;
while(scanf("%d",&x)==1)
a[n++]=x;//等价于{a[n]=x;n++;}
for(i=n-1;i>=1;i--)
printf("%d ",a[i]);
printf("%d\n",a[0]);
return 0;
}
2.开灯问题
有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯被打开, 开着灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?
输入:n和k,输出开着的灯编号。k≤n≤1000。
样例输入:7 3
样例输出:1 5 6 7
#include<stdio.h>
#include<string.h>
#define MAXN 1000+10
int a[MAXN];
int main()
{
int i,j,n,k,first = 1;
memset(a,0,sizeof(a)); //把数组清零
scanf("%d%d",&n,&k);
for(i=1;i<=k;i++)
for(j=1;j<=n;j++)
if(j%i==0) a[j] = !a[j]; //a[j]=0或1
for(i=1;i<=n;i++)
if(a[i]) {if(first) first=0; else printf(" "); printf("%d",i);} //设置first标志变量使第一个输出前没有空格,其他都有
printf("\n");
return 0;
}
3.给定一个 n , 在 n * n 的方阵中填入 1 ,2, 3,……,n * n, 要求填成蛇形
例如在 n = 5 时 , 如下所示:
13 14 15 16 1
12 23 24 17 2
11 22 25 18 3
10 21 20 19 4
9 8 7 6 5
思路:先判断再填数,填数是考虑方向的问题。
#include<stdio.h>
#include<string.h>
#define MAXN 10
int a[MAXN][MAXN];
int main()
{
int n,x,y,tot=0;
scanf("%d",&n);
memset(a,0,sizeof(a));
tot=a[x=0][y=n-1] = 1;
while(tot<n*n)
{
while(x+1<n && !a[x+1][y]) a[++x][y] = ++tot;
while(y-1>=0 && !a[x][y-1]) a[x][--y] = ++tot;
while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++tot;
while(y+1<n && !a[x][y+1]) a[x][++y] = ++tot;
}
for(x=0;x<n;x++){
for(y=0;y<n;y++) printf("%d ",a[x][y]);
printf("\n");
}
return 0;
}
4.找出形如 abc*de (三位数乘以两位数) 的算式,使得在完整的竖式中,所有数字属于一个特定的数字集合。输入数字集合 (相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后应有一个空行。最后输出解的总数。
样例输入:
2357
样例输出:
#include<stdio.h>
#include<string.h>
int main()
{
int abc,de,x,y,i,z,count=0,ok;
char s[20],buf[99];
scanf("%s",s);
for(abc=111;abc<=999;abc++)//刘老师给出的是111,但是我没有想明白。懂的朋友可以解释一下
for(de=11;de<=99;de++)
{
x=abc*(de%10);y=abc*(de/10);z=abc*de;
sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z);
ok = 1;
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",abc,de,x,y,z);
}
}
printf("the number of solutions:%d",count);
return 0;
}
练习1.用getchar函数读入一个整数。假设它占据单独的一行,读到行末为止,包括换行符。输入保证读入的整数可以保存在int中。
#include<stdio.h>
#include<stdlib.h>
#define MAXN 1000
int main()
{
char a[MAXN];
char ch;
int i=0;
for(ch;ch = getchar();ch != EOF)
{
if(ch!='\n') a[i++]=ch;
else break;
}
i=atoi(a);
printf("%d",i);
return 0;
}
练习2.怎么在c语言中,只用fgets函数读入一个整数,假设它占据单独的一行,读到行末为止,包括换行符,输入保证读入的整数可以保存在int 中。
#include<stdio.h>
#include<stdlib.h>
#define MAXN 1000
int main()
{
char a[MAXN];
int i=0;
fgets(a,sizeof(a),stdin);
i=atoi(a);
printf("%d",i);
return 0;
}
练习3.只用getchar实现fgets的功能。
注意:一开始我的输出里面有很多“烫”。这是因为字符串数组里有的元素没有被赋值。应该加上以’\0’结尾。
#include<stdio.h>
//#include<stdlib.h>
#define MAXN 1000
int main()
{
char a[MAXN];
char ch;
int i=0;
for(i=0;;){
ch=getchar();
if(ch!= '\n') a[i++]=ch;
else {
a[i++]='\0';//字符串以'\0'结尾
break;}
}
//printf("%s",a);
puts(a);
}
字符串处理的常见问题
tot=1;
for(i = 0; i < strlen(s); i++)
if(s[i] == 1) tot++;
printf("There are %d character(s) '1' in the string.\n",tot);
本程序的功能是统计字符串中字符1的个数。
实验1:添加字符串s的声明语句,长度不小于107。提示:放在main函数内还是外?
解答:添加字符串s的声明语句如下:
#define MAXN 10000000
char s[MAXN];
应放在main函数外。
实验2:添加读取语句,测试上述程序是否输出了期望的结果。如果不是,请改正。
#include<stdio.h>
//#include<stdlib.h>
#include<string.h>
#define MAXN 10000000
char s[MAXN];
int main()
{
fgets(s,sizeof(s),stdin);
int tot=0,i;
for(i=0;i<strlen(s);i++)
if(s[i]=='1') tot++;//把1改为'1'
printf("%d",tot);
return 0;
}