19_1_C语言基础_Lab06_函数初步

1.立方和

(循环)编写程序输出这样的三位数:这个三位数本身正好等于其每个数字的立方和。(如 153 = 1的立方+5的立方+3的立方)数字间用空格隔开。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    for(num=100;num<1000;num++)
    {
        if(num==target(num))
            printf("%d ",num);
    }
    return 0;
}
int target(int n)
{
    int n1,a,b,c;
    a=n/100;//hundred
    n=n%100;
    b=n/10;//ten
    c=n%10;
    n1=a*a*a+b*b*b+c*c*c;
    return n1;
}

2.素数分解//哥德巴赫猜想

(循环)In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: Every even number greater than 4 can be written as the sum of two odd prime numbers.
For Example:
8 = 3 + 5. (Both 3 and 5 are odd prime numbers.)
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
Write a program to solve this problem. The value of even number is given by user.
输入不大于1000,请以=a+b的形式输出所有情况,a≤b,以a的升序输出。
样例输入:
20
样例输出:
20=3+17=7+13

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,a;
    scanf("%d",&num);
    printf("%d",num);
    for(i=2;i<num-1;i++)
    {
        a=num-i;
        if((judgesu(a)==0)&&(judgesu(i)==0)&&(i<=a))
            printf("=%d+%d",i,a);
    }
    return 0;
}

int judgesu(int n)
{
    int cnt=0,j;
    for(j=2;j<n;j++)
    {
        if(n%j==0)
            cnt++;
    }
    return cnt;
}

3.ASCII码表的可打印字符

(函数)Write a function that will output the printable characters for character code values from st(0-127) to ed(0-127). Output each character code along with its symbol with two characters to a line. Make sure the columns are aligned. (Hint: You can use the isgraph() function that’s declared in ctype.h to determine when a character is printable.)
输入两个空格隔开的数字st和ed,st<ed,范围同题目描述。
输出包含st和ed对应字符(若符合要求),若没有字符满足要求则输出“NONE”。
每行的输出格式如“65 A”(不含引号),要求字符列对齐,最后一行没有换行。
样例输入1:
0 2
样例输出1:
NONE
样例输出2:
98 102
样例输出2:
98 b
99 c
100 d
101 e
102 f

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    int st,ed,flag=0,i;
    scanf("%d %d",&st,&ed);
    for(i=st;i<=ed;i++)
    {
        if(isgraph(i)!=0)
        {
            flag=1;
            if(wei(i)==2)
               printf("%d      %c\n",i,i);//平台不认制表符\t……
            else printf("%d     %c\n",i,i);
        }
    }
    if(flag==0)
    {
        printf("NONE\n");
    }
    return 0;
}

int wei(int x)
{
    int count1=0;
    while(x!=0)
    {
        x = x/10;
        count1++;
    }
    return count1;
}

4.8进制转16进制

(函数)请写函数完成8进制数向10进制数的转换;(在main函数中的8进制数由键盘输入),输入保证了输出为非负int类型整数。
样例输入:
100
样例输出:
64

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int a;
	scanf("%o",&a);
	printf("%d",a);
	return 0;
}

5.除3余6

(循环)请编写程序输出100以内能被3整除且个位数为6的所有整数。两个数之间用空格隔开,行末没有空格。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n1;

    for(n1=0;n1<100;n1++)
    {
        if((n1%10==6)&&(n1%3==0))
            printf("%d ",n1);
    }
    return 0;
}

6.2/1序列求和

(循环)编写程序,求分数序列2/1,3/2,5/3,8/5,13/8,21/13….的前50项之和。结果保留2位小数。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long a=2,b=1,i;
    double total=0;
    for(i=1;i<=50;i++)
    {
        total=total+1.0*a/b;
        a=a+b;
        b=a-b;
    }
    printf("%.2lf",total);
    return 0;
}

7.字母构成的菱形

(循环)编写程序,输出字母组成的菱形图案,第一行为1个字母A,第二行为3个字母B,以此类推,第n行为2n-1个对应的字母,以后每行递减。整数n(2≤n≤13)由键盘输入。输出行末没有空格。
样例输入:
5
样例输出:
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,m=0,n,x;
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        for(m=2*(n-i);m>0;m--)
        {
            printf(" ");
        }
        printf("1");
        for(j=2;j<=i;j++)
        {
            printf("+%d",j);
        }
        printf("=");
        for(x=i;x>=2;x--)
        {
            printf("%d+",x);
        }
        printf("1");
        printf("\n");
    }
    return 0;
}

8.等式三角形

(循环)用户输入 n ,输出如样例所示的行数为 n 的等式三角形。
其中第 k 行形如:
1+2+3+…+k=k+…+3+2+1
每行的等号对齐。
【输入】
一个正整数 n,1 ≤ n < 10
【输出】
如样例所示的行数为 n 的等式三角形,行末无空格,最后一行行末无换行。
【样例输入1】
3
【样例输出1】
        1=1
    1+2=2+1
1+2+3=3+2+1
【样例输入2】
5
【样例输出2】
                1=1
            1+2=2+1
        1+2+3=3+2+1
    1+2+3+4=4+3+2+1
1+2+3+4+5=5+4+3+2+1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,m=0,n,x;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(m=2*(n-i);m>0;m--)
        {
            printf(" ");
        }
        printf("1");
        for(j=2;j<=i;j++)
        {
            printf("+%d",j);
        }
        printf("=");
        for(x=i;x>=2;x--)
        {
            printf("%d+",x);
        }
        printf("1");
        printf("\n");
    }
    return 0;
}

9.乘法口诀表

(函数)请用函数分解的方法实现Beginning C的chapter 4-1的练习:
乘法口诀表的输出。输入正整数1≤n≤9,注意格式,参考样例输出。
样例输入:
5
样例输出:
1 * 1 = 1
1 * 2 =  2 2 * 2 =   4
1 * 3 = 3 2 * 3 = 6 3 * 3 =   9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            if(wei(i*j)==1)
               printf(" %d * %d =  %d",j,i,i*j);//格式对齐
               else if(wei(i*j)==2)
                printf(" %d * %d = %d",j,i,i*j);
            if(i==j){
                printf("\n");
                continue;
            }
        }
    }
    return 0;
}

int wei(int x)
{
    int count=0;
    while(x!=0)
    {
        x=x/10;
        count++;
    }
    return count;
}

家具店的收银台

(函数)You are working for a lumber(家具) company, and they would like a program that calculates the cost of lumber for a order. The company sells pin, fir, cedar, maple, and oak lumber. Lumber is priced by board feet. One board foot equals one a square foot, one inch thick. The price per board foot is given in the following table:
Pine 0.89
Fire 1.09
Cedar 2.26
Maple 4.50
Oak 3.10
The lumber is sold in different dimensions (specified in inches of width and height, and feet of length)that need to be converted to board feet. For example. A 248 piece is 2 inches wide, 4 inches high, and 8 feet long, and is equivalent to 5.333 board feet. An entry from the user will be in the form of a letter and four integer numbers. The integers are: number of pieces, width, height, and length. The letter will be one of P, F, C, M, O(corresponding to the five kinds of wood) or T, meaning Total. When the letter is T, There are no integers following it on the line. The program should print out the price for each entry, and print the total after T is entered. Here is an example run:

Enter item: P 10 2 4 8
10 248 Pine, Cost: $47.47
Enter item: M 1 1 12 8
1 1128 Maple, Cost: $36.00
Enter item: T
Total cost: $83.47
Develop the program, and use proper style and documentation in your code.
1英尺=12英寸,价格保留2位小数。
样例输入:(输入为T时输出total cost且停止输入)
P 10 2 4 8
M 1 1 12 8
T
样例输出:(每行输出对应每行输入)
10 2 * 4 * 8 Pine, Cost: $47.47
1 1*12 *8 Maple, Cost: $36.00
Total cost: $83.47

#include <stdio.h>
#include <stdlib.h>

double board(double w,double h,double l)
{
    double brd;
    brd=(w/12.0)*h*l;
    return brd;
}

int main()
{
    int num,w,h,l;
    char kind;
    char* name;
    double widthi,heighti,lengthf,per,cost,total=0;
    scanf("%c",&kind);
    while(kind!='T')
    {
        scanf(" %d %d %d %d",&num,&w,&h,&l);
        widthi=(double)w;heighti=(double)h;lengthf=(double)l;//输入用int,计算用double
        switch(kind)
        {
        case 'P': per=0.89; name="Pine";break;
        case 'F': per=1.09; name="Fire";break;
        case 'C': per=2.26; name="Cedar";break;
        case 'M': per=4.50; name="Maple";break;
        case 'O': per=3.10; name="Oak";break;
        default:break;
        }
        cost=num*per*board(widthi,heighti,lengthf);
        total=total+cost;
        printf("%d %d*%d*%d %s, Cost: $%.2f\n",num,w,h,l,name,cost+1e-6);//四舍五入
        scanf(" %c",&kind);
    }
    if(kind=='T')
        printf("Total cost: $%.2f",total);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,`__va_start`是一个宏定义,用于在函数内部初始化一个可变参数列表。它的定义如下: ```c #define __va_start(ap, v) (ap = (va_list)&v + _INTSIZEOF(v)) ``` 这个宏的作用是将指向可变参数列表的指针`ap`指向参数`v`的起始地址,并根据`v`所占据的内存大小调整指针的位置。这样,函数就可以通过指针`ap`遍历可变参数列表中的每个参数。 另外,还有一个相关的宏`va_arg`,用于获取可变参数列表中的下一个参数。它的定义如下: ```c #define va_arg(ap, t) (*(t*)((ap = (ap + _INTSIZEOF(t))) - _INTSIZEOF(t))) ``` 这个宏的作用是根据参数类型`t`,将指针`ap`向后移动相应的字节数,并返回移动前的指针位置处的值作为参数的值。 综上所述,`__va_start`和`va_arg`是C语言中用于处理可变参数列表的宏定义。`__va_start`用于初始化可变参数列表的指针,而`va_arg`用于获取可变参数列表中的参数值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [va_start和va_end,以及c语言中的可变参数原理](https://blog.csdn.net/weixin_35698867/article/details/117157195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值