C语言/C++常见习题问答集锦(二)

C语言/C++常见习题问答集锦(二)

程序之美

在这里插入图片描述

1.【设计性实验】编写程序实现投票的过程。已知有三位候选人要参加竞选,先输入参与投票的人数和投票的内容,最终统计出三位候选人的最终得票,然后根据每个人总票数的高低来决定谁当选学生会主席。

思路:
(1)定义储存投票内容的数组变量,及存储三位候选人得票数的变量:
(2)输入参与投票的人数和投票的内容,将内容储存到数组中;
(3)对储存到数组中的元素进行判断,统计出各候选人的票数:
(4)根据三位各自的票数,判断胜利者是谁

#include<stdio.h>

#include<string.h>

int main()

{int i,j,a[3]={0};

 char name[3][9]={"张三","李四","王五"},s[9];  

 for(i=0;i<10;i++)

 {scanf("%s",s);

  for(j=0;j<3;j++)

    if(strcmp(s,name[j])==0)

      a[j]++;

 }

 for(i=0;i<3;i++)

   printf("%s: %d\n",name[i],a[i]);

 return 0;

}

在这里插入图片描述

2.用函数调用的形式打印九九乘法表

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

void getResult()
{
int i,j,c;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
c=i*j;
cout<<i<<"*"<<j<<"="<<setw(2)<<c<<" ";
}

cout<<endl;
}

cout<<endl;
}

int main()
{
getResult();
system("pause");
return 1;
}

3.设计函数求素数并应用

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

int isPrime(int x)
{
    int ret = 1;
    int i;
    if(x==1 || (x%2==0 && x!=2))
        ret = 0;
    for(i=3;i<sqrt(x);i+=2){
        if(x%i==0){
            ret = 0;
            break;
        }
    }
    return ret;
}

void main(){
    for(int i=2; i <= 2000; i++){
	if(isPrime(i)){
      	printf("%d是素数。\n",i);
	}
	else{
     	printf("%d不是素数。\n",i);
	}
}
    return 0;
}

在这里插入图片描述

4.给定任意三个点坐标(X1.y1)(x2.y2)(x3.y3)检验能否构成三角形,输入六个【-100.100】内的数字,即x1.y1.x2.y2.x3.y3若不能构成,输出"不可能"若可以,输出周长和面积

include<stdio.h>

#include<math.h>
double fun(double x1,double y1,double x2,double y2)
{
    double temp1=x1-x2;
    double temp2=y1-y2;
    return sqrt(temp1*temp1+temp2*temp2);
}

int main()
{
    double x1,y1;
    double x2,y2;
    double x3,y3;
    
    scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
    
    double lon1=fun(x1,y1,x2,y2);
    double lon2=fun(x2,y2,x3,y3);
    double lon3=fun(x3,y3,x1,y1);

    if(lon1+lon2>lon3&&lon1+lon3>lon2&&lon2+lon3>lon1)
    {
        double p=(lon1+lon2+lon3)/2.0;
        double A=sqrt(p*(p-lon1)*(p-lon2)*(p-lon3));
        printf("L = %.2lf, A = %.2lf\n",lon1+lon2+lon3,A);
    }
    else printf("Impossible\n");
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五一编程

程序之路有我与你同行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值