20 \百钱百鸡\问题。百钱买百鸡,
鸡翁一值钱三,鸡母一值钱二,鸡雏三值钱一,问鸡翁、鸡母、鸡雏各几何?*/ #include \main() {
int jiweng,jimu,jichou;
for(jiweng=1;jiweng<=33;jiweng++) for(jimu=1;jimu<=50;jimu++) {
jichou=100-jiweng-jimu;
if(jiweng*3+jimu*2+jichou*1.0/3==100)
printf(\,jiweng,jimu,jichou);
}
}
21 B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛. (1)A参加时,B也参加; (2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加; (4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。 (程序有误) #include \main() {
int a,b,c,d,e; /*用表示参加,表示未参加*/ for(a=0;a<=1;a++) for(b=0;b<=1;b++) for(c=0;c<=1;c++) for(d=0;d<=1;d++) for(e=0;e<=1;e++) {
if(a==1) b=1; if(b==1) c=0; else c=1; if(c==1) d=1;
else if(c==0) d=0; if(d||e) } }
22 输入一个字串,判断它是否是对称串。如”abcdcba”是对称串,”123456789”不是。 #include \#include \main() {
char s[50]; int i,flag=1;; gets(s);
for(i=0;i<=strlen(s)/2-1;i++) {
if(s[i]!=s[strlen(s)-i-1]) { flag=0;break; } }
if(flag==1)
{ printf(\是对称串\,s);} else
{ printf(\不是对称串\,s);} }
23 随机产生N个大写字母输出,然后统计其中共有多少个元音字符。(设N为200) #include \#include \#include \#define N 200 main() {
int i,count=0,ch; randomize();
for(i=1;i<=N;i++) {
ch=random(26)+65; printf(\,ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') count++; }
printf(\,count); }
24 从键盘输入长度不等的两个字串,将长串连接于短串之后输出。 include \main() {
char s1[50],s2[100];
gets(s1); /*输入长串*/ gets(s2); /*输入短串*/
puts(strcat(s2,s1)); }
25 键盘输入两个字串,输出其中较短的那个字串,并输出它的长度。 #include \#include \main() {
char s1[100],s2[100]; gets(s1); gets(s2);
if(strlen(s1) puts(s1); printf(\,strlen(s1)); } else { puts(s2); printf(\,strlen(s2)); } } 26 入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。、 #include \#include \main() { char s[100]; int i,ywcount=0,spcount=0,szcount=0,qtcount=0; gets(s); for(i=0;i if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z') ywcount++; else if(s[i]==' ') spcount++; else if(s[i]>='0'&&s[i]<='9') szcount++; else qtcount++; } printf(\英文字母=%d,空格=%d,数字=%d,其他字符=%d\,ywcount,spcount,szcount,qtcount); } 27 个字符串中的元音字母复制到另一字符串,然后输出“另一字符串”。 #include \#include \main() { char s1[100],s2[100]; int i,j=0; gets(s1); for(i=0;i<=strlen(s1);i++) { if(s1[i]=='a'||s1[i]=='e'||s1[i]=='i'||s1[i]=='o'||s1[i]=='u') { s2[j]=s1[i]; j++; } } puts(s2); } 28 字符数组str1种下标为偶数的元素赋给另一字符数组str2,并输出str1和str2。、 #include \#include \main() { char str1[100],str2[100]; int i,j=0; gets(str1); for(i=0;i<=strlen(str1);i=i+2) { str2[j]=str[i]; j++; } puts(str2); 29入一行英文,已知各单词之间用1个空格或一个标点符号相隔(设第一个单词前没有空格),统计这行英文有多少个单词。 #include \#include \main() { char s[100]; int i,count=0; gets(s); for(i=0;i if(s[i]==' ') count++; } printf(\,count+1); } 30输入一行字符串,按如下规则加密:如果是英文字母则大写变小写、小写变大写,对非英文字符则保持不变。试写加密程序。 #include \#include \main() { char s[100]; int i; gets(s); for(i=0;i if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]+32; else if(s[i]>='a'&&s[i]<='z') s[i]=s[i]-32; } puts(s); }