蓝桥杯备赛day01:循环

这类题目较为简单,就不写解析了,提供三种语言的参考代码,欢迎在评论区讨论!

分离整数的各个位数
cpp 复制代码
#include<iostream>
using namespace std;
int main(){
	int n;
	cin>>n;
	while(n>0){
		cout<<n%10<<" ";
		n=n/10;
	}
	return 0;
}
python 复制代码
n = int(input())
while n > 0:
    print(n % 10, end=' ')
    n = n // 10
java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n > 0) {
            System.out.print(n % 10 + " ");
            n = n / 10;
        }
        scanner.close();
    }
}
数字统计
cpp 复制代码
#include<iostream>
using namespace std;
int main()
{
    //读入L和R
    int L,R;
    cin>>L>>R;
    //范围[L, R]的所有整数
    int sum = 0;//sum和
    for(int i = L;i<=R;i++){//i遍历了L到R的所有数字
        int j = i;//i在循环里发生了变化,应该用别的数来存i。
        while(j>0){
            if(j%10==2){//判断此时的个位数是否是数字2.
               sum++;
            }
            j/=10;
        }
    }
    //输出
    cout<<sum;
    return 0;
}
python 复制代码
L, R = map(int, input().split())

sum_count = 0
for i in range(L, R + 1):
    j = i
    while j > 0:
        if j % 10 == 2:
            sum_count += 1
        j //= 10

print(sum_count)
java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int L = scanner.nextInt();
        int R = scanner.nextInt();
        int sumCount = 0;
        
        for (int i = L; i <= R; i++) {
            int j = i;
            while (j > 0) {
                if (j % 10 == 2) {
                    sumCount++;
                }
                j /= 10;
            }
        }
        System.out.println(sumCount);
        scanner.close();
    }
}
与7无关的数
cpp 复制代码
#include<iostream>
using namespace std;
 
bool isRelated(int n)
{
    if(n % 7 == 0)  // 能被7整除
        return true;
    while(n)    // 数字上有7
    {
        if(n % 10 == 7)
            return true;
        n /= 10;
    }
    return false;
}
 
int main()
{
    int n, sum = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)
        if(!isRelated(i))   // 如果与7无关
            sum += i * i;
    cout << sum << endl;
    return 0;
}
python 复制代码
def is_related(n):
    if n % 7 == 0:  # 能被7整除
        return True
    while n > 0:    # 数字中有7
        if n % 10 == 7:
            return True
        n //= 10
    return False

n = int(input())
sum_of_squares = 0
for i in range(1, n + 1):
    if not is_related(i):  # 如果与7无关
        sum_of_squares += i * i

print(sum_of_squares)
java 复制代码
import java.util.Scanner;
public class Main {
    public static boolean isRelated(int n) {
        if (n % 7 == 0) {  // 能被7整除
            return true;
        }
        while (n > 0) {    // 数字中有7
            if (n % 10 == 7) {
                return true;
            }
            n /= 10;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sumOfSquares = 0;
        
        for (int i = 1; i <= n; i++) {
            if (!isRelated(i)) {  // 如果与7无关
                sumOfSquares += i * i;
            }
        } 
        System.out.println(sumOfSquares);
        scanner.close();
    }
}
相关推荐
m0_6312704030 分钟前
标准C++(二)
开发语言·c++·算法
沫刃起32 分钟前
Codeforces Round 972 (Div. 2) C. Lazy Narek
数据结构·c++·算法
寂然如故1 小时前
Anaconda 安装
python
GZM8888881 小时前
配置VS Code以进行C/C++编程:深入探讨与实操指南
c++
martian6652 小时前
学懂C++(六十):C++ 11、C++ 14、C++ 17、C++ 20新特性大总结(万字详解大全)
开发语言·c++·c++20
zhangbin_2372 小时前
【Python机器学习】NLP信息提取——命名实体与关系
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
985小水博一枚呀2 小时前
【梯度消失|梯度爆炸】Vanishing Gradient|Exploding Gradient——为什么我的卷积神经网络会不好呢?
人工智能·python·深度学习·神经网络·计算机视觉·cnn·numpy
小灰灰爱代码3 小时前
C++——判断year是不是闰年。
数据结构·c++·算法
小灰灰爱代码3 小时前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数数的情况),用函数重载方法。
数据结构·c++·算法
Kerwin要坚持日更3 小时前
Java小白一文讲清Java中集合相关的知识点(九)
java·开发语言