Sgu 111
|
111. Very simple problem time limit per test: 0.25 sec. memory limit per test: 4096 KB
You are given natural number X. Find such maximum integer number that it square is not greater than X.
Input Input file contains number X (1≤X≤101000).
Output Write answer in output file.
Sample Input 16 Sample Output 4 |
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <string> 11 #include <math.h> 12 #include <stdlib.h> 13 #include <time.h> 14 using namespace std; 15 /* 16 * 完全大数模板 17 * 输出cin>>a 18 * 输出a.print(); 19 * 注意这个输入不能自动去掉前导0的,可以先读入到char数组,去掉前导0,再用构造函数。 20 */ 21 #define MAXN 9999 22 #define MAXSIZE 1010 23 #define DLEN 4 24 25 class BigNum 26 { 27 private: 28 int a[MAXN]; //可以控制大数的位数 29 int len; 30 public: 31 BigNum(){ len = 1; memset(a, 0, sizeof(a)); } //构造函数 32 BigNum(const int); //将一个int类型的变量转化成大数 33 BigNum(const char*); //将一个字符串类型的变量转化为大数 34 BigNum(const BigNum &); //拷贝构造函数 35 BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算 36 friend istream& operator>>(istream&, BigNum&); //重载输入运算符 37 friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符 38 39 BigNum operator+(const BigNum &)const; //重载加法运算符,两个大数之间的相加运算 40 BigNum operator-(const BigNum &)const; //重载减法运算符,两个大数之间的相减运算 41 BigNum operator*(const BigNum &)const; //重载乘法运算符,两个大数之间的相乘运算 42 BigNum operator/(const int &)const; //重载除法运算符,大数对一个整数进行相除运算 43 44 BigNum operator^(const int &)const; //大数的n次方运算 45 int operator%(const int &)const; //大数对一个int类型的变量进行取模运算 46 bool operator>(const BigNum &T)const; //大数和另一个大数的大小比较 47 bool operator>(const int &t)const; //大数和一个int类型的变量的大小比较 48 49 void print(); //输出大数 50 }; 51 BigNum::BigNum(const int b) //将一个int类型的变量转化为大数 52 { 53 int c, d = b; 54 len = 0; 55 memset(a, 0, sizeof(a)); 56 while (d>MAXN) 57 { 58 c = d - (d / (MAXN + 1))*(MAXN + 1); 59 d = d / (MAXN + 1); 60 a[len++] = c; 61 } 62 a[len++] = d; 63 } 64 BigNum::BigNum(const char *s) //将一个字符串类型的变量转化为大数 65 { 66 int t, k, index, L, i; 67 memset(a, 0, sizeof(a)); 68 L = strlen(s); 69 len = L / DLEN; 70 if (L%DLEN)len++; 71 index = 0; 72 for (i = L - 1; i >= 0; i -= DLEN) 73 { 74 t = 0; 75 k = i - DLEN + 1; 76 if (k<0)k = 0; 77 for (int j = k; j <= i; j++) 78 t = t * 10 + s[j] - '0'; 79 a[index++] = t; 80 } 81 } 82 BigNum::BigNum(const BigNum &T) :len(T.len) //拷贝构造函数 83 { 84 int i; 85 memset(a, 0, sizeof(a)); 86 for (i = 0; i<len; i++) 87 a[i] = T.a[i]; 88 } 89 BigNum & BigNum::operator=(const BigNum &n) //重载赋值运算符,大数之间赋值运算 90 { 91 int i; 92 len = n.len; 93 memset(a, 0, sizeof(a)); 94 for (i = 0; i<len; i++) 95 a[i] = n.a[i]; 96 return *this; 97 } 98 istream& operator>>(istream &in, BigNum &b) 99 { 100 char ch[MAXSIZE * 4]; 101 int i = -1; 102 in >> ch; 103 int L = strlen(ch); 104 int count = 0, sum = 0; 105 for (i = L - 1; i >= 0;) 106 { 107 sum = 0; 108 int t = 1; 109 for (int j = 0; j<4 && i >= 0; j++, i--, t *= 10) 110 { 111 sum += (ch[i] - '0')*t; 112 } 113 b.a[count] = sum; 114 count++; 115 } 116 b.len = count++; 117 return in; 118 } 119 ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符 120 { 121 int i; 122 cout << b.a[b.len - 1]; 123 for (i = b.len - 2; i >= 0; i--) 124 { 125 printf("%04d", b.a[i]); 126 } 127 return out; 128 } 129 BigNum BigNum::operator+(const BigNum &T)const //两个大数之间的相加运算 130 { 131 BigNum t(*this); 132 int i, big; 133 big = T.len>len ? T.len : len; 134 for (i = 0; i<big; i++) 135 { 136 t.a[i] += T.a[i]; 137 if (t.a[i]>MAXN) 138 { 139 t.a[i + 1]++; 140 t.a[i] -= MAXN + 1; 141 } 142 } 143 if (t.a[big] != 0) 144 t.len = big + 1; 145 else t.len = big; 146 return t; 147 } 148 BigNum BigNum::operator-(const BigNum &T)const //两个大数之间的相减运算 149 { 150 int i, j, big; 151 bool flag; 152 BigNum t1, t2; 153 if (*this>T) 154 { 155 t1 = *this; 156 t2 = T; 157 flag = 0; 158 } 159 else 160 { 161 t1 = T; 162 t2 = *this; 163 flag = 1; 164 } 165 big = t1.len; 166 for (i = 0; i<big; i++) 167 { 168 if (t1.a[i]<t2.a[i]) 169 { 170 j = i + 1; 171 while (t1.a[j] == 0) 172 j++; 173 t1.a[j--]--; 174 while (j>i) 175 t1.a[j--] += MAXN; 176 t1.a[i] += MAXN + 1 - t2.a[i]; 177 } 178 else t1.a[i] -= t2.a[i]; 179 } 180 t1.len = big; 181 while (t1.a[len - 1] == 0 && t1.len>1) 182 { 183 t1.len--; 184 big--; 185 } 186 if (flag) 187 t1.a[big - 1] = 0 - t1.a[big - 1]; 188 return t1; 189 } 190 BigNum BigNum::operator*(const BigNum &T)const //两个大数之间的相乘 191 { 192 BigNum ret; 193 int i, j, up; 194 int temp, temp1; 195 for (i = 0; i<len; i++) 196 { 197 up = 0; 198 for (j = 0; j<T.len; j++) 199 { 200 temp = a[i] * T.a[j] + ret.a[i + j] + up; 201 if (temp>MAXN) 202 { 203 temp1 = temp - temp / (MAXN + 1)*(MAXN + 1); 204 up = temp / (MAXN + 1); 205 ret.a[i + j] = temp1; 206 } 207 else 208 { 209 up = 0; 210 ret.a[i + j] = temp; 211 } 212 } 213 if (up != 0) 214 ret.a[i + j] = up; 215 } 216 ret.len = i + j; 217 while (ret.a[ret.len - 1] == 0 && ret.len>1)ret.len--; 218 return ret; 219 } 220 BigNum BigNum::operator/(const int &b)const //大数对一个整数进行相除运算 221 { 222 BigNum ret; 223 int i, down = 0; 224 for (i = len - 1; i >= 0; i--) 225 { 226 ret.a[i] = (a[i] + down*(MAXN + 1)) / b; 227 down = a[i] + down*(MAXN + 1) - ret.a[i] * b; 228 } 229 ret.len = len; 230 while (ret.a[ret.len - 1] == 0 && ret.len>1) 231 ret.len--; 232 return ret; 233 } 234 int BigNum::operator%(const int &b)const //大数对一个 int类型的变量进行取模 235 { 236 int i, d = 0; 237 for (i = len - 1; i >= 0; i--) 238 d = ((d*(MAXN + 1)) % b + a[i]) % b; 239 return d; 240 } 241 BigNum BigNum::operator^(const int &n)const //大数的n次方运算 242 { 243 BigNum t, ret(1); 244 int i; 245 if (n<0)exit(-1); 246 if (n == 0)return 1; 247 if (n == 1)return *this; 248 int m = n; 249 while (m>1) 250 { 251 t = *this; 252 for (i = 1; (i << 1) <= m; i <<= 1) 253 t = t*t; 254 m -= i; 255 ret = ret*t; 256 if (m == 1)ret = ret*(*this); 257 } 258 return ret; 259 } 260 bool BigNum::operator>(const BigNum &T)const //大数和另一个大数的大小比较 261 { 262 int ln; 263 if (len>T.len)return true; 264 else if (len == T.len) 265 { 266 ln = len - 1; 267 while (a[ln] == T.a[ln] && ln >= 0) 268 ln--; 269 if (ln >= 0 && a[ln]>T.a[ln]) 270 return true; 271 else 272 return false; 273 } 274 else 275 return false; 276 } 277 bool BigNum::operator>(const int &t)const //大数和一个int类型的变量的大小比较 278 { 279 BigNum b(t); 280 return *this>b; 281 } 282 void BigNum::print() //输出大数 283 { 284 int i; 285 printf("%d", a[len - 1]); 286 for (i = len - 2; i >= 0; i--) 287 printf("%04d", a[i]); 288 printf("\n"); 289 } 290 BigNum x; 291 void fun(){ 292 BigNum l = 0, r = 1, mid; 293 for (int i = 0; i < 1000; i++)r =r* 10; 294 while (r > l){ 295 mid = (l + r) / 2; 296 if (mid*mid>x)r = mid; 297 else l = mid + 1; 298 } 299 cout << l - 1 << endl; 300 } 301 int main(){ 302 cin >> x; 303 fun(); 304 return 0; 305 }
浙公网安备 33010602011771号