Write a program that will read unknown number of three integers, and for each triple (a, b, c) will print the number (a or b) that contains the digit c more times on even positions (the positions are counted from right to left, and the first position is 1).
Here’s my try (it gives me a time limit exceeded error, it’s a problem with the function find):
#include <iostream>
using namespace std;
int find(int a, int b, int c){
int counta=0, countb=0;
int tempa=a, tempb=b;
while(tempa>0){
if(((tempa%100)/10)==c){
counta++;
tempa/=100;
}
}
while(tempb>0){
if(((tempb%100)/10)==c){
countb++;
tempb/=100;
}
}
if(counta>countb){
return a;
}
else{
return b;
}
}
int main(){
int a,b,c;
cin>>a>>b>>c;
while(cin>>a>>b>>c){
cout<<find(a,b,c)<<endl;
}
return 0;
}
//TYYY if someone helps <3
New contributor
Kiruma Souichi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3