#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<10;i++)
for(j=1;j<10;j++)
printf((j==9)?"%4d\n":"%4d", i*j);
}
### 徐彬彬提供
#include <stdio.h>
int main()
{
int i,j;
for (i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
if(i>=j)
printf("%d x %d =%2d ",j,i,i*j);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
void main()
{
int i,j,k;
i=j=1;
for (int n=0; n<20; n++) {
if (n<2) {
printf("%4d", 1);
} else {
printf("%4d", i+j);
k=j;
j=(i+j);
i=k;
}
}
}
#下面是丁帅帅作法
#include <stdio.h>
main()
{
int a[20],i,n=20,m=0;
a[0]=1;a[1]=1;
for(i=2;i<n;i++)
a[i]=a[i-1]+a[i-2];
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
m++;
if(m%5==0)
printf("\n");
}
}
#刘义宝做法
#include <stdio.h>
void main()
{
int i=1, total, success=0;
while (!success) {
total = i*7;i++;
if (total%2==1 && total%3==2 && total%5==4 && total%6==5) {
success = 1;
}
}
printf("%d\n", total);
}
#丁帅帅作法
#include <stdio.h>
main()
{
int i=0;
while(i%2!=1&&i%3!=2&&i%5!=4&&i%6!=5)
{
i=i+7;
}
printf("%d\n",i );
}
#书上答案
#include <stdio.h>
main()
{
int x=7;
while(x%3!=2 || x%5!=4 || x%6!=5)
x+=14;
printf("%d\n", x);
}
#刘义宝做法
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,k,aLength;
char a[80] = "zkcvbn";
char b[80] = "asfd";
aLength = strlen(a);
for(i=0; b[i]; i++){
a[aLength] = b[i];
aLength++;
}
aLength = strlen(a);
for(i=0; i<aLength-1; i++){
for(j=i+1; j<aLength; j++){
if (a[i] > a[j]) {
k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
printf("%s\n", a);
}
#丁帅帅做法
#include <stdio.h>
#include <string.h>
main()
{
char a[]="zhangsan",b[]="wangwu",c[20];
int i=0,j=0;
while(a[i])
{
c[i]=a[i];i++;
}
while(b[j])
{
c[i+j]=b[j];j++;
}
c[i+j]='\0';
puts(c);
//排序由徐彬彬添加
len = strlen(c);
for (i=0;i<len-1;i++)
{
for(j=0;j<len-1;j++)
{
if(c[j]>c[j+1])
{
t = c[j];
c[j] =c[j+1];
c[j+1] =t;
}
}
}
puts(c);
}
#书上答案
void main()
{
int i, flag=0;
char name[5][20] = {"Li Fei", "He Fei", "Liu Lu", "Zhang San", "Wang Na"};
char yourname[20];
printf("Enter your name:");gets(yourname);
for (i=0; i<5; i++)
if (strcmp(name[i], yourname) == 0) flag=1;
puts(yourname);
if (flag)
printf("is in this class");
else
printf("is not in this class");
}
#书上答案
void main()
{
char str1[80], str2[80]; int i,j;
gets(str1);
gets(str2);
i=strlen(str1);
j=0;
while (str2[j]!='\0') str1[i++]=str2[j++];
str1[i]='\0';
printf("%s", str1);
}
#书上答案
void main()
{
int data=0,i,n; char str[10];
printf("enter hex string\n");
gets(str);
for(i=0; str[i]!='\0'; i++){
if(str[i]>='0'&&str[i]<='9')
n=str[i]-'0';
else if(str[i]>='A'&&str[i]<='F')
n=str[i]-'A'+10;
else exit(0);
data=data*16+n;
}
printf("%d", data);
}
#以 下 程 序 中 , 函 数 c o l l e c t 的 功 能 是 对 n 位 学生的考试 成绩 统 计 总 平 均 分 和 低 于 总 平
#均分的人 数 , 本题 约 定, 人 数 的统 计 数 由函 数 返 回 , 总平均分 则 由形 式 参 数 a v e r 带出
#该 程 序 有 错 误 , 请指 出其 出错 行 , 并修改 它 直 至达 到 程 序 的预 期 功 能 为 止
#下面是改正好的代码
#include <stdio.h>
#define N 10
int collect(float s[], int n, float *aver)
{
int i,count=0;
float x=0.0;
for(i=0; i<N; i++) x+=s[i];
printf("%d", x);
x=x/N;
for(i=0; i<N; i++)
if(s[i]<x) count++;
*aver = x;
return count;
}
mainx()
{
float s[N],aver; int i,num;
for(i=0;i<N;i++) scanf("%f", &s[i]);
num=collect(s,N,&aver);
printf("average=%.1f\n", aver);
printf("<%.1f is:%d\n", aver, num);
}
#丁帅帅答案 比书上答案思路清晰
#编写函数 int isprime(int n),判断整数n是否是为素数,再设计main函数,
#调用isprime函数,输出3至m(3<m<100)之内的所有素数
#解析:素数指只能被1和它本身整除的数 2,3,5,7,11,13,....
#include <stdio.h>
int isprime(int n)
{
int i,yn=1;
for(i=2;i<=n/2;i++)
if(n%i==0)
{
yn=0;
}
return yn;
}
main()
{
int m,n;
scanf("%d",&m);
for(n=3;n<m;n++)
{
if(isprime(n))
{
printf("%d", n);
}
}
}
#徐彬彬做法
#include <stdio.h>
int findsub(char* str1,char* str2)
{
int i=0,j,pos;
while(str1[i] != '\0')
{
//查找到第一个相同字母的位置
if(str1[i] == str2[0])
{
j=0;
//开始用小的遍历大的
while(str2[j] != '\0'&& str1[i+j] == str2[j])
{
j++;
}
//如果已经遍历到小的末尾了
if(str2[j]=='\0')
{
return i+1;
}
}
i++;
}
return -1;
}
int main()
{
char *s1="wheresverkmn",*s2="sv";
printf("%d\n",findsub(s1,s2));
return 0;
}
#丁帅帅做法
#include<stdio.h>
int findsub(char *str1,char *str2)
{
int i=0,yn;
while(str1[i])
{
for(;str1[i]!=str2[0];i++);
if(str1[i]==str2[0])
{
for(j=0;str1[i+j]!='\0'&&str1[i+j]==str2[j];j++);
if(str2[j]=='\0')
{
return i+1;
i++;
}
return -1;
}
}
}
main()
{
char *s1="wherever",*s2="er";
printf("%d",findsub(s1,s2))
}