输入的c值 | 输出的a值 |
---|---|
$ | 8.26 |
7 | 13.48 |
% | 1.10 |
# | 0.075 |
其他字符 | 0 |
/*switch-case写法*/
#include
int main(int argc, char *argv[])
{
char c;
float a;
scanf("%c",&c);
switch(c){
case '$':a=8.26;break;
case '&':a=13.48;break;
case '%':a=1.10;break;
case '#':a=0.075;break;
default:a=0;break;
}
printf("a=%f\n",a);
return 0;
}
/*if-else写法*/
#include
int main(int argc, char *argv[])
{
char c;
float a;
scanf("%c",&c);
if(c=='$')
a=8.26;
else if(c=='&')
a=13.48;
else if(c=='%')
a=1.10;
else if(c=='#')
a=0.075;
else
a=0;
printf("a=%f\n",a);
return 0;
}
#include
int main(int argc, char *argv[])
{
int i,num=0;
for(i=100;i<=1000;i++){
if(i%7==0&i%9==0){
printf("%d ",i);
num++;
if(num%5==0)
printf("\n");
}
}
return 0;
}
输入字符 | 输出数据 |
---|---|
u | 826.72 |
e | 1182.30 |
j | 15.13 |
h | 109.65 |
其他字符 | 0 |
/*if-else写法*/
#include
int main(int argc, char const *argv[])
{
char c;float d;
scanf("%c",&c);
if (c=='u')
{
a = 826.72;
}else if (c=='e')
{
a = 1182.30;
}else if (c=='j')
{
a = 15.13;
}else if (c=='h')
{
a = 109.65;
}else
a = 0;
printf("a=%f\n",a);
return 0;
}
/*switch-case写法*/
#include
int main(int argc, char const *argv[])
{
char c;float d;
scanf("%c",&c);
switch(c){
case 'u': a=826.72;break;
case 'e': a=1182.30;break;
case 'j': a=15.13;break;
case 'h': a=109.65;break;
default:a=0;break;
}
printf("a=%f\n",a);
return 0;
}
sum=11+13+15+……+197+199
#include
int main(int argc, char const *argv[]){
int i,sum=0;
for (int i = 11; i <=199; i+=2){
sum+=i;
}
printf("sum=%d\n", sum);
return 0;
}
S = 1 + 1/1! + 1/2! + 1/3! +1/4! + …… + 1/n!
#include
double fun(float x){
if (x==1)
return 1;
else
return x*fun(x-1);
}
int main(int argc, char const *argv[]){
float S=1.0;
int i,num;
scanf("%d",&num);
for (int i = 1; i <= num; i++){
S+=1/fun(i);
}
printf("S=%d\n", S);
return 0;
}
#include
int main(){
int i,n;
for(i=2;i<=100;i++){ //1肯定不是素数,所以从2开始遍历
for(n=2; n<i;n++) //判断是否为素数(除了1和本身,不能被其他的数整除),从2到i
if (i%n==0) //如果2到i中有任意的一个数可以整除,则退出
break;
if(n>=i)
printf("%d is prime number\n", i);
}
return 0;
}
z = { 3 x ² − y ² , x > 0 且 y > 0 4 x ³ + y ³ , x < 0 且 y < 0 0 , x 、 y 不 满 足 以 上 两 个 条 件 时 z= \begin{cases} 3x²-y², x>0且y>0\\ 4x³+y³, x<0且y<0\\ 0 , x、y不满足以上两个条件时 \end{cases} z=⎩⎪⎨⎪⎧3x²−y²,x>0且y>04x³+y³,x<0且y<00,x、y不满足以上两个条件时
#include
int main(){
int x,y,z=0;
scanf("%d%d",&x,&y);
if(x>0&&y>0)
z=3*x*x-y*y;
else if(x<0&&y<0)
z=4*x*x*x+y*y*y;
else
z=0;
printf("z=%d",z);
return 0;
}
#include
int main( )
{
int year;
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
printf("%d is RunNian",year);
else
printf("%d is not RunNian",year);
return 0;
}
输入数据 | 输出等级 |
---|---|
300 | A |
200-299 | B |
100-199 | C |
0-99 | D |
/*if-else写法*/
#include
int main(){
int num;
char Grade;
scanf("%d",&num);
if(num==300)
Grade = 'A';
else if(num>=200&&num<=299)
Grade = 'B';
else if(num>=100&&num<=199)
Grade = 'C';
else if(num>=0&&num<=99)
Grade = 'D';
else
printf("输入的数据不合法!");
if(Grade=='A'||Grade=='B'||Grade=='C'||Grade=='D')
printf("Grade=%c\n",Grade);
else
printf("\n");
return 0;
}
/*switch写法*/
#include
int main(){
int num;
char Grade;
scanf("%d",&num);
switch(num/100){
case 3:Grade='A';break;
case 2:Grade='B';break;
case 1:Grade='C';break;
case 0:Grade='D';break;
default: printf("输入的数据不合法!");
}
if(Grade=='A'||Grade=='B'||Grade=='C'||Grade=='D')
printf("Grade=%c\n",Grade);
else
printf("\n");
return 0;
}
s u m = 1 − 2 3 + 4 5 − 6 7 + … … − 94 95 + 96 97 − 98 99 + 100 101 sum=1- \frac 23+\frac 45-\frac 67+……-{94\over 95}+{96\over 97}-{98\over 99}+{100\over 101} sum=1−32+54−76+……−9594+9796−9998+101100
#include
int main(){
float sum=1.0,symbol=1.0;
int i;
for(i=0;i<=50;i++){
sum+=symbol*(2*i)/(2*i+1);
symbol=-symbol;
}
printf("%f",sum);
return 0;
}
写法1:
#include
int main(){
char s1[100],s2[50];
int i=0,j=0;
scanf("%s",s1);
scanf("%s",s2);
while(s1[i]!='\0')
i++;
while(s2[i]!='\0')
s1[i++]=s2[j++];
s1[i]!='\0';
printf("%s\n",s1);
return 0;
}
写法2:
#include
int main(){
char s1[100],s2[50];
int i=0,j=0;
gets(s1);
gets(s2);
while(s1[i]!='\0')
i++;
while(s2[i]!='\0')
s1[i++]=s2[j++];
s1[i]!='\0';
puts(s1);
return 0;
}
方法1:判断各个位上是否有非0数字
#include
int main(){
int num_in,num_dight=0;//num_in为输入的数,num_dight为判断几位数的变量
int num_gewei,num_shiwei,num_baiwei,num_qianwei;
scanf("%d",&num_in);
if(num_in>=0&&num_in<=9999){
num_qianwei=num_in/1000%10;
num_baiwei=num_in/100%10;
num_shiwei=num_in/10%10;
num_gewei=num_in/1%10;
if(num_qianwei!=0)
num_dight=4;
else if(num_qianwei==0&&num_baiwei!=0)
num_dight=3;
else if(num_qianwei==0&&num_baiwei==0&&num_shiwei!=0)
num_dight=2;
else
num_dight=1;
printf("您输入的num_in=%d是%d位数\n",num_in,num_dight);
}else
printf("您输入的数据不合法!\n");
return 0;
}
方法2:直接比较法
#include
int main(){
int num_in,num_dight=0;
scanf("%d",&num_in);
if(num_in>=1000&&num_in<=9999)
num_dight=4;
else if(num_in>=100&&num_in<=999)
num_dight=3;
else if(num_in>=10&&num_in<=99)
num_dight=2;
else if(num_in>=0&&num_in<=9)
num_dight=1;
else
printf("您输入的数不合法!");
if(num_dight==1||num_dight==2||num_dight==3||num_dight==4)
printf("您输入的是%d位数\n",num_dight);
else
printf("\n");
return 0;
}
方法3:do-while循环
#include
int main(){
int num_in,num_dight=0;
scanf("%d",&num_in);
do{
num_dight+=1;
num_in/=10;
}while(num_in>0);
printf("您输入的是%d位数\n",num_dight);
return 0;
}
#include
int main(){
int i,j;
for(i=1;i<=9;i++)
for(j=1;j<=9;j++){
if(j<i)
printf(" ");
else
printf("%d*%d=%-2d\t",i,j,i*j);
}
printf("\n");
return 0;
}
#include
int main()
{
printf("输入一行字符:\n");
char ch;
int i,count=0,word=0;
while((ch=getchar())!='\n')
if(ch==' ')
word=0;
else if(word==0)
{
word=1;
count++;
}
printf("总共有 %d 个单词\n",count);
return 0;
}
#include
int main()
{
int num1,num2,num3,temp;
scanf("%d,%d,%d",&num1,&num2,&num3); /*输入任意的3个整数*/
if(num1>num2){ /*如果num1大于num2,借助中间变量temp实现num1与num2值的互换*/
temp=num1;
num1=num2;
num2=temp;
}
if(num1>num3){ /*如果num1大于num3,借助中间变量temp实现num1与num3值的互换*/
temp=num1;
num1=num3;
num3=temp;
}
if(num2>num3){ /*如果num2大于num3,借助中间变量temp实现num2与num3值的互换*/
temp=num2;
num2=num3;
num3=temp;
}
printf("The order of the nums is:%d,%d,%d",num1,num2,num3); /*输出函数顺序输出num1、num2、num3的值*/
return 0;
}
猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将第一天剩下的桃子吃掉一半,有多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,发现只剩下一个桃子了。编写程序求猴子第一天摘了多少个桃子。
/*while循环*/
#include
int main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0){
x1=(x2+1)*2;
x2=x1;
day--;
}
printf("peach sum is%d\n",x1);
return 0;
}
/*for循环*/
include <stdio.h>
int main()
{
int day,x1,x2;
x2=1;
for(day=9;day>0;day--){
x1=(x2+1)*2;
x2=x1;
}
printf("peach sum is%d",x1);
return 0;
}
完数概念:如果一个数等于它的因子之和,则称该数为“完数”(或“完全数”)。例如,6的因子为1、2、3,而 6=1+2+3,因此6是“完数”。
#include
int main()
{
int i,j,sum,num; /*变量i控制选定数范围,j控制除数范围,s记录累加因子之和*/
printf("请输入所选范围上限:\n");
scanf("%d",&num); /* num的值由键盘输入*/
for(i=2;i<=num;i++){
sum=0; /*保证每次循环时sum的初值为0*/
for(j=1;j<i;j++){
if(i%j==0) /*判断j是否为i的因子*/
sum+=j;
}
if(sum==i) /*判断因子的和是否和原数相等*/
printf("%d is a perfect number!\n",i);
}
return 0;
}
#include
int main(int argc, char *argv[])
{
int a,b;//输入的两个数
int sum=0;//两个数的和或差的初始值
scanf("%d,%d",&a,&b);
if(a%2!=0&&b%2!=0)
// printf("%d",sum=a-b);
sum+=(a-b);
else
sum+=(a+b);
printf("%d",sum);
return 0;
}
#include
int main(int argc, char *argv[])
{
char c,d;
c=getchar();
if(c>='A'&&c<='Z')
printf("%c",c);
else{
c-=32;
printf("%c",c);
}
return 0;
}
#include
int main(int argc, char *argv[])
{
int i;
for(i=10;i<100;i++){
if(i%3==0&&i%5==3&&i%7==1)
printf("%d\n",i);
}
return 0;
}
#include
int main(int argc, char *argv[])
{
int i,sum=0;
for(i=1;i<=1000;i++){
if((i%3==0||i%13==0)&&!(i%3==0&&i%13==0)){
printf("%d ",i);//验证数字用
sum+=1;
}
}
printf("%d\n",sum);
return 0;
}
#include
int main(int argc, char *argv[])
{
int i,a,sum=0,num=0;//num为偶数个数,sum为偶数和
for(i=1;i<=20;i++){
scanf("%d",&a);
if(a%2==0){
//printf("%d\n",a);
num+=1;
sum+=a;
}
}
printf("num=%d\n",num);
printf("sum=%d\n",sum);
return 0;
}
#include
int main(int argc, char *argv[])
{
int i,a,sum=0;
for(i=1;i<=10;i++){
scanf("%d",&a);
if(a>0){
sum+=a;
}
}
printf("sum=%d\n",sum);
return 0;
}
#include
int a,b,c,d,pingfang;
void fun(int x){
a=x/1000%10;
b=x/100%10;
c=x/10%10;
d=x/1%10;
pingfang=a*a+b*b+c*c+d*d;
printf("%d",pingfang);
}
int main(int argc, char *argv[])
{
fun(2345);
return 0;
}
int a,b,c,d,e,sum;
int fun(int x){
a=x/10000%10;
b=x/1000%10;
c=x/100%10;
d=x/10%10;
e=x/1%10;
sum=a+b+c+d+e;
}
int main(int argc, char *argv[])
{
int i,sum_number=0;
for(i=100;i<=10000;i++){
if(fun(i)%7==0)
sum_number+=1;
}
printf("%d\n",sum_number);
return 0;
}
#include
int a,b,c,sum;
int fun(int x){
a=x/100%10;
b=x/10%10;
c=x/1%10;
sum=a+b+c;
}
int main(int argc, char *argv[])
{
int i,sum_number=0;
for(i=100;i<=999;i++){
if(fun(i)==15){
sum_number+=1;
printf("%d\n",i);
}
}
printf("%d\n",sum_number);
return 0;
}
(当前程序逻辑应该没有问题,就是在Cfree编辑器里不执行)
1-1/2×2-2/3×3-3/4×4-……-(n-1)/n×n
#include
int main(int argc, char *argv[])
{
int n;
float i,sum=1.0;
printf("请输入正整数n:");
scanf("%d\n",&n);
if(n<=0)
printf("输入错误,请输入正整数!");
else if(n==1)
printf("sum=%f",sum);
else{
for(i=2;i<=n;i++)
sum-=((i-1)/(i*i));
printf("sum=%f",sum);
}
return 0;
}
#include
int main(int argc, char *argv[])
{
float a[20]={2.0,3.0};
float b[20]={1.0,2.0};
int i;
float sum=0;
for(i=0;i<20;i++){
if(i>=2){
a[i]=a[i-1]+a[i-2];
b[i]=a[i-1];
}
sum+=a[i]/b[i];
}
printf("%f",sum);
return 0;
}
#include
int main(int argc, char *argv[])
{
float num=0,sum=0;
int i;
for(i=1;i<=1000000;i++){
if(i%2==0)
num=-i;
else
num=i;
sum+=1/num;
}
printf("sum=%f",sum);
return 0;
}
#include
int main(int argc, char *argv[])
{
int a[10],i,min,max;
for(i=0;i<10;i++){
scanf("%d",&a[i]);//将10个数存入数组
}
for(i=0;i<9;i++){
if(a[i]>a[i+1]){
max=a[i];
//min=a[i+1];
a[i]=a[i+1];
a[i+1]=max;
}
}
for(i=9;i>0;i--){
if(a[i]>a[i+1]){
//max=a[i];
min=a[i+1];
a[i]=min;
a[i+1]=a[i];
}
}
for(i=0;i<10;i++){
printf("%d ",a[i]);//将10个数存入数组
}
return 0;
}
#include
int main(int argc, char *argv[])
{
int score[10],i;
float average=0,sum=0;
for(i=0;i<10;i++){
scanf("%d",&score[i]);
sum+=score[i];
}
average=sum/10;
for(i=0;i<10;i++)
if(score[i]
#include
int main(int argc, char *argv[])
{
int a[10],i,sum=0;
for(i=0;i<10;i++){
scanf("%d",&a[i]);
if(i%2==1&&a[i]%2==1)
sum+=a[i];
}
printf("sum=%d",sum);
return 0;
}
#include
#include
int main(int argc, char *argv[])
{
char s1[100],s2[50];
int i=0,j=0;
gets(s1);
gets(s2);
while(s1[i]!='\0'){
if(s1[i]>='a'&&s1[i]<='z'){
s2[j]=s1[i];
j++;
}
i++;
}
s2[j]='\0';
puts(s2);
return 0;
}