很简单的题,我们使用unsigned long long防止数据溢出,计算后通过循环算出x的位数,然后用取模运算判断最后几位是不是与原数相等。
这道题需要注意的就是输入的x也可能不是两位数,有可能是一位数或者三位数、四位数……
1 2 3 4 5 6 7 8 9 10
intIsAutomorphic(int x){ unsignedlonglong s =(unsignedlonglong) x * x; int temp = x; int bit =1; while(temp !=0){ temp /=10; bit *=10; } return(s % bit)== x; }
intmain(){ int n; scanf("%d",&n); int result =0; int plus =1; int amount =0; int level =1; for(int i =0; i != n;++i){ result += plus; if(++amount == level){ amount =0; ++plus; ++level; } } printf("%d", result); return0; }
intmain(){ int n, k; scanf("%d %d",&n,&k); int result =0; for(int i = n, p =0; i !=0;){ ++result; if(++p == k){ p =0; }else{ --i; } } printf("%d", result); return0; }
intmain(){ int n, k; scanf("%d %d",&n,&k); int result = n; int j, m; do{ j = n / k; m = n % k; result += j; n = j + m; }while(n >= k); printf("%d", result); return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13
intsimulate(int n,int k){ int j = n / k; int m = n % k; if(j + m < k)return j; return j +simulate(j + m, k); }
intmain(){ int n, k; scanf("%d %d",&n,&k); printf("%d", n +simulate(n, k)); return0; }
这里我们使用一个数组amount存储每个分数的人数,用score存储每个同学的成绩。然后使用sum存储低于某个分数的人数,比如sum[i]就是比i分低的人数(这个 sum 其实就是前缀和的思想)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intmain(){ int amount[1001]={0}; int score[100000]; int n; scanf("%d",&n); for(int i =0; i != n;++i){ scanf("%d",&score[i]); ++amount[score[i]]; } int sum[1001]={0}; for(int i =1; i !=1001;++i){ sum[i]= sum[i -1]+ amount[i -1]; } for(int i =0; i != n;++i){ printf("%d ", sum[score[i]]); } return0; }
intmain(){ int input; bool start = false; while(~scanf("%d",&input)){ if(start)printf("\n"); else start = true; int d =(input >>24)&0xff; int c =(input >>16)&0xff; int b =(input >>8)&0xff; int a = input &0xff; printf("%d", a + b + c + d); } return0; }
intmain(){ int n; scanf("%d",&n); int s =1; for(int line =1; line != n;++line){ int space = n - line; for(int i =0; i != space;++i)printf(" "); printf("*"); if(line !=1){ for(int i =0; i != s;++i)printf(" "); printf("*"); s +=2; } printf("\n"); } for(int i =(n ==1? s -1:-2); i != s;++i)printf("*"); return0; }
// 二分查找 // 参数: // num[] - 源数组 // length - 数组的大小 // key - 要查找的值 // 返回值: // 如果找到指定的数,那么返回其在数组中的下标 // 如果数组中不包含指定的数,那么返回-(插入点 + 1) // 插入点: // 数组中第一个大于key的元素的下标 intbinarySearch(int num[],int length,int key){ int low =0; int high = length -1; while(low <= high){ int mid =(low + high)>>1; int cmp = num[mid]- key; if(cmp <0) low = mid +1; elseif(cmp >0) high = mid -1; else return mid;// key found } return-(low +1);// key not found. }
intmain(){ int n, m, x, temp; scanf("%d %d %d",&n,&m,&x); int A[n]; for(int i =0; i != n;++i)scanf("%d",&A[i]); for(int i =0; i != m;++i){ scanf("%d",&temp); int key = x - temp; int index =binarySearch(A, n, key); if(index >=0){ printf("%d %d", index, i); break; } } return0; }
/** * Searches a range of * the specified array for the specified object using the binary * search algorithm. * The range must be sorted into ascending order * according to the * {@linkplain Comparable natural ordering} * of its elements (as by the * {@link #sort(Object[], int, int)} method) prior to making this * call. If it is not sorted, the results are undefined. * (If the range contains elements that are not mutually comparable (for * example, strings and integers), it <i>cannot</i> be sorted according * to the natural ordering of its elements, hence results are undefined.) * If the range contains multiple * elements equal to the specified object, there is no guarantee which * one will be found. * * @param a the array to be searched * @param fromIndex the index of the first element (inclusive) to be * searched * @param toIndex the index of the last element (exclusive) to be searched * @param key the value to be searched for * @return index of the search key, if it is contained in the array * within the specified range; * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The * <i>insertion point</i> is defined as the point at which the * key would be inserted into the array: the index of the first * element in the range greater than the key, * or <tt>toIndex</tt> if all * elements in the range are less than the specified key. Note * that this guarantees that the return value will be >= 0 if * and only if the key is found. * @throws ClassCastException if the search key is not comparable to the * elements of the array within the specified range. * @throws IllegalArgumentException * if {@code fromIndex > toIndex} * @throws ArrayIndexOutOfBoundsException * if {@code fromIndex < 0 or toIndex > a.length} * @since 1.6 */