当前位置:网站首页>C语言初级—常见问题(100~200素数,计算1+11+111+...,从键盘获取一个数并输出有几个位)
C语言初级—常见问题(100~200素数,计算1+11+111+...,从键盘获取一个数并输出有几个位)
2022-08-02 14:03:00 【iccoke】
输出100~200之间的素数
具体代码
#include<stdio.h>
int main()
{
int num = 100;
int i;
while (num <= 200)
{
for (i = 2; i < num; i++)
{
if (num % i == 0)
break;
}
if (i == num)
{
printf("100~200之间的素数有%d\n", num);
}
num++;
}
return 0;
}首先要求100~200之间获取素数,在判断前给出约束条件
函数体利用素数特性,即除去一和它本身不能有别的除数
计算1+11+111+...
在给出具体代码之前,观察加数形式,11,111
得出计算加数的关键代码
即 b = b + (int)a * pow(10, i)
由于pow函数计算后默认为double类型,因此这里用(int)进行强制转换
其次在用pow等其他数学函数时,头文件因加上#include<math.h>
具体代码如下
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int getsum(int n) {
int a=1;
int sn=0;
int b = 0;
for (int i = 0; i < n; i++)
{
b = b + (int)a * pow(10, i);
sn = sn + b;
}
return sn;
}
int main() {
int n;
scanf("%d", &n);
int result = getsum(n);
printf("%d\n", result);
return 0;
}从键盘获取一个整形并输出有几位
基本思想:数字每能被十除一次并得到大于一的数,则可以加位
具体代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
double num;
int a = 1;
scanf("%lf", &num);
while (10<=num )
{
num = num / 10;
a += 1;
}
printf("这是个%d位数字\n", a);
return 0;
}优化
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main(){
int num;
scanf("%d", &num);
int count = 0;
while (num != 0);
{
num /= 10;
count++;
}在定义输入输出时因尽量做到见名知意
边栏推荐
- What's wrong with running yolov5 (1) p, r, map are all 0
- EasyExcel 的使用
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
- 第十单元 前后连调
- [ROS](02)创建&编译ROS软件包Package
- window10下半自动标注
- Linux: CentOS 7 install MySQL5.7
- The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
- C语言一级指针(补)
- Error Correction Design Principle of Hamming Check Code
猜你喜欢

The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~

Unit 14 Viewsets and Routing

8580 Merge linked list

window10 lower semi-automatic labeling

Deep learning framework pytorch rapid development and actual combat chapter4

What's wrong with running yolov5 (1) p, r, map are all 0

Unit 15 Paging, Filtering

run yolov5

static关键字3种作用,简单粗暴对比,好理解

yolov5 improvement (1) Add attention focus mechanism
随机推荐
PHP open source customer service system _ online customer service source code PHP
window10下半自动标注
Basic operations of 8583 sequential stack
第十二单元 关联序列化处理
A little thought about password encryption
Flask框架的搭建及入门
window10 lower semi-automatic labeling
Chapter6 visualization (don't want to see the version)
yolov5,yolov4,yolov3乱七八糟的
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
Unit 6 meet ORM
The specific operation process of cloud GPU (Hengyuan cloud) training
redis delay queue
Deep learning framework pytorch rapid development and actual combat chapter3
YOLOv7使用云GPU训练自己的数据集
Unit 13 Mixing in View Base Classes
[ROS] Introduction to common tools in ROS (to be continued)
Raj delivery notes - separation 第08 speak, speaking, reading and writing
鼠标右键菜单栏太长如何减少
第十一单元 序列化器