题目1:
一个数如果恰好等于它的因子之和,这个数被成为”完数”,例如:6=1+2+3.请编程找出1000以内的完数
#include <stdio.h>
int main()
{
int i,j;
for(i=2;i<1000;i++)
{
int sum=0;
for(j=1;j<i/2+1;j++)
{
if(i%j == 0)
{
sum += j;
}
}
if(i == sum)
{
printf("%d\n",i);
}
}
return 0;
}
题目2:
编写程序,打印出9×9乘法表
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<10;i++)
{
for(j=1;j<=i;j++)
{
printf("%d*%d=%d ",i,j,i*j);
}
printf("\n");
}
return 0;
}
题目3:
编写程序,打印菱形星号组合
#include <stdio.h>
int main()
{
int x,m,y;
for (y=10;y>=-10;y--)
{
m = y;
for (x=0;x<=20;x++)
{
if (x == m || x == 20-m || x == -m || x == 20+m)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
题目4:
输入一个字符串,计算字符串中子串出现的次字数
#include <stdio.h>
int count(char *p1,char *p2)
{
int count=0;
char *pa = p1;
char *pb = p2;
while(1)
{
if(*pa == 0)
{
break;
}
if(*pa == *pb)
{
pb++;
if(*pb == 0)
{
count++;
pb = p2;
}
}
else
{
pb = p2;
}
pa++;
}
return count;
}
int main()
{
char str[100] ={0};
char str1[100]={0};
printf("Input string:\n");//输入字符串
scanf("%s",str);
printf("Input string:\n");//输入子串
scanf("%s",str1);
printf("count=%d\n",count(str,str1));
return 0;
}
题目5:
输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧
#include <stdio.h>
void fun(char *pstr)
{
char headstr[10]="head";
char tailstr[10]="tail";
char *p =pstr;
char *ph =headstr;
char *pt =tailstr;
int temp=1;
while(temp == 1)
{
if(*p == 0)
{
temp = 0;
break;
}
if(*p == *ph)
{
ph++;
if(*ph == 0)
{
p=p-3;
temp = 2;
break;
}
}
else
{
ph = headstr;
}
p++;
}
while(temp == 2)
{
if(*p == 0)
{
temp = 0;
break;
}
*pstr++=*p;
if(*p == *pt)
{
pt++;
if(*pt == 0)
{
*(++p)='\0';
*pstr='\0';
temp=3;
break;
}
}
else
{
pt = tailstr;
}
p++;
}
}
int main()
{
char str[100]={0};
printf("Input string:\n");
scanf("%s",str);
fun(str);
printf("%s\n",str);
return 0;
}
题目6:
有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
#include <stdio.h>
int main()
{
int n;
float a=2,b=1;
float sum=0;
for(n=0;n<20;n++)
{
sum+=a/b;
a=a+b;
b=a-b;
}
printf("%f\n",sum);
return 0;
}
题目7:
打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
#include <stdio.h>
int main()
{
int 1;
int j1,j2,j3;
for(i=100;i<=999;i++)
{
j1=i%10;
j2=(i/10)%10;
j3=i/100;
if(j1*j1*j1+j2*j2*j2+j3*j3*j3 == i)
printf("%d\n",i);
}
return 0;
}
题目8:
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制
#include <stdio.h>
int main()
{
int a,n,sum=0;
int i;
printf("Input a n\n");
scanf("%d,%d",&a,&n);
for(i=1;i<=n;i++)
{
sum=sum*10+i*a;
}
printf("sum = %d\n",sum);
return 0;
}
题目9:
将字符串“We Are Family!”,去除空格后打印出来。(WeAreFamily!)
#include <stdio.h>
void fun(char *pstr)
{
char *p=pstr;
int temp=1;
while(1)
{
if(*p == '\0')
{
*pstr='\0';
break;
}
if(*p == ' ')
{
p++;
continue;
}
*pstr++=*p++;
}
}
int main()
{
char str[100]="We Are Family!";
printf("%s\n",str);
fun(str);
printf("%s\n",str);
return 0;
}
题目10:
输入一个字符串,判断其是否是回文。(回文:即正读和反读都一样,如abcba, abccba)
#include <stdio.h>
int fun(char *pstr)
{
int left = 0;
int len=sizeof(pstr)/sizeof(int);
int right = len-1;
while(left < right)
{
if(pstr[left] != pstr[right])
{
return 0;
}
left++;
right--;
}
return 1;
}
int main()
{
char str[100]="aaabbbbbbbccccbbbbbbbaaa";
int temp=fun(str);
if(temp == 1)
{
printf("%s 是回文\n",str);
}
else
{
printf("%s 不是回文\n",str);
}
return 0;
}
题目12:
输入一段字符串,无论是否有重复字母出现,都只打印出现过的小写字母,并按照小写字母顺序打印。
(如输入qewqwr322rqw<>211qESFSSEraZz, 打印aeqrwz)
#include <stdio.h>
int main()
{
char str[256] = {0};
char c;
char i;
while(c != '\n')
{
scanf ("%c", &c);
str[c]++;
}
for(i='a';i<'z';i++)
{
if(str[i] != 0 || str[i+'A'-'a'] != 0)
{
printf("%c",i);
}
}
printf("\n");
return 0;
}