思考题目,仔细检查,外加一个ceil函数
題目:
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The first few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.
Input
The input consists of a series of lines with each line containing one integer value i (1 ≤ i ≤ 2 ? 109 ). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing ‘0’.
Output
For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.
Sample Input
1
12
24
0
Sample Output
1
33
151
這道題目就是要求你找出第n個回文序列數(shù)字,容易看出一位數(shù)的回文9種,二位數(shù)的回文9種,三位數(shù)的回文90種,四位數(shù)的回文90種,所以我們設一位數(shù)a[1]=9,a[2]=9后面就用一個函數(shù)得出來,a[i]=a[i-2]*10;規(guī)律容易,但是要想清楚怎么把一個個回文數(shù)字得出來,這題改bug改了兩個來小時,最后別人提醒我可能數(shù)組開小了,然后才過了,值得后面再做幾遍鍛煉邏輯能力
#include <bits/stdc++.h> using namespace std; long long a[20]; int b[20]; void value(){a[1]=9,a[2]=9;for(int i=3;i<=19;){a[i]=a[i-2]*10;a[i+1]=a[i-1]*10;i+=2;} } int main() {long long i,j,n,x,y,flag1,flag;value();while(cin>>n){if(n==0){break;}i=1,y=1;x=n;while(n>a[i]){n-=a[i];i++;}flag1=i;if((flag1)%2){flag=(flag1)/2+1;}else{flag=(flag1)/2;}b[1]=n*9/a[flag1];if((n*9)%a[flag1]){b[1]++;n-=(b[1]-1)*a[flag1]/9+1;}else{n-=(b[1]-1)*a[flag1]/9+1;}for(i=2;i<=flag;i++){y=1;for(j=2;j<=i;j++){y*=10;}b[i]=n/(a[flag1]/9/y)-1;if((n-(a[flag1]/9/y)*b[i])!=0){b[i]+=1;}n-=b[i]*(a[flag1]/9/y);}for(i=1;i<=flag;i++){cout<<b[i];}if(flag1%2){for(i=1;i<=flag-1;i++){cout<<b[flag-i];}}else{for(i=1;i<=flag;i++){cout<<b[flag-i+1];}}cout<<endl; }return 0; }最后AC的感覺真爽
然后是一個ceil函數(shù),頭文件是#include<math.h>,這個函數(shù)感覺挺有用的,就是求一個數(shù),大于等于這個數(shù)的最小整數(shù)值。
附上一個水題目加深印象。
A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).
Input
The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤ b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive).
Sample Input
1?4
1?10
0?0
Sample Output
2
3
這題就是給你兩個數(shù),讓你求出兩個數(shù)之間有可以開出整數(shù)平方根的數(shù)目。
#include<iostream> #include<math.h> using namespace std; int main() {long long n,x,m,y;while(cin>>n>>m){if(n==0&&m==0){break;}x=ceil(sqrt(n));y=sqrt(m);cout<<y-x+1<<endl; } return 0; }?
?
轉載于:https://www.cnblogs.com/wangzhelin/p/9366290.html
總結
以上是生活随笔為你收集整理的思考题目,仔细检查,外加一个ceil函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python入门学习笔记11(静态方法、
- 下一篇: CentOS下开启配置端口转发