目录
3. 日本发生命案,警察把嫌疑人确定在4人当中(a,b,c,d) 。审讯结果:两个人说的真话,两个人说的假话。请根据以下对话及审讯结果写一个代码,判断谁是罪犯。
4. 给定一个整数,判断它是否被3、5、7整除,并输出一下信息:
1. 杨辉三角
#include <stdio.h>
int main(int argc, const char *argv[])
{
//杨辉三角(练)
int m;
printf("m=");
scanf("%d",&m);
int a[m][m];
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-i;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if(j==0||i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
printf("%4d",a[i][j]);
}
printf("\n");
}
return 0;
}
2. 字母三角
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i,j;
char a='A';
for(i=1;i<5;i++)
{
for(j=0;j<i;j++)
{
printf("%c",a);
}
a++;
printf("\n");
}
printf("\n");
char b='D';
for(i=1;i<5;i++)
{
for(j=5;j>i;j--)
{
printf("%c",b);
}
b--;
printf("\n");
}
return 0;
}
3. 日本发生命案,警察把嫌疑人确定在4人当中(a,b,c,d) 。审讯结果:两个人说的真话,两个人说的假话。请根据以下对话及审讯结果写一个代码,判断谁是罪犯。
#include <stdio.h>
int main(int argc, const char *argv[])
{
char killer=0;
for(killer='A';killer<='D';killer++)
{
if((killer!='A')+(killer!='B'&&killer=='C')+(killer=='A'||killer=='D')+(killer!='D'&&killer=='C')==2)
{
printf("killer=%c\n",killer);
}
}
return 0;
}
4. 给定一个整数,判断它是否被3、5、7整除,并输出一下信息:
①能同时被3,5,7整除(直接输出3 5 7,每个数字之间一个空格)
②只能内其中两个整数整除(输出两个数,小的在前,大的在后,例如3 5 或者 3 7 或者5 7,中间使用空格隔开)
③只能被其中一个整数整除(输出这个除数)
④不能被任何数整除,输出小写字母n
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num,a,b,c;
printf("num=");
scanf("%d",&num);
a=num%3;
b=num%5;
c=num%7;
if(a==0 || b==0 || c==0)
{
if(a==0 && b==0)
{
if(c==0)
printf("3 5 7\n");
else
printf("3 5\n");
}
else if(a==0&&c==0)
printf("3 7\n");
else if(b==0&&c==0)
printf("5 7\n");
else
{
if(a==0)
printf("3\n");
else if(b==0)
printf("5\n");
else
printf("7\n");
}
}
else
printf("n\n");
return 0;
}
5. 字符串逆置【good good study】
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
//字符串逆置
char a[]="good good study";
int i=0,j=strlen(a)-1,k=0;
char t;
while(i<j)//整体逆置
{
t=a[i];a[i]=a[j];a[j]=t;
i++;j--;
}
i=0;
puts(a);
while(a[i]!='\0')//单个单词逆置 i代表单词首字母,j需要找到单词尾字母
{
j=i;
while(a[j]!=' '&&a[j]!='\0')//找到单词后的空格
{
j++;
}
k=j-1;//定位单词尾字母
while(i<k)
{
t=a[i];a[i]=a[k];a[k]=t;
i++;k--;
}
while(a[j]==' ')//继续完成下一个单词的逆置
{
j++;
}
i=j;
}
puts(a);
return 0;
}
6. atoi函数
#include <stdio.h>
int main(int argc, const char *argv[])
{
//atoi
int i=0,sum=0;
char a[]="456";
while(a[i]!='\0')
{
int n=a[i]-'0';
sum=sum*10+n;
i++;
}
printf("sum=%d\n",sum);
return 0;
}
7. 输入一个字符串,删除字符串中的空格
【输入】"this is my class"
【输出】"thisismyclass"
#include <stdio.h>
int main(int argc, const char *argv[])
{
char a[20]="";
gets(a);
char b[20]="";
int i=0,j=0;
while(a[i]!='\0')
{
if(a[i]==' ')
{
i++;
}
else
{ b[j]=a[i];
i++;j++;
}
}
b[j]='\0';
puts(b);
return 0;
}
8. 四个字符串函数,非函数实现
(1)strlen【计算字符串长度】
#include <stdio.h>
int main(int argc, const char *argv[])
{
char a[20]="";
gets(a);
int count=0;
for(int i=0;a[i]!='\0';i++)
{
count++;
}
printf("a的长度为:%d\n",count+1);
return 0;
}
(2)strcpy【拷贝字符串】
#include <stdio.h>
int main(int argc, const char *argv[])
{
//非函数实现strcpy
char a[10]="asdf";
char b[10]="123";
int i=0;
while(b[i]!='\0')
{
a[i]=b[i];
i++;
}
a[i]='\0';
puts(a);
puts(b);
return 0;
}
(3)strcat【连接字符串】
#include <stdio.h>
int main(int argc, const char *argv[])
{
//非函数实现strcat
char a[20]="asdf";
char b[20]="123";
int i,j;
for(i=0;a[i]!='\0';i++);
while(b[j]!='\0')
{
a[i]=b[j];
i++;j++;
}
puts(a);
puts(b);
return 0;
}
(4)strcmp【交换字符串】
#include <stdio.h>
int main(int argc, const char *argv[])
{
//非函数实现strcmp
char a[20]="";
char b[20]="";
gets(a);
gets(b);
int i=0,j=0;
while(a[i]==b[j])
{
if(a[i]=='\0')
{
break;
}
i++;j++;
}
if(a[i]>b[j])
printf("a>b\n");
else if(a[i]<b[j])
printf("a<b\n");
else if(a[i]=b[j])
printf("a=b\n");
return 0;
}