输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。
在一行中输出PA + PB的值。
3862767 6 13530293 3
399
很傻的方法,初学者的思路 #include <iostream> using namespace std; long PA(long A,long DA) { int i; int count=0; long PA=0; for(i=A;i!=0;i/=10) { if(i%10==DA) { count++; } } for(i=count;i>0;i--) { PA*=10; PA+=DA; } return PA; } int main() { long A,DA,B,DB; cin>>A>>DA>>B>>DB; cout<<PA(A,DA)+PA(B,DB); }
,int正数最大为20亿左右,所以不能用int,需要long long类型。
/* * app=PAT-Basic lang=c++ * https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328 */ #include <cstdio> int main() { long long A, B,PA = 0,PB = 0; int DA, DB; scanf("%lld%d%lld%d",&A,&DA,&B,&DB); while (A != 0){ if ((A % 10) == DA){ PA *= 10; PA += DA; } A /= 10; } while (B != 0){ if ((B % 10) == DB){ PB *= 10; PB += DB; } B /= 10; } printf("%lld",PA+PB); return 0; }
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = newScanner(System.in);int A = scanner.nextInt();int Da = scanner.nextInt();int B = scanner.nextInt();int Db = scanner.nextInt();int pa = calculateP(A, Da);int pb = calculateP(B, Db);System.out.println(pa + pb);scanner.close();}private static int calculateP(int num1, int num2) {int count = 0, newNum = 0;while(num1 != 0) {if(num1 % 10== num2)count++;num1 = num1 / 10;}if(count > 0) {for(inti = 0; i < count; i++) {newNum = newNum * 10+ num2;}}return newNum;}}
#include <iostream> using namespace std; int extractNum(int A, int DA); int main() { int A, DA, B, DB; cin >> A >> DA >> B >> DB; int PA, PB; PA = extractNum(A, DA); PB = extractNum(B, DB); cout << PA + PB << endl; return 0; } int extractNum(int A, int DA) { int res = 0; while(A>0) { int temp = A%10; if(temp == DA) { res = res*10 + DA; } A /= 10; } return res; }
#include<iostream> using namespace std; int main() { string a,b; int da,db; cin>>a>>da>>b>>db; int pa=0,pb=0; for(int i=0;i<a.length();i++){ if(a[i]-'0'==da){ pa=pa*10+da; } } for(int i=0;i<b.length();i++){ if(b[i]-'0'==db){ pb=pb*10+db; } } cout<<(pa+pb); return 0; }
int main()
{
string A, B;
char DA, DB;
int sum_A = 0, sum_B = 0;
int count_A = 0, count_B = 0;
cin >> A;
cin >> DA;
cin >> B;
cin >> DB;
for(int i = 0; A[i] != '\0'; i++)
{
if(A[i] == DA)
{
sum_A += pow(10, count_A) * (A[i] - '0');
count_A++;
}
}
for(int i = 0; B[i] != '\0'; i++)
{
if(B[i] == DB)
{
sum_B += pow(10, count_B) * (B[i] - '0');
count_B++;
}
}
cout << sum_A + sum_B;
}
//其实测试用例并不完整,感觉他没考虑Pa和Pb超出int范围的情况, //如下代码我凭着试一试的心态竟然通过了: #include <iostream> #include <sstream> #include <string> using namespace std; int main(){ string A = "", B = "", Pa = "", Pb = ""; int Da = 0, Db = 0; int lengthA = 0, lengthB = 0; int i = 0; cin >> A >> Da >> B >> Db; lengthA = A.length(); lengthB = B.length(); //求Pa for(i = 0;i < lengthA;i++){ if((A[i]-'0')==Da) Pa += A[i]; } //求Pb for(i = 0;i < lengthB;i++){ if((B[i]-'0')==Db) Pb += B[i]; } int PaInt = 0, PbInt = 0; stringstream ss1, ss2; ss1 << Pa; ss1 >> PaInt; ss2 << Pb; ss2 >> PbInt; cout<< PaInt+PbInt <<endl;//这里直接相加了,更严谨应该用类似大整数的方法去做。 return 0; }
var readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false }); rl.on('line', function(line) { var arr = line.split(' '); console.log((+arr[0].replace(new RegExp("[^" + arr[1] + "]", 'g'), '')) + (+arr[2].replace(new RegExp("[^" + arr[3] + "]", 'g'), ''))); });
#!/usr/bin/env python #-*- coding:utf8 -*- def findNum(s1, a, s2, b): n1, n2 = 0, 0 for i in s1: if i == a: n1 = n1 *10 + 1 for i in s2: if i == b: n2 = n2 *10 + 1 n1 *= int(a) n2 *= int(b) return n1+n2 if __name__ == '__main__': s1, a, s2, b = raw_input().split() print findNum(s1, a, s2, b)
主要错在最后那里
int a2=str1.length()>0?Integer.valueOf(str1):0;
要进行判断,要是str1是空的话,用不了这个方法
import java.util.Scanner;
public class Main {
public static void main(String[] s){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
int a1=sc.nextInt();
int b=sc.nextInt();
int b1=sc.nextInt();
String str1="";
String str2="";
while(a>0){
if(a%10==a1){
str1+=a1;
}
a/=10;
}
while(b>0){
if(b%10==b1){
str2+=b1;
}
b/=10;
}
int a2=str1.length()>0?Integer.valueOf(str1):0;
int b2=str2.length()>0?Integer.valueOf(str2):0;
System.out.println(a2+b2);
}
}
}
public class Main{ public static void main(String[] args){ Scanner s = new Scanner (System.in); int A = s.nextInt(); int DA = s.nextInt(); int B = s.nextInt(); int DB = s.nextInt(); int sum = print(A,DA)+print(B,DB); System.out.println(sum); } public static int print(int A, int DA){ //ArrayList<Integer> ls = new ArrayList<>(); int sum =0; String str = String.valueOf(A); for(int i=0; i<str.length(); ++i){ if((str.charAt(i)-'0') == DA){ sum=DA+sum*10; } } //int num = Integer.parseInt(str1); return sum; } }
#include<iostream>using namespace std;intmain(){intDA,DB;longintA,B;longintPA=0,PB=0;inti,PAnum=0,PBnum=0;cin>>A>>DA>>B>>DB;while(A>0){if(A%10== DA)PAnum++;A = A/10;}for(i=0;i<PAnum;i++){PA = PA*10+DA;}while(B>0){if(B%10== DB)PBnum++;B = B/10;}for(i=0;i<PBnum;i++){PB = PB*10+DB;}cout<<PA+PB<<endl;}
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); in.close(); String[] str = s.split(" "); int a = 0, b = 0; Long PA = Long.parseLong(str[1]); Long PB = Long.parseLong(str[3]); Long sumA = 0L, sumB = 0L; for (int i = 0; i < str[0].length(); i++) { if (str[1].charAt(0) == str[0].charAt(i)) { sumA = (long) (sumA + PA * Math.pow(10, a)); a++; } } for (int i = 0; i < str[2].length(); i++) { if (str[3].charAt(0) == str[2].charAt(i)) { sumB = (long) (sumB + PB * Math.pow(10, b)); b++; } } System.out.println(sumA+sumB); } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String A = in.next(); String DA = in.next(); String B = in.next(); String DB = in.next(); StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for(int i = 0;i<A.length();i++){ if(A.charAt(i)==DA.charAt(0)) sb1.append(DA); } for(int i = 0;i<B.length();i++){ if(B.charAt(i)==DB.charAt(0)) sb2.append(DB); } long a = sb1.length()==0?0:Long.parseLong(sb1.toString()); long b = sb2.length()==0?0:Long.parseLong(sb2.toString()); System.out.println(a+b); in.close(); } }
#include<stdio.h> #include<string.h> long fun(char *str,int d,int len) { int i; int result=0; for(i=0;i<len;i++) { if(str[i]==d+'0') { result=d+result*10; } } return result; } int main() { char str1[11],str2[11]; int da,db; long pa,pb; scanf("%s %d %s %d",str1,&da,str2,&db); int len1=strlen(str1); int len2=strlen(str2); pa=fun(str1,da,len1); pb=fun(str2,db,len2); printf("%d",pa+pb); return 0; }