滴滴后端8.21第一题

n=abc+acc,a是1-9,b与c是0-9,a b c互不相等,100<n<2000,输出abc  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        int count=0;
        ArrayList<Integer> arr = new ArrayList<>();
        for (int i = 100; i < n; i++) {
            for (int j = i; j < n; j++) {
                if (i+j==n){
                    String str1=String.valueOf(i);
                    String str2=String.valueOf(j);
                    char[] chars1 = str1.toCharArray();
                    char[] chars2 = str2.toCharArray();
                    if (chars1.length==3 && chars2.length==3){
                        if (chars1[0]!=chars1[1] & chars1[1]!=chars1[2] & chars1[0]!=chars1[2] & chars1[0]==chars2[0] & chars1[2]==chars2[2] & chars2[2]==chars2[1]){
                            count+=1;
                            arr.add(i);
                        }
                    }

                }
            }
        }
        Collections.sort(arr);
        System.out.println(count);
        for (int i = 0; i <arr.size() ; i++) {
            if (i==arr.size()-1){
                System.out.print(arr.get(i));
            }
            else{
                System.out.print(arr.get(i)+" ");
            }
        }

    }

}




#笔试题目#
全部评论
#include<iostream> #include<bits/stdc++.h> using namespace std; vector<int>v1; vector<int>v2; int main() {     int n;     cin>>n;     if(n<=100||n>=2000){         cout<<0;         return 0;     }     int ans=0;     for(int a=1;a<=9;a++){         for(int b=0;b<=9;b++){             for(int c=0;c<=9;c++){                 if(a==b||a==c||b==c){                     continue;                 }                 int abc=a*100+b*10+c;                 int acc=a*100+c*10+c;                 if(abc+acc==n){                     //cout<<"abc:"<<abc<<",acc:"<<acc<<endl;                     ans++;                     v1.push_back(abc);                     v2.push_back(acc);                 }             }         }     }     cout<<ans;     if(ans>0){         cout<<"\n";         for(int i=0;i<ans;i++){             cout<<v1[i]<<" "<<v2[i]<<endl;         }     } }
点赞 回复 分享
发布于 2020-08-21 21:09

相关推荐

点赞 2 评论
分享
牛客网
牛客企业服务