前言
C语言是大多数小白走上程序员道路的第一步,在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。
Tips:题目是英文的,但我相信你肯定能看懂
一、There is only one thief among the four suspects A,B,C,D. and each of them can explain only once.
A said: it's not me.
B said: it's C.
C said: it's B.
D: C is talking nonsense.
Three of the four are known to have told the truth. Please program to find out who is the thief, output the corresponding capital letters.
#include <stdio.h>
int main()
{
char i;
for (i='A';i<='D';i++)
{
if ((i!='A')+(i=='C')+(i=='B')+(i!='B')==3)
printf("%c",i);
}
return 0;
}
二、Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4. show “no input” if input 0 only.
Here are sample runs:
<output1>
3 5 2 5 5 5 0 <enter icon>
largest number:5 occurrences count:4
<end output1>
<output2>
-3 -5 -2 -4 -2 -6 0 <enter icon>
largest number:-2 occurrences count:2
<end output2>
<output3>
0 <enter icon>
No input
<end output3>
#include <stdio.h>
#include <limits.h>
int main()
{
int a,b=INT_MIN,i=1;
while (scanf("%d",&a) && a!=0)
{
if (b==a)
i++;
if (a>b)
b=a;
}
if (b==0&&a==0)
printf ("No input");
else
printf("largest number:%d occurrences count:%d",b,i);
return 0;
}
三、Input a positive integer and output all the prime factors of the number with ascending order. You can stop it when input a negative number such as -1.
Here are sample runs:
<output>
120<enter icon>
2 2 2 3 5
6<enter icon>
2 3
32767<enter icon>
7 31 151
-3<enter icon>
Done
<end output>
#include <stdio.h>
int main()
{
int a,i;
while (scanf("%d",&a) && a>=0)
{
for (i=2;i<=a;i++)
{
if (a%i==0)
{
printf ("%d ",i);
a=a/i;
i=1;
}
}
putchar('\n');
}
printf("Done");
return 0;
}
四、Armstrong number is a number that is equal to the sum of digits raise to the power total number of digits in the number. Some Armstrong numbers are: 153, 370, 407, 1634, 8208, etc.
371 = 3^3 + 7^3 + 1^3 (27 + 343 +1)
8208 = 8^4 + 2^4 +0^4 + 8^4
please input a positive number N (3≤N≤7), and output all Armstrong number with N digits.Here are sample runs:
<output1>
3<enter icon>
153 370 371 407
<end output1>
<output2>
7<enter icon>
1741725 4210818 9800817 9926315
<end output2>
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c,d,i,t;
scanf ("%d",&a);
for (i=pow(10,a-1);i<pow(10,a);i++)
{
c=i,d=i;
for (t=a;t>0;t--)
{
b=pow(d%10,a);
c-=b;
d=d/10;
}
if (c==0)
printf ("%d ",i);
}
return 0;
}
五、 (Reduce fractions) Write a program that prompts the user to enter the numerator and denominator of a fraction. The program determines whether the number is a proper fraction or an improper fraction. If it is a proper fraction, display the number. If not, reduce it to a mixed irreducible fraction or to an integer. The program will be ended with the input for denominator is 0.
Here are sample runs:
<output>
Enter a numerator: 45 <enter icon>
Enter a denominator: 46 <enter icon>
45 / 46 is a proper fraction
Enter a numerator: 45 <enter icon>
Enter a denominator: 15 <enter icon>
45 / 15 is an improper fraction and it can be reduced to 3
Enter a numerator: 45 <enter icon>
Enter a denominator: 25 <enter icon>
45 / 25 is an improper fraction and its mixed fraction is 1 + 4 / 5
Enter a numerator: 45 <enter icon>
Enter a denominator: 0 <enter icon>
Done
<output>
#include <stdio.h>
int main()
{
int a,b,c,d,e,i;
while (1)
{
printf("Enter a numerator: ");
scanf("%d",&a);
printf("Enter a denominator:");
scanf("%d",&b);
if (b==0) break;
else
{
c=a-a/b*b;
if (a<b)
printf("%d / %d is a proper fraction\n",a,b);
else if (c==0)
printf("%d / %d is an improper fraction and it can be reduced to %d\n",a,b,a/b);
else
{
i=c<b? b: c;
for (;i>1;i--)
{
if (c%i==0&&b%i==0)
d=c/i,e=b/i;
}
printf("%d / %d is an improper fraction and its mixed fraction is %d + %d / %d\n",a,b,a/b,d,e);
}
}
}
printf ("Done");
return 0;
}
总结
以上就是本文全部内容,你学会了吗?