Basic C algorithm problem I haven't touched C for many years. I'm very depressed~
// // // Algorithm Question// // Created by mac on 14-8-9. // Copyright (c) 2014 mac. All rights reserved.// #include <> #include <> //10. /* Find the value of S(n) = a+aa+aaa+aaaa+...+aa..a, where a is a number, n represents the number of digits of a for example: 2+22+222+2222+2222+2222 (at this time n=5), and both n and a are input from the keyboard. */ int questionN(int n) { int a = 2,num = 0; //n represents the number of sums he digits, a represents the base number int sum = 0; for (int i = 0 ; i++ < n; ) { if(i==0) { num = a; continue; } num = num*10 + a; //2+10^i printf("%d+",num); sum += num; } return sum; } //11. /* Monkeys eat peach problems. The monkey picked several peaches on the first day and ate half of them immediately. It was not satisfied, so he ate another one. The next morning, I ate half of the remaining peaches and ate one more. From then on, I ate half and one left of the day before every morning. By the morning of the 10th day, there was only one peach left. I would like to ask how many peaches I picked on the first day. */ int peank(int n) { if (n == 1 ) return 1; return (peank(n-1)+1)*2; } int my_peank() { int pean = 1,day = 9; for (;day-->0; ) pean = (pean+1)*2; return pean; } int main(int argc, const char * argv[]) { printf("A total of picks[%d]A peach...(My algorithm)\n",my_peank()); printf("A total of picks[%d]A peach...(Teacher's algorithm)\n",peank(10)); printf("=[%d]",questionN(3)); return 0; }
The above is the entire content of this article. I hope you can like it and you can directly criticize the interviewer when you encounter such a mentally retarded interview question.